blob: 5c36adaa9b448d6afcb7fb49a704cf131081f99b [file] [log] [blame]
Christoph Hellwigc60166f2020-07-21 11:12:08 +02001// 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 Hellwigdb63f1e2020-07-22 11:25:21 +020010#include <linux/fs_struct.h>
Christoph Hellwigf0735312020-07-28 17:49:47 +020011#include <linux/file.h>
Christoph Hellwigc60166f2020-07-21 11:12:08 +020012#include <linux/init_syscalls.h>
Christoph Hellwig4b7ca502020-07-22 11:26:13 +020013#include <linux/security.h>
Christoph Hellwigc60166f2020-07-21 11:12:08 +020014#include "internal.h"
15
16int __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, type_page, flags, data_page);
26 path_put(&path);
27 return ret;
28}
Christoph Hellwig09267de2020-07-23 08:23:08 +020029
30int __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, flags);
42}
Christoph Hellwig8fb9f732020-07-23 08:23:40 +020043
Christoph Hellwigdb63f1e2020-07-22 11:25:21 +020044int __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;
Christian Brauner02f92b32021-01-21 14:19:22 +010052 error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
Christoph Hellwigdb63f1e2020-07-22 11:25:21 +020053 if (!error)
54 set_fs_pwd(current->fs, &path);
55 path_put(&path);
56 return error;
57}
58
Christoph Hellwig4b7ca502020-07-22 11:26:13 +020059int __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;
Christian Brauner02f92b32021-01-21 14:19:22 +010067 error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
Christoph Hellwig4b7ca502020-07-22 11:26:13 +020068 if (error)
69 goto dput_and_out;
70 error = -EPERM;
71 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
72 goto dput_and_out;
73 error = security_path_chroot(&path);
74 if (error)
75 goto dput_and_out;
76 set_fs_root(current->fs, &path);
77dput_and_out:
78 path_put(&path);
79 return error;
80}
81
Christoph Hellwigb8734982020-07-22 11:13:26 +020082int __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(path.mnt);
92 if (!error) {
93 error = chown_common(&path, user, group);
94 mnt_drop_write(path.mnt);
95 }
96 path_put(&path);
97 return error;
98}
99
Christoph Hellwig10977422020-07-22 11:41:02 +0200100int __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, mode);
109 path_put(&path);
110 return error;
111}
112
Christoph Hellwigeb9d7d32020-07-22 11:14:02 +0200113int __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;
Christian Brauner02f92b32021-01-21 14:19:22 +0100121 error = path_permission(&path, MAY_ACCESS);
Christoph Hellwigeb9d7d32020-07-22 11:14:02 +0200122 path_put(&path);
123 return error;
124}
125
Christoph Hellwig716308a2020-07-22 11:15:40 +0200126int __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
Christoph Hellwig5fee64f2020-07-22 11:41:20 +0200141int __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 = kern_path_create(AT_FDCWD, filename, &path, 0);
153 if (IS_ERR(dentry))
154 return PTR_ERR(dentry);
155
156 if (!IS_POSIXACL(path.dentry->d_inode))
157 mode &= ~current_umask();
158 error = security_path_mknod(&path, dentry, mode, dev);
159 if (!error)
Christian Braunerb816dd52021-01-21 14:19:39 +0100160 error = vfs_mknod(mnt_user_ns(path.mnt), path.dentry->d_inode,
161 dentry, mode, new_decode_dev(dev));
Christoph Hellwig5fee64f2020-07-22 11:41:20 +0200162 done_path_create(&path, dentry);
163 return error;
164}
165
Christoph Hellwig812931d2020-07-22 11:14:19 +0200166int __init init_link(const char *oldname, const char *newname)
167{
168 struct dentry *new_dentry;
169 struct path old_path, new_path;
Christian Braunerb816dd52021-01-21 14:19:39 +0100170 struct user_namespace *mnt_userns;
Christoph Hellwig812931d2020-07-22 11:14:19 +0200171 int error;
172
173 error = kern_path(oldname, 0, &old_path);
174 if (error)
175 return error;
176
177 new_dentry = kern_path_create(AT_FDCWD, newname, &new_path, 0);
178 error = PTR_ERR(new_dentry);
179 if (IS_ERR(new_dentry))
180 goto out;
181
182 error = -EXDEV;
183 if (old_path.mnt != new_path.mnt)
184 goto out_dput;
Christian Braunerb816dd52021-01-21 14:19:39 +0100185 mnt_userns = mnt_user_ns(new_path.mnt);
186 error = may_linkat(mnt_userns, &old_path);
Christoph Hellwig812931d2020-07-22 11:14:19 +0200187 if (unlikely(error))
188 goto out_dput;
189 error = security_path_link(old_path.dentry, &new_path, new_dentry);
190 if (error)
191 goto out_dput;
Christian Braunerb816dd52021-01-21 14:19:39 +0100192 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
193 new_dentry, NULL);
Christoph Hellwig812931d2020-07-22 11:14:19 +0200194out_dput:
195 done_path_create(&new_path, new_dentry);
196out:
197 path_put(&old_path);
198 return error;
199}
200
Christoph Hellwigcd3acb62020-07-22 11:14:36 +0200201int __init init_symlink(const char *oldname, const char *newname)
202{
203 struct dentry *dentry;
204 struct path path;
205 int error;
206
207 dentry = kern_path_create(AT_FDCWD, newname, &path, 0);
208 if (IS_ERR(dentry))
209 return PTR_ERR(dentry);
210 error = security_path_symlink(&path, dentry, oldname);
211 if (!error)
Christian Braunerb816dd52021-01-21 14:19:39 +0100212 error = vfs_symlink(mnt_user_ns(path.mnt), path.dentry->d_inode,
213 dentry, oldname);
Christoph Hellwigcd3acb62020-07-22 11:14:36 +0200214 done_path_create(&path, dentry);
215 return error;
216}
217
Christoph Hellwig8fb9f732020-07-23 08:23:40 +0200218int __init init_unlink(const char *pathname)
219{
220 return do_unlinkat(AT_FDCWD, getname_kernel(pathname));
221}
Christoph Hellwig20cce022020-07-22 11:11:45 +0200222
Christoph Hellwig83ff98c2020-07-22 11:14:59 +0200223int __init init_mkdir(const char *pathname, umode_t mode)
224{
225 struct dentry *dentry;
226 struct path path;
227 int error;
228
229 dentry = kern_path_create(AT_FDCWD, pathname, &path, LOOKUP_DIRECTORY);
230 if (IS_ERR(dentry))
231 return PTR_ERR(dentry);
232 if (!IS_POSIXACL(path.dentry->d_inode))
233 mode &= ~current_umask();
234 error = security_path_mkdir(&path, dentry, mode);
235 if (!error)
Christian Braunerb816dd52021-01-21 14:19:39 +0100236 error = vfs_mkdir(mnt_user_ns(path.mnt), path.dentry->d_inode,
237 dentry, mode);
Christoph Hellwig83ff98c2020-07-22 11:14:59 +0200238 done_path_create(&path, dentry);
239 return error;
240}
241
Christoph Hellwig20cce022020-07-22 11:11:45 +0200242int __init init_rmdir(const char *pathname)
243{
244 return do_rmdir(AT_FDCWD, getname_kernel(pathname));
245}
Christoph Hellwig235e5792020-07-21 16:05:31 +0200246
247int __init init_utimes(char *filename, struct timespec64 *ts)
248{
249 struct path path;
250 int error;
251
252 error = kern_path(filename, 0, &path);
253 if (error)
254 return error;
255 error = vfs_utimes(&path, ts);
256 path_put(&path);
257 return error;
258}
Christoph Hellwigf0735312020-07-28 17:49:47 +0200259
260int __init init_dup(struct file *file)
261{
262 int fd;
263
264 fd = get_unused_fd_flags(0);
265 if (fd < 0)
266 return fd;
267 fd_install(fd, get_file(file));
268 return 0;
269}