Christoph Hellwig | c60166f | 2020-07-21 11:12:08 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Routines that mimic syscalls, but don't use the user address space or file |
| 4 | * descriptors. Only for init/ and related early init code. |
| 5 | */ |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/mount.h> |
| 8 | #include <linux/namei.h> |
| 9 | #include <linux/fs.h> |
Christoph Hellwig | db63f1e | 2020-07-22 11:25:21 +0200 | [diff] [blame] | 10 | #include <linux/fs_struct.h> |
Christoph Hellwig | c60166f | 2020-07-21 11:12:08 +0200 | [diff] [blame] | 11 | #include <linux/init_syscalls.h> |
Christoph Hellwig | 4b7ca50 | 2020-07-22 11:26:13 +0200 | [diff] [blame^] | 12 | #include <linux/security.h> |
Christoph Hellwig | c60166f | 2020-07-21 11:12:08 +0200 | [diff] [blame] | 13 | #include "internal.h" |
| 14 | |
| 15 | int __init init_mount(const char *dev_name, const char *dir_name, |
| 16 | const char *type_page, unsigned long flags, void *data_page) |
| 17 | { |
| 18 | struct path path; |
| 19 | int ret; |
| 20 | |
| 21 | ret = kern_path(dir_name, LOOKUP_FOLLOW, &path); |
| 22 | if (ret) |
| 23 | return ret; |
| 24 | ret = path_mount(dev_name, &path, type_page, flags, data_page); |
| 25 | path_put(&path); |
| 26 | return ret; |
| 27 | } |
Christoph Hellwig | 09267de | 2020-07-23 08:23:08 +0200 | [diff] [blame] | 28 | |
| 29 | int __init init_umount(const char *name, int flags) |
| 30 | { |
| 31 | int lookup_flags = LOOKUP_MOUNTPOINT; |
| 32 | struct path path; |
| 33 | int ret; |
| 34 | |
| 35 | if (!(flags & UMOUNT_NOFOLLOW)) |
| 36 | lookup_flags |= LOOKUP_FOLLOW; |
| 37 | ret = kern_path(name, lookup_flags, &path); |
| 38 | if (ret) |
| 39 | return ret; |
| 40 | return path_umount(&path, flags); |
| 41 | } |
Christoph Hellwig | 8fb9f73 | 2020-07-23 08:23:40 +0200 | [diff] [blame] | 42 | |
Christoph Hellwig | db63f1e | 2020-07-22 11:25:21 +0200 | [diff] [blame] | 43 | int __init init_chdir(const char *filename) |
| 44 | { |
| 45 | struct path path; |
| 46 | int error; |
| 47 | |
| 48 | error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); |
| 49 | if (error) |
| 50 | return error; |
| 51 | error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
| 52 | if (!error) |
| 53 | set_fs_pwd(current->fs, &path); |
| 54 | path_put(&path); |
| 55 | return error; |
| 56 | } |
| 57 | |
Christoph Hellwig | 4b7ca50 | 2020-07-22 11:26:13 +0200 | [diff] [blame^] | 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 | |
Christoph Hellwig | 8fb9f73 | 2020-07-23 08:23:40 +0200 | [diff] [blame] | 81 | int __init init_unlink(const char *pathname) |
| 82 | { |
| 83 | return do_unlinkat(AT_FDCWD, getname_kernel(pathname)); |
| 84 | } |
Christoph Hellwig | 20cce02 | 2020-07-22 11:11:45 +0200 | [diff] [blame] | 85 | |
| 86 | int __init init_rmdir(const char *pathname) |
| 87 | { |
| 88 | return do_rmdir(AT_FDCWD, getname_kernel(pathname)); |
| 89 | } |