| 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> |
| 10 | #include <linux/fs_struct.h> |
| 11 | #include <linux/file.h> |
| 12 | #include <linux/init_syscalls.h> |
| 13 | #include <linux/security.h> |
| 14 | #include "internal.h" |
| 15 | |
| 16 | int __init init_mount(const char *dev_name, const char *dir_name, |
| 17 | const char *type_page, unsigned long flags, void *data_page) |
| 18 | { |
| 19 | struct path path; |
| 20 | int ret; |
| 21 | |
| 22 | ret = kern_path(dir_name, LOOKUP_FOLLOW, &path); |
| 23 | if (ret) |
| 24 | return ret; |
| 25 | ret = path_mount(dev_name, path: &path, type_page, flags, data_page); |
| 26 | path_put(&path); |
| 27 | return ret; |
| 28 | } |
| 29 | |
| 30 | int __init init_umount(const char *name, int flags) |
| 31 | { |
| 32 | int lookup_flags = LOOKUP_MOUNTPOINT; |
| 33 | struct path path; |
| 34 | int ret; |
| 35 | |
| 36 | if (!(flags & UMOUNT_NOFOLLOW)) |
| 37 | lookup_flags |= LOOKUP_FOLLOW; |
| 38 | ret = kern_path(name, lookup_flags, &path); |
| 39 | if (ret) |
| 40 | return ret; |
| 41 | return path_umount(path: &path, flags); |
| 42 | } |
| 43 | |
| 44 | int __init init_chdir(const char *filename) |
| 45 | { |
| 46 | struct path path; |
| 47 | int error; |
| 48 | |
| 49 | error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); |
| 50 | if (error) |
| 51 | return error; |
| 52 | error = path_permission(path: &path, MAY_EXEC | MAY_CHDIR); |
| 53 | if (!error) |
| 54 | set_fs_pwd(current->fs, &path); |
| 55 | path_put(&path); |
| 56 | return error; |
| 57 | } |
| 58 | |
| 59 | int __init init_chroot(const char *filename) |
| 60 | { |
| 61 | struct path path; |
| 62 | int error; |
| 63 | |
| 64 | error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); |
| 65 | if (error) |
| 66 | return error; |
| 67 | error = path_permission(path: &path, MAY_EXEC | MAY_CHDIR); |
| 68 | if (error) |
| 69 | goto dput_and_out; |
| 70 | error = -EPERM; |
| 71 | if (!ns_capable(ns: current_user_ns(), CAP_SYS_CHROOT)) |
| 72 | goto dput_and_out; |
| 73 | error = security_path_chroot(path: &path); |
| 74 | if (error) |
| 75 | goto dput_and_out; |
| 76 | set_fs_root(current->fs, &path); |
| 77 | dput_and_out: |
| 78 | path_put(&path); |
| 79 | return error; |
| 80 | } |
| 81 | |
| 82 | int __init init_chown(const char *filename, uid_t user, gid_t group, int flags) |
| 83 | { |
| 84 | int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
| 85 | struct path path; |
| 86 | int error; |
| 87 | |
| 88 | error = kern_path(filename, lookup_flags, &path); |
| 89 | if (error) |
| 90 | return error; |
| 91 | error = mnt_want_write(mnt: path.mnt); |
| 92 | if (!error) { |
| 93 | error = chown_common(path: &path, user, group); |
| 94 | mnt_drop_write(mnt: path.mnt); |
| 95 | } |
| 96 | path_put(&path); |
| 97 | return error; |
| 98 | } |
| 99 | |
| 100 | int __init init_chmod(const char *filename, umode_t mode) |
| 101 | { |
| 102 | struct path path; |
| 103 | int error; |
| 104 | |
| 105 | error = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 106 | if (error) |
| 107 | return error; |
| 108 | error = chmod_common(path: &path, mode); |
| 109 | path_put(&path); |
| 110 | return error; |
| 111 | } |
| 112 | |
| 113 | int __init init_eaccess(const char *filename) |
| 114 | { |
| 115 | struct path path; |
| 116 | int error; |
| 117 | |
| 118 | error = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 119 | if (error) |
| 120 | return error; |
| 121 | error = path_permission(path: &path, MAY_ACCESS); |
| 122 | path_put(&path); |
| 123 | return error; |
| 124 | } |
| 125 | |
| 126 | int __init init_stat(const char *filename, struct kstat *stat, int flags) |
| 127 | { |
| 128 | int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
| 129 | struct path path; |
| 130 | int error; |
| 131 | |
| 132 | error = kern_path(filename, lookup_flags, &path); |
| 133 | if (error) |
| 134 | return error; |
| 135 | error = vfs_getattr(&path, stat, STATX_BASIC_STATS, |
| 136 | flags | AT_NO_AUTOMOUNT); |
| 137 | path_put(&path); |
| 138 | return error; |
| 139 | } |
| 140 | |
| 141 | int __init init_mknod(const char *filename, umode_t mode, unsigned int dev) |
| 142 | { |
| 143 | struct dentry *dentry; |
| 144 | struct path path; |
| 145 | int error; |
| 146 | |
| 147 | if (S_ISFIFO(mode) || S_ISSOCK(mode)) |
| 148 | dev = 0; |
| 149 | else if (!(S_ISBLK(mode) || S_ISCHR(mode))) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | dentry = start_creating_path(AT_FDCWD, filename, &path, 0); |
| 153 | if (IS_ERR(ptr: dentry)) |
| 154 | return PTR_ERR(ptr: dentry); |
| 155 | |
| 156 | mode = mode_strip_umask(dir: d_inode(dentry: path.dentry), mode); |
| 157 | error = security_path_mknod(dir: &path, dentry, mode, dev); |
| 158 | if (!error) |
| 159 | error = vfs_mknod(mnt_idmap(mnt: path.mnt), path.dentry->d_inode, |
| 160 | dentry, mode, new_decode_dev(dev)); |
| 161 | end_creating_path(&path, dentry); |
| 162 | return error; |
| 163 | } |
| 164 | |
| 165 | int __init init_link(const char *oldname, const char *newname) |
| 166 | { |
| 167 | struct dentry *new_dentry; |
| 168 | struct path old_path, new_path; |
| 169 | struct mnt_idmap *idmap; |
| 170 | int error; |
| 171 | |
| 172 | error = kern_path(oldname, 0, &old_path); |
| 173 | if (error) |
| 174 | return error; |
| 175 | |
| 176 | new_dentry = start_creating_path(AT_FDCWD, newname, &new_path, 0); |
| 177 | error = PTR_ERR(ptr: new_dentry); |
| 178 | if (IS_ERR(ptr: new_dentry)) |
| 179 | goto out; |
| 180 | |
| 181 | error = -EXDEV; |
| 182 | if (old_path.mnt != new_path.mnt) |
| 183 | goto out_dput; |
| 184 | idmap = mnt_idmap(mnt: new_path.mnt); |
| 185 | error = may_linkat(idmap, link: &old_path); |
| 186 | if (unlikely(error)) |
| 187 | goto out_dput; |
| 188 | error = security_path_link(old_dentry: old_path.dentry, new_dir: &new_path, new_dentry); |
| 189 | if (error) |
| 190 | goto out_dput; |
| 191 | error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode, |
| 192 | new_dentry, NULL); |
| 193 | out_dput: |
| 194 | end_creating_path(&new_path, new_dentry); |
| 195 | out: |
| 196 | path_put(&old_path); |
| 197 | return error; |
| 198 | } |
| 199 | |
| 200 | int __init init_symlink(const char *oldname, const char *newname) |
| 201 | { |
| 202 | struct dentry *dentry; |
| 203 | struct path path; |
| 204 | int error; |
| 205 | |
| 206 | dentry = start_creating_path(AT_FDCWD, newname, &path, 0); |
| 207 | if (IS_ERR(ptr: dentry)) |
| 208 | return PTR_ERR(ptr: dentry); |
| 209 | error = security_path_symlink(dir: &path, dentry, old_name: oldname); |
| 210 | if (!error) |
| 211 | error = vfs_symlink(mnt_idmap(mnt: path.mnt), path.dentry->d_inode, |
| 212 | dentry, oldname); |
| 213 | end_creating_path(&path, dentry); |
| 214 | return error; |
| 215 | } |
| 216 | |
| 217 | int __init init_unlink(const char *pathname) |
| 218 | { |
| 219 | return do_unlinkat(AT_FDCWD, name: getname_kernel(pathname)); |
| 220 | } |
| 221 | |
| 222 | int __init init_mkdir(const char *pathname, umode_t mode) |
| 223 | { |
| 224 | struct dentry *dentry; |
| 225 | struct path path; |
| 226 | int error; |
| 227 | |
| 228 | dentry = start_creating_path(AT_FDCWD, pathname, &path, |
| 229 | LOOKUP_DIRECTORY); |
| 230 | if (IS_ERR(ptr: dentry)) |
| 231 | return PTR_ERR(ptr: dentry); |
| 232 | mode = mode_strip_umask(dir: d_inode(dentry: path.dentry), mode); |
| 233 | error = security_path_mkdir(dir: &path, dentry, mode); |
| 234 | if (!error) { |
| 235 | dentry = vfs_mkdir(mnt_idmap(mnt: path.mnt), path.dentry->d_inode, |
| 236 | dentry, mode); |
| 237 | if (IS_ERR(ptr: dentry)) |
| 238 | error = PTR_ERR(ptr: dentry); |
| 239 | } |
| 240 | end_creating_path(&path, dentry); |
| 241 | return error; |
| 242 | } |
| 243 | |
| 244 | int __init init_rmdir(const char *pathname) |
| 245 | { |
| 246 | return do_rmdir(AT_FDCWD, name: getname_kernel(pathname)); |
| 247 | } |
| 248 | |
| 249 | int __init init_utimes(char *filename, struct timespec64 *ts) |
| 250 | { |
| 251 | struct path path; |
| 252 | int error; |
| 253 | |
| 254 | error = kern_path(filename, 0, &path); |
| 255 | if (error) |
| 256 | return error; |
| 257 | error = vfs_utimes(path: &path, times: ts); |
| 258 | path_put(&path); |
| 259 | return error; |
| 260 | } |
| 261 | |
| 262 | int __init init_dup(struct file *file) |
| 263 | { |
| 264 | int fd; |
| 265 | |
| 266 | fd = get_unused_fd_flags(flags: 0); |
| 267 | if (fd < 0) |
| 268 | return fd; |
| 269 | fd_install(fd, file: get_file(f: file)); |
| 270 | return 0; |
| 271 | } |
| 272 | |