Commit Graph

41 Commits

Author SHA1 Message Date
Thomas Deutschmann 037c7816ce
switch_root: check if mount point to move even exists
Signed-off-by: Thomas Deutschmann <whissi@gentoo.org>
2021-02-08 15:35:07 +01:00
Karel Zak d3e58a8ec9 switch_root: fix double close [coverity scan]
Up on successful fdopendir(3) file descriptior that will be closed, that
happens in recursiveRemove() switch_root(8) function.

CID: 360697
Reference: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html
Co-Author: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-10-16 11:56:35 +02:00
Sami Kerola 12558a4c47 nsenter / switch_root: fix insecure chroot [coverity scan]
If a call to chroot is not followed by a call to chdir("/") the chroot jail
confinement can be violated.  See also CWE-243.

CID: 360718
CID: 360800
Reference: http://cwe.mitre.org/data/definitions/243.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-10-16 11:32:38 +02:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Paul Asmuth 9737a16711 fix a bug where switch_root would erroneously try to parse initargs
before this change, switch_root would try to parse all arguments,
including 'initargs', using getopt, which would lead to an 'unrecognized
option' error when trying to pass a flag to the init program
2018-11-12 14:20:45 +01:00
Karel Zak fd777151bf sys-utils: use errexec()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-01 15:44:25 +01:00
Ruediger Meier f45f3ec34a misc: consolidate macro style USAGE_HELP_OPTIONS
changed in include/c.h and applied via sed:

  sed -i 's/fprintf.*\(USAGE_MAN_TAIL.*\)/printf(\1/' $(git ls-files -- "*.c")
  sed -i 's/print_usage_help_options\(.*\);/printf(USAGE_HELP_OPTIONS\1);/' $(git ls-files -- "*.c")

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 16:54:33 +02:00
Ruediger Meier b1a294c448 misc: introduce print_usage_help_options()
Consolidate --help and --version descriptions. We are
now able to align them to the other options.

We changed include/c.h. The rest of this patch was
generated by sed, plus manually setting the right
alignment numbers. We do not change anything but
white spaces in the --help output.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 12:26:19 +02:00
Ruediger Meier 6e1eda6f22 misc: never use usage(stderr)
Here we fix all cases where we have usage(FILE*)
functions.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-26 14:38:24 +02:00
Sami Kerola 105bb8572f switch_root: use getopt_long() to parse options
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-05-10 10:53:28 +02:00
Patrick Steinhardt 85bfb519af switch_root: unlink files without _DIRENT_HAVE_D_TYPE
When _DIRENT_HAVE_D_TYPE is not defined, we need to always fstat the
directory entry in order to determine whether it is a directory or not.
If we determine that the file is indeed a directory on the same device,
we proceed to recursively remove its contents as well. Otherwise, we
simply skip removing the entry altogether.

This logic is not entirely correct though. Note that we actually skip
deletion of the entry if it is either not a directory or if it is not on
the same device. The second condition is obviously correct here, as we
do not want to delete files on other mounts here. But skipping deletion
of the entry itself if it is not a directory is wrong.

When _DIRENT_HAVE_D_TYPE is defined, this condition should never be
triggered, as we have already determined that the entry is a directory.
But if it is not, we will always do the fstat and check. Because of
this, we will now skip deletion of all files which are not directories,
which is wrong.

Fix the issue by disentangling both conditions. We now first check
whether we are still on the same device - if not, we skip recursive
deletion as well as deletion of the directory entry. Afterwards, we
check whether it is a directory - if so, we do delete its contents
recursively. And finally, we will now unlink the entry disregarding
whether it is a directory or not.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
2017-04-26 11:23:50 +02:00
Sami Kerola 341154da28
switch_root: simplify code and reduce indentation [oclint]
The if statement in line 162 already ensures value of cfd to be 0 or
greater, so the later if is not needed.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2016-07-21 21:14:33 +01:00
Benno Schulenberg fc14ceba5e textual: grammarize and harmonize the stat error message
The message "stat failed %s" seems to say that stat() failed to
do something, or failed to pass a test, but of course it means
that the statting of something failed.  So say so.  Also make
two very similar messages equal to this one.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-02-02 11:27:10 +01:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Karel Zak 64d0cee6c0 include/statfs_magic: use macro rather than type for f_type
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-11-10 11:29:42 +01:00
Karel Zak 8f806bb1ea switch_root: improve statfs->f_type portability
__SWORD_TYPE is not available everywhere, for example it's not defined
by musl libc. It also seems that __SWORD_TYPE is not used for f_type
on some architectures (s390x).

Reported-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-11-06 12:50:27 +01:00
Ruediger Meier 60cb2c3720 build: fix some compiler warnings
Most of them catched on 32bit gcc and icc.

disk-utils/fsck.cramfs.c:     printf format type
lib/boottime.c:               unused variables
misc-utils/cal.c:             set but never used
sys-utils/losetup.c:          set but never used
sys-utils/lscpu-dmi.c:        defined but not used
sys-utils/switch_root.c:      comparison between signed and unsigned
tests/helpers/test_sysinfo.c: printf format type
2014-05-30 01:18:09 +02:00
Dave Reisner c7832fb8f1 switch_root: verify initramfs by f_type, not devno
As of linux 3.14, the initramfs device will have both major and
minor 0, causing our paranoia check to fail. Make this version agnostic
by checking the filesystem type, rather than a device number.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
2014-04-04 14:55:13 +02:00
Karel Zak 7ad19a3feb switch_root: make dirent d_type usage more robust
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-03-04 11:45:44 +01:00
Richard Weinberger 07de470e43 switch_root: Add a sanity check
As switch_root basically does rm -Rf / we should make sure
that / is really an initramfs.

Signed-off-by: Richard Weinberger <richard@nod.at>
2013-02-13 14:55:30 +01:00
Karel Zak 17df84df27 switch_root: check open() return value [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-09-07 13:02:42 +02:00
Sami Kerola add1b8afd0 translation: unify stat error messages
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-07-16 18:18:22 +02:00
Sami Kerola efb8854f4c sys-utils: verify writing to streams was successful
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-04-04 19:49:40 +02:00
Sami Kerola dd41e06795 switch_root: close open file after usage [cppcheck]
[sys-utils/switch_root.c:159]: (error) Resource leak: cfd

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-02-08 14:23:28 +01:00
Harald Hoyer 944de78b5d switch_root: umount mount points we cannot move with MNT_DETACH
If a mount point cannot be moved to the new root, umount it with
MNT_DETACH, so that it is lazy umounted and does not show up in
/proc/mounts anymore.

[kzak@redhat.com: - add MNT_DETACH macro fallback]

Signed-off-by: Harald Hoyer <harald@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-11-14 14:49:37 +01:00
Sami Kerola 0fbd4c8572 switch_root: add nls support
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2011-10-30 14:47:31 +01:00
Sami Kerola ed8d2938f3 switch_root: align with howto-usage-function.txt
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2011-10-30 14:45:14 +01:00
Harald Hoyer acb03ad46a switch_root: handle /run and do not mount over existing mounts
Handle /run just like /dev, /sys and /proc

Do not mount move, if there is already something mounted on the
destination folder.

Signed-off-by: Harald Hoyer <harald@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2011-10-20 12:27:35 +02:00
Fabian Groffen eb76ca98b0 build-sys: provide alternatives for err, errx, warn and warnx
Solaris lacks err, errx, warn and warnx.  This also means the err.h header
doesn't exist.  Removed err.h include from all files, and included err.h from
c.h instead if it exists, otherwise alternatives are provided.

Signed-off-by: Fabian Groffen <grobian@gentoo.org>
2011-02-14 17:45:24 +01:00
Karel Zak 558417e8c8 Revert "switch_root: add subroot support"
This reverts commit a692a87459.

On Wed, Nov 18, 2009 at 03:33:12PM +0000, Daniel Drake wrote:
> Booting into a system this way just leads to problems because
> you cannot remount the root read-only at shutdown (leading to unclean
> shutdowns).

> Miklos Szeredi pointed out a trick to turn any directory into a
> mount point which avoids this problem. Therefore we can simplify
> switch_root again and simply document that its users should set
2009-11-20 15:11:59 +01:00
Karel Zak 532c6cb199 switch_root: remove TIOCSCTTY and setsid()
requested by dracut developers because:

   bash: cannot set terminal process group (-1): Inappropriate ioctl for device

Addresses-Red-Hat-Bug: #519237
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-10-09 15:17:54 +02:00
Daniel Drake a692a87459 switch_root: add subroot support
The current switch_root can only switch to a new root that is the root
of a mount point.

This patch adds support for "subroots", where the new root is
somewhere below a mount point. It does this by adding in a few extra
steps to chroot into the subroot after the enclosing partition has
been moved and entered.

This will be used by OLPC, who sort-of have 2 copies of Fedora stored
on a single partition under different directory trees, where the
initramfs decides which one to boot into

[kzak@redhat.com:
    - port to the current u-l-ng switch_root code
    - don't use static buffer for "dir" in get_parent_mount()]

CC: Peter Jones <pjones@redhat.com>
Signed-off-by: Daniel Drake <dsd@laptop.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-07-14 14:41:33 +02:00
Peter Jones 2a7ccc65e9 switch_root: do recursiveRemove after our root is moved to avoid races.
This way there's no race between unlinking the /newroot directory and
the MS_MOVE/chroot() to get away from it.

Signed-off-by: Peter Jones <pjones@redhat.com>
2009-06-22 21:45:25 +02:00
Peter Jones 4c2d96e62a switch_root: fork before cleaning up the filesystem.
Fork before cleaning up the old filesystem, so it becomes asyncronous,
which results in a faster boot time.

Signed-off-by: Peter Jones <pjones@redhat.com>
2009-06-22 21:45:25 +02:00
Peter Jones 82476a9080 switch_root: use file descriptor instead of path for recursiveRemove()
This makes recursiveRemove() use fdopendir() instead of taking a path,
so we're always sure about which namespace we're starting from.

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:38:12 +02:00
Karel Zak 8f24e52ed7 switch_root: use snprintf() rather tan str{cpy,cat}()
Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:47 +02:00
Karel Zak 8b6457d06a switch_root: clean up argv[] usage, add -h and -V
Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:47 +02:00
Karel Zak fcb495b1df switch_root: use err.h, clean up return codes
Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:47 +02:00
Karel Zak a6fc8b07f9 switch_root: rewrite to use fstatat() and unlinkat()
Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:47 +02:00
Karel Zak 3ddbe4d26b switch_root: fix coding style
Signed-off-by: Peter Jones <pjones@redhat.com>
Acked-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:47 +02:00
Karel Zak 711ea7307d switch_root: new command
Copied from Dracut project:
git://dracut.git.sourceforge.net/gitroot/dracut

switch_root history in dracut.git repository:

$ git shortlog switch_root.c
Harald Hoyer (5):
      replace switch_root shell script with binary
      add \n to switch_root
      use switch_root code from http://pjones.fedorapeople.org/mkstart/usr/lib/mkstart/switchroot.c
      mount move instead of umount and fix the search for fallback inits
      setsid() and set controlling terminal for real /sbin/init

Victor Lowther (2):
      Simplify switch_root.c a bit
      Remove all files on the initramfs before switching root

Signed-off-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2009-06-22 21:30:46 +02:00