Karel Zak [Thu, 12 Aug 2021 10:12:39 +0000 (12:12 +0200)]
libmount: change propagation of /run for X-mount.subdir
We do not need to create a new mount node from /run/mount/tmptgt
(where we mount filesystem root), because /run is already mount
node in all mainstream distros, and we can use MS_PRIVATE for
this top-level directory. There is still fallback if /run is
on root filesystem.
This solution reduces number of mount operations, with the patch the
subdir implementation is:
* open current namespace from /proc/self/ns/mnt
* mkdir /run/mount/tmptg (if it does not exist)
* make /run private by mount(MS_PRIVATE)
* mount filesystem to /run/mount/tmptg
* bind mount /run/mount/tmptg/<subdir> to <target>
* umount /run/mount/tmptg
* setns() to the original namespace
Karel Zak [Thu, 12 Aug 2021 08:58:18 +0000 (10:58 +0200)]
libmount: use /run/mount/tmptgt rather than /tmp/mount/mount.<pid>
The unshared namespace is per-process, so we can use the same
directory in all mount(8) instances. It's unnecessary to create
and remove process specific directory.
* open current namespace from /proc/self/ns/mnt
* mkdir /tmp/mount/mount.<pid>
* unshare mount namespace
* create mount node from /tmp/mount/mount.<pid> by mount(MS_BIND)
* mark /tmp/mount/mount.<pid> private by mount(MS_PRIVATE|MS_REC)
* bind mount /tmp/mount/mount.<pid>/subdir to <target>
* umount /tmp/mount/mount.<pid>
* rmdir /tmp/mount/mount.<pid>
* setns() to the original namespace
Note that /tmp/mount/mount.<pid> conversion to mount node and call for
MS_PRIVATE are visible (propagated to the system) if /tmp is a shared
filesystem, the rest (all operations with the desired filesystem) is
atomic for a parental namespace.
Maybe one day it will be possible to reimplement it in more
elegant way with new mount kernel APIs (open_tree(), etc.).
Fixes: https://github.com/karelzak/util-linux/issues/1103 Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/prlimit.c:467:16: warning: 'hard' may be used uninitialized in this function [-Wmaybe-uninitialized]
lim->rlim_max = hard;
~~~~~~~~~~~~~~^~~~~~
sys-utils/prlimit.c:456:15: note: 'hard' was declared here
rlim_t soft, hard;
^~~~
sys-utils/prlimit.c:466:16: warning: 'soft' may be used uninitialized in this function [-Wmaybe-uninitialized]
lim->rlim_cur = soft;
~~~~~~~~~~~~~~^~~~~~
sys-utils/prlimit.c:456:9: note: 'soft' was declared here
rlim_t soft, hard;
^~~~
References: https://github.com/karelzak/util-linux/issues/1406 Signed-off-by: Karel Zak <kzak@redhat.com>
Smitty [Sun, 8 Aug 2021 22:04:30 +0000 (18:04 -0400)]
whereis: use commands for Bash completions
Currently, the Bash completions for `whereis <TAB>` gives
`whereis file` ("file" is not a placeholder here, it literally expands
to that). This fixes that by passing the `-c` flag to `compgen` to
request completions for command names.
Karel Zak [Mon, 2 Aug 2021 08:40:10 +0000 (10:40 +0200)]
Merge branch 'chs' of https://github.com/pali/util-linux
* 'chs' of https://github.com/pali/util-linux:
fdisk: Add support for fixing MBR partitions CHS values
libfdisk: (dos) Add function fdisk_dos_fix_chs() for fixing CHS values for all partitions
libfdisk: (dos) Add function dos_partition_sync_chs() for updating CHS values
fdisk: Add support for fixing MBR partitions CHS values
Add a new extended option 'F' to fdisk which recalculates and fixes CHS
values for each partition in MBR table according to current fdisk settings
for number of heads and sectors per track.
This allows in interactive mode to repair existing partitions to be
compatible with CHS addressing after changing extended option 'h' (number
of heads) or 's' (sectors per track) as these options do not modify content
of existing partitions.
libfdisk: (dos) Add function fdisk_dos_fix_chs() for fixing CHS values for all partitions
This function fixes beginning and ending CHS values for every partition
according to LBA relative offset, relative beginning and size and fdisk
idea of disk geometry (sectors per track and number of heads). This
function may be used for repairing existing partitions to be compatible
with CHS addressing.
Karel Zak [Fri, 30 Jul 2021 09:50:46 +0000 (11:50 +0200)]
agetty: use CTRL+C to erase username
aggety(8) from the beginning ignores ^C (the small exception was
between 2.32 and 2.34 when this char has been misinterpreted).
This patch forces agetty to interpret ^C like ^U, it means to
erase the user's input and wait for a completely new username.
The small difference is that for ^C it does not set 'kill character'.
This change does not affect serial lines where ^C is still ignored like
in previous decades. I'd like to avoid any regression as I have
no clue if any serial lines do not send this control char in some
context ...
Fixes: https://github.com/karelzak/util-linux/issues/1399
References: https://github.com/karelzak/util-linux/issues/1046 Signed-off-by: Karel Zak <kzak@redhat.com>
Karel Zak [Thu, 29 Jul 2021 19:34:20 +0000 (21:34 +0200)]
Merge branch 'xget' of https://github.com/ericonr/util-linux
* 'xget' of https://github.com/ericonr/util-linux:
lib/pwdutils: use assert to check correct usage.
logger: use xgetlogin from pwdutils.
wall: use xgetlogin.
lib/pwdutils: don't use getlogin(3).
Karel Zak [Thu, 29 Jul 2021 18:37:32 +0000 (20:37 +0200)]
libmount: don't use setgroups at all()
It's probably good idea to call setgroups() to cleanup groups,
but it introduces a regression as some mount helpers depend on
supplementary groups like "network" etc.
Fixes: https://github.com/karelzak/util-linux/issues/1398 Signed-off-by: Karel Zak <kzak@redhat.com>
Since these functions are only used internally, we can make sure they
are being used correctly, and assert() helps in catching remaining
issues. Usage of each changed function has been reviewed:
For xgetpwnam:
- chsh(1) only calls it if a username has been set
- login(1) only calls it if username has been set and is not empty
- su(1) always initializes new_user to "root"
- unshare(1) calls get_user with optarg, so always set as well
For xgetgrnam:
- unshare(1) calls get_group with optarg
For xgetpwuid:
- chsh(1) passes a stack allocated struct for struct passwd
It defined its own xgetlogin, which queried geteuid() instead of getuid(),
with a fallback to "<someone>" when lookup fails. This has been the case
since the function was introduced in 019b97024fde3f07eaf541eef990762483369a11, so geteuid() has always been
used. Since using geteuid for identification isn't consistent with the
rest of util-linux, switching to xgetlogin(), which uses getuid(),
should be correct.
getlogin(3) shouldn't be used for identification here. This also removes
the bug where a missing entry for getuid() in passwd database wouldn't
print a warning, because whom would be set to "???".
For consistency, switch to "<someone>" when pw look up fails.
Per the man page, it shouldn't be used for security purposes. This is an
issue especially on musl, where getlogin is implemented as
getenv("LOGNAME"). Since xgetlogin is being used as user identity in su(1), to
set PAM_RUSER, we simply switch to always using getpwuid(getuid()).
Karel Zak [Tue, 27 Jul 2021 09:10:53 +0000 (11:10 +0200)]
libfdisk: (dos) don't ignore MBR+FAT use-case
Since libblkid commit 751eca28fcfa1e325770904d7181b22e48c72b29 it does
not ignore FAT on whole-disks, so now libfdisk sees collision between
FAT and MBR. It's fine to report the collision to users, but we should
not ignore the MBR.
Thomas Abraham [Thu, 22 Jul 2021 19:43:13 +0000 (15:43 -0400)]
blockdev: allow for larger values for start sector
commit 9147d2ad8a ("blockdev: Don't fail on missing start sector") limits
the size of the start sector to 10 digits.
Multi-terrabyte devices can have partitions with a start sector larger than
10 digits, which will cause an sprintf() to abort due to overflowing the buffer.
Karel Zak [Thu, 22 Jul 2021 09:03:54 +0000 (11:03 +0200)]
lslogins: use sd_journal_get_data() in proper way
man sd_journal_get_data:
The returned data is in a read-only memory map and is only valid until the next invocation
of sd_journal_get_data().
It means that use data after 3x sd_journal_get_data() is really bad
idea. It also seems better to not assume the fields are zero
terminated as journal API works with void* and size_t to address the
data.
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1984704 Signed-off-by: Karel Zak <kzak@redhat.com>
Karel Zak [Mon, 19 Jul 2021 11:34:24 +0000 (13:34 +0200)]
Merge branch 'chs' of https://github.com/pali/util-linux
* 'chs' of https://github.com/pali/util-linux:
tests: fdisk: Update CHS values in MBR partitions
libfdisk: (dos) Fix determining number of heads and sectors per track from MBR
libfdisk: (dos) Fix upper bound cylinder check in check_consistency()
libfdisk: (dos) Fix upper bound cylinder check in check()
libfdisk: (dos) Fix setting CHS values when creating new partition
libfdisk: (dos) Use helper macros cylinder() and sector() in check_consistency()
libfdisk: (dos) Put number of CHS check_consistency errors into summart message
libfdisk: (dos) Add check both begin and end CHS partition parameters
libfdisk: (dos) Fix check error message when CHS calculated sector does not match LBA
libfdisk: (dos) Fix printing number of CHS sectors in check error message
libfdisk: (dos) Recalculate number of cylinders after changing number of heads and sectors
It looks like that old CHS values prior this change in test data are
incorrect. Possible explanation why CHS values are incorrect is in commit
message 9cade4fbf8c4 ("libfdisk: (dos) Fix determining number of heads and
sectors per track from MBR").
This change updates test data to pass test suite after applying mentioned
commit.
libfdisk: (dos) Fix determining number of heads and sectors per track from MBR
Current algorithm implemented in get_partition_table_geometry() function
is wrong. It take head bits from CHS position from the end of the last
partition and proclaim that this value is number of heads on disk. Same
does for sectors bits from CHS and proclaim that this value is number of
sectors per tracks. As number of disk heads and head bits in CHS position
are different things this just results in bogus detected disk geometry.
Implement a new algorithm to determinate number of heads and sectors per
track from partition sector offsets. In MBR table for every partition is
stored every sector offset in two formats: one as CHS and one as LBA.
Conversion from CHS to LBA is defined by formula:
LBA = (c * N_heads + h) * N_sectors + (s - 1)
So if we have two different offsets in both CHS and LBA formats, written as
(LBA1, c1, h1, s1) and (LBA2, c2, h2, s2) then we can calculate number of
heads and sectors per tracks by solving linear equations as:
Integer division also verifies that offsets were calculated from same
number of heads and sectors per track as division must be always positive
with no reminder.
There is a special case when either numerator or denominator is zero. This
indicates that comparing partition offsets have also same CHS offset
divisor which leads to fact that calculation of number of heads and sector
per tracks is not possible.
In MBR table are stored up to 8 offset numbers, so it means there are up
to the 28 of candidates which can expose number of disk heads and number of
disk sectors per track. Start with offsets which belongs to same partition
and start from the first partition. As the first partition in most cases
was used by BIOS for booting.
It is required to skip CHS values 1023/254/63 and 1023/255/63 as these
indicates that values are invalid or overflowed.
libfdisk: (dos) Fix upper bound cylinder check in check_consistency()
To determinate if CHS values are valid for consistency check and not
overflowed, it is needed to check current CHS cylinder value and not total
disk cylinder values. It is possible that also on "big" disk there is first
small partition which can be represented by CHS tuple.
libfdisk: (dos) Fix upper bound cylinder check in check()
To check if LBA sector can be represented in CHS without overflow it is
required to check for cylinder value which belongs to the LBA sector. And
not the total number of disk cylinders. Note that maximal representable
total number of disk cylinders is 1024 and therefore the last cylinder
which which can be stored in CHS tuple is 1023. Hence strict inequality is
used.
libfdisk: (dos) Fix setting CHS values when creating new partition
It does not matter if dos compatibility flag is turned on or off. CHS
values in partition entry can overflow also when dos compatibility flag is
turned off. In this case maximal representable CHS values should be used.
libfdisk: (dos) Put number of CHS check_consistency errors into summart message
Function check_consistency() prints errors to console output but does not
calculate correct error numbers in summary error message. Fix it, so fdisk
prints correct number in summary message "N errors detected.".
libfdisk: (dos) Add check both begin and end CHS partition parameters
There is no reason to check only end CHS partition parameters. Check also
begin values and correctly count errors in nerrors variable. fdisk then
print correctly summary message "N errors detected.".
libfdisk: (dos) Fix check error message when CHS calculated sector does not match LBA
Function check() checks that LBA addresses sector number matches CHS sector
number. But error message contains information about "previous sectors" and
"total sectors" which is misleading so change it.
Also rename variables 'start' and 'total' to make it clear what value is
stored in them. Note that in 'start' variable is currently stored last LBA
partition sector and in 'total' is stored last CHS partition sector. So
neither sector where partition starts nor total sectors of partition.
Code and check logic looks to be correct, just error message and variable
names are misleading. Therefore no functional changes in this patch.
Todd Lewis [Thu, 15 Jul 2021 04:15:55 +0000 (00:15 -0400)]
rename: add --all and --last parameters
Renaming files with rename often involves multiple passes in order
to, say, replace all spaces with underscores because traditionally
rename only replaces the first occurrence of the expression. The
--all parameter makes this task simple.
With the addition of --last, rename becomes much safer to use when
replacing file extensions, whereas before it would mangle a file
which had its extension also embedded elsewhere in its name.
The implied --first, together with --all and --last, round out the
common cases for renaming files.