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.
Unified · Split
| @@ -413,7 +413,7 @@ static int __init devtmpfs_setup(void *p) · 412 unmodified lines | ||
| 413 | 413 | if (err) |
| 414 | 414 | goto out; |
| 415 | 415 | init_chdir("/.."); /* will traverse into overmounted root */ |
| 416 | − ksys_chroot("."); | |
| 416 | + init_chroot("."); | |
| 417 | 417 | out: |
| 418 | 418 | *(int *)p = err; |
| 419 | 419 | complete(&setup_done); |
| @@ -9,6 +9,7 @@ · 8 unmodified lines | ||
| 9 | 9 | #include <linux/fs.h> |
| 10 | 10 | #include <linux/fs_struct.h> |
| 11 | 11 | #include <linux/init_syscalls.h> |
| 12 | +#include <linux/security.h> | |
| 12 | 13 | #include "internal.h" |
| 13 | 14 | |
| 14 | 15 | 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 | ||
| 54 | 55 | return error; |
| 55 | 56 | } |
| 56 | 57 | |
| 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 | + | |
| 57 | 81 | int __init init_unlink(const char *pathname) |
| 58 | 82 | { |
| 59 | 83 | return do_unlinkat(AT_FDCWD, getname_kernel(pathname)); |
| @@ -530,7 +530,7 @@ out: · 529 unmodified lines | ||
| 530 | 530 | return error; |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | −int ksys_chroot(const char __user *filename) | |
| 533 | +SYSCALL_DEFINE1(chroot, const char __user *, filename) | |
| 534 | 534 | { |
| 535 | 535 | struct path path; |
| 536 | 536 | int error; |
| @@ -563,11 +563,6 @@ out: · 26 unmodified lines | ||
| 563 | 563 | return error; |
| 564 | 564 | } |
| 565 | 565 | |
| 566 | −SYSCALL_DEFINE1(chroot, const char __user *, filename) | |
| 567 | −{ | |
| 568 | − return ksys_chroot(filename); | |
| 569 | −} | |
| 570 | − | |
| 571 | 566 | static int chmod_common(const struct path *path, umode_t mode) |
| 572 | 567 | { |
| 573 | 568 | struct inode *inode = path->dentry->d_inode; |
| @@ -4,5 +4,6 @@ int __init init_mount(const char *dev_name, const char *dir_name, · 3 unmodified lines | ||
| 4 | 4 | const char *type_page, unsigned long flags, void *data_page); |
| 5 | 5 | int __init init_umount(const char *name, int flags); |
| 6 | 6 | int __init init_chdir(const char *filename); |
| 7 | +int __init init_chroot(const char *filename); | |
| 7 | 8 | int __init init_unlink(const char *pathname); |
| 8 | 9 | int __init init_rmdir(const char *pathname); |
| @@ -1235,8 +1235,6 @@ asmlinkage long sys_ni_syscall(void); · 1234 unmodified lines | ||
| 1235 | 1235 | * Instead, use one of the functions which work equivalently, such as |
| 1236 | 1236 | * the ksys_xyzyyz() functions prototyped below. |
| 1237 | 1237 | */ |
| 1238 | − | |
| 1239 | −int ksys_chroot(const char __user *filename); | |
| 1240 | 1238 | ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count); |
| 1241 | 1239 | int ksys_fchown(unsigned int fd, uid_t user, gid_t group); |
| 1242 | 1240 | ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count); |
| @@ -629,7 +629,7 @@ void __init prepare_namespace(void) · 628 unmodified lines | ||
| 629 | 629 | out: |
| 630 | 630 | devtmpfs_mount(); |
| 631 | 631 | init_mount(".", "/", NULL, MS_MOVE, NULL); |
| 632 | − ksys_chroot("."); | |
| 632 | + init_chroot("."); | |
| 633 | 633 | } |
| 634 | 634 | |
| 635 | 635 | static bool is_tmpfs; |
| @@ -63,7 +63,7 @@ static int __init init_linuxrc(struct subprocess_info *info, struct cred *new) · 62 unmodified lines | ||
| 63 | 63 | /* move initrd over / and chdir/chroot in initrd root */ |
| 64 | 64 | init_chdir("/root"); |
| 65 | 65 | init_mount(".", "/", NULL, MS_MOVE, NULL); |
| 66 | − ksys_chroot("."); | |
| 66 | + init_chroot("."); | |
| 67 | 67 | ksys_setsid(); |
| 68 | 68 | return 0; |
| 69 | 69 | } |
| @@ -101,7 +101,7 @@ static void __init handle_initrd(void) · 31 unmodified lines | ||
| 101 | 101 | /* move initrd to rootfs' /old */ |
| 102 | 102 | init_mount("..", ".", NULL, MS_MOVE, NULL); |
| 103 | 103 | /* switch root and cwd back to / of rootfs */ |
| 104 | − ksys_chroot(".."); | |
| 104 | + init_chroot(".."); | |
| 105 | 105 | |
| 106 | 106 | if (new_decode_dev(real_root_dev) == Root_RAM0) { |
| 107 | 107 | init_chdir("/old"); |