History

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.

Christoph Hellwig committed 6 years ago

Commit 4b7ca501 parent db63f1e7 files · +30 −12Browse files

Unified · Split

7 files
  1. drivers/base/devtmpfs.c +1 −1
  2. fs/init.c +24 −0
  3. fs/open.c +1 −6
  4. include/linux/init_syscalls.h +1 −0
  5. include/linux/syscalls.h +0 −2
  6. init/do_mounts.c +1 −1
  7. init/do_mounts_initrd.c +2 −2
drivers/base/devtmpfs.c+1−1
@@ -413,7 +413,7 @@ static int __init devtmpfs_setup(void *p) · 412 unmodified lines
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+24−0
@@ -9,6 +9,7 @@ · 8 unmodified lines
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) · 39 unmodified lines
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+1−6
@@ -530,7 +530,7 @@ out: · 529 unmodified lines
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 @@ out: · 26 unmodified lines
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+1−0
@@ -4,5 +4,6 @@ int __init init_mount(const char *dev_name, const char *dir_name, · 3 unmodified lines
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+0−2
@@ -1235,8 +1235,6 @@ asmlinkage long sys_ni_syscall(void); · 1234 unmodified lines
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+1−1
@@ -629,7 +629,7 @@ void __init prepare_namespace(void) · 628 unmodified lines
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+2−2
@@ -63,7 +63,7 @@ static int __init init_linuxrc(struct subprocess_info *info, struct cred *new) · 62 unmodified lines
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) · 31 unmodified lines
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");

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t