Skip to content

Commit 4b7ca50

Browse files
author
Christoph Hellwig
committed
init: add an init_chroot helper
Add a simple helper to chroot with a kernel space file name and switch the early init code over to it. Remove the now unused ksys_chroot. Signed-off-by: Christoph Hellwig <[email protected]>
1 parent db63f1e commit 4b7ca50

File tree

7 files changed

+30
-12
lines changed

7 files changed

+30
-12
lines changed

drivers/base/devtmpfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static int __init devtmpfs_setup(void *p)
413413
if (err)
414414
goto out;
415415
init_chdir("/.."); /* will traverse into overmounted root */
416-
ksys_chroot(".");
416+
init_chroot(".");
417417
out:
418418
*(int *)p = err;
419419
complete(&setup_done);

fs/init.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/fs.h>
1010
#include <linux/fs_struct.h>
1111
#include <linux/init_syscalls.h>
12+
#include <linux/security.h>
1213
#include "internal.h"
1314

1415
int __init init_mount(const char *dev_name, const char *dir_name,
@@ -54,6 +55,29 @@ int __init init_chdir(const char *filename)
5455
return error;
5556
}
5657

58+
int __init init_chroot(const char *filename)
59+
{
60+
struct path path;
61+
int error;
62+
63+
error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
64+
if (error)
65+
return error;
66+
error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
67+
if (error)
68+
goto dput_and_out;
69+
error = -EPERM;
70+
if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
71+
goto dput_and_out;
72+
error = security_path_chroot(&path);
73+
if (error)
74+
goto dput_and_out;
75+
set_fs_root(current->fs, &path);
76+
dput_and_out:
77+
path_put(&path);
78+
return error;
79+
}
80+
5781
int __init init_unlink(const char *pathname)
5882
{
5983
return do_unlinkat(AT_FDCWD, getname_kernel(pathname));

fs/open.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ SYSCALL_DEFINE1(fchdir, unsigned int, fd)
530530
return error;
531531
}
532532

533-
int ksys_chroot(const char __user *filename)
533+
SYSCALL_DEFINE1(chroot, const char __user *, filename)
534534
{
535535
struct path path;
536536
int error;
@@ -563,11 +563,6 @@ int ksys_chroot(const char __user *filename)
563563
return error;
564564
}
565565

566-
SYSCALL_DEFINE1(chroot, const char __user *, filename)
567-
{
568-
return ksys_chroot(filename);
569-
}
570-
571566
static int chmod_common(const struct path *path, umode_t mode)
572567
{
573568
struct inode *inode = path->dentry->d_inode;

include/linux/init_syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ int __init init_mount(const char *dev_name, const char *dir_name,
44
const char *type_page, unsigned long flags, void *data_page);
55
int __init init_umount(const char *name, int flags);
66
int __init init_chdir(const char *filename);
7+
int __init init_chroot(const char *filename);
78
int __init init_unlink(const char *pathname);
89
int __init init_rmdir(const char *pathname);

include/linux/syscalls.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,6 @@ asmlinkage long sys_ni_syscall(void);
12351235
* Instead, use one of the functions which work equivalently, such as
12361236
* the ksys_xyzyyz() functions prototyped below.
12371237
*/
1238-
1239-
int ksys_chroot(const char __user *filename);
12401238
ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count);
12411239
int ksys_fchown(unsigned int fd, uid_t user, gid_t group);
12421240
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count);

init/do_mounts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void __init prepare_namespace(void)
629629
out:
630630
devtmpfs_mount();
631631
init_mount(".", "/", NULL, MS_MOVE, NULL);
632-
ksys_chroot(".");
632+
init_chroot(".");
633633
}
634634

635635
static bool is_tmpfs;

init/do_mounts_initrd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
6363
/* move initrd over / and chdir/chroot in initrd root */
6464
init_chdir("/root");
6565
init_mount(".", "/", NULL, MS_MOVE, NULL);
66-
ksys_chroot(".");
66+
init_chroot(".");
6767
ksys_setsid();
6868
return 0;
6969
}
@@ -101,7 +101,7 @@ static void __init handle_initrd(void)
101101
/* move initrd to rootfs' /old */
102102
init_mount("..", ".", NULL, MS_MOVE, NULL);
103103
/* switch root and cwd back to / of rootfs */
104-
ksys_chroot("..");
104+
init_chroot("..");
105105

106106
if (new_decode_dev(real_root_dev) == Root_RAM0) {
107107
init_chdir("/old");

0 commit comments

Comments
 (0)