blob: 0ba34c1355932401d44ad7007679e82fba2abb3a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Viro7ed1ee62010-03-23 10:37:36 -04002#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05003#include <linux/export.h>
Al Viro7ed1ee62010-03-23 10:37:36 -04004#include <linux/fs.h>
5#include <linux/file.h>
Christoph Hellwig365b1812010-07-07 18:53:25 +02006#include <linux/mount.h>
Al Viro7ed1ee62010-03-23 10:37:36 -04007#include <linux/namei.h>
8#include <linux/statfs.h>
9#include <linux/security.h>
10#include <linux/uaccess.h>
Al Viro4ada54e2017-04-08 18:08:15 -040011#include <linux/compat.h>
Al Virocf31e702012-01-02 22:28:36 -050012#include "internal.h"
Al Viro7ed1ee62010-03-23 10:37:36 -040013
Christoph Hellwig365b1812010-07-07 18:53:25 +020014static int flags_by_mnt(int mnt_flags)
15{
16 int flags = 0;
17
18 if (mnt_flags & MNT_READONLY)
19 flags |= ST_RDONLY;
20 if (mnt_flags & MNT_NOSUID)
21 flags |= ST_NOSUID;
22 if (mnt_flags & MNT_NODEV)
23 flags |= ST_NODEV;
24 if (mnt_flags & MNT_NOEXEC)
25 flags |= ST_NOEXEC;
26 if (mnt_flags & MNT_NOATIME)
27 flags |= ST_NOATIME;
28 if (mnt_flags & MNT_NODIRATIME)
29 flags |= ST_NODIRATIME;
30 if (mnt_flags & MNT_RELATIME)
31 flags |= ST_RELATIME;
Mattias Nisslerdab741e02020-08-27 11:09:46 -060032 if (mnt_flags & MNT_NOSYMFOLLOW)
33 flags |= ST_NOSYMFOLLOW;
Christoph Hellwig365b1812010-07-07 18:53:25 +020034 return flags;
35}
36
37static int flags_by_sb(int s_flags)
38{
39 int flags = 0;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080040 if (s_flags & SB_SYNCHRONOUS)
Christoph Hellwig365b1812010-07-07 18:53:25 +020041 flags |= ST_SYNCHRONOUS;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080042 if (s_flags & SB_MANDLOCK)
Christoph Hellwig365b1812010-07-07 18:53:25 +020043 flags |= ST_MANDLOCK;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080044 if (s_flags & SB_RDONLY)
Carlos Maiolinoa8e2b632017-06-29 11:25:40 +020045 flags |= ST_RDONLY;
Christoph Hellwig365b1812010-07-07 18:53:25 +020046 return flags;
47}
48
49static int calculate_f_flags(struct vfsmount *mnt)
50{
51 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
52 flags_by_sb(mnt->mnt_sb->s_flags);
53}
54
Al Virocf31e702012-01-02 22:28:36 -050055static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
Al Viro7ed1ee62010-03-23 10:37:36 -040056{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020057 int retval;
Al Viro7ed1ee62010-03-23 10:37:36 -040058
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020059 if (!dentry->d_sb->s_op->statfs)
60 return -ENOSYS;
61
62 memset(buf, 0, sizeof(*buf));
63 retval = security_sb_statfs(dentry);
64 if (retval)
65 return retval;
66 retval = dentry->d_sb->s_op->statfs(dentry, buf);
67 if (retval == 0 && buf->f_frsize == 0)
68 buf->f_frsize = buf->f_bsize;
Al Viro7ed1ee62010-03-23 10:37:36 -040069 return retval;
70}
71
Amir Goldsteinec86ff52019-01-10 19:04:38 +020072int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
73{
74 struct kstatfs st;
75 int error;
76
77 error = statfs_by_dentry(dentry, &st);
78 if (error)
79 return error;
80
81 *fsid = st.f_fsid;
82 return 0;
83}
84EXPORT_SYMBOL(vfs_get_fsid);
85
Al Virof0bb5aa2016-11-20 20:27:12 -050086int vfs_statfs(const struct path *path, struct kstatfs *buf)
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020087{
Christoph Hellwig365b1812010-07-07 18:53:25 +020088 int error;
89
90 error = statfs_by_dentry(path->dentry, buf);
91 if (!error)
92 buf->f_flags = calculate_f_flags(path->mnt);
93 return error;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020094}
Al Viro7ed1ee62010-03-23 10:37:36 -040095EXPORT_SYMBOL(vfs_statfs);
96
Al Viroc8b91ac2011-03-12 10:41:39 -050097int user_statfs(const char __user *pathname, struct kstatfs *st)
Al Viro7ed1ee62010-03-23 10:37:36 -040098{
Al Viroc8b91ac2011-03-12 10:41:39 -050099 struct path path;
Jeff Layton96948fc2012-12-11 12:10:14 -0500100 int error;
101 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
102retry:
103 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -0500104 if (!error) {
105 error = vfs_statfs(&path, st);
106 path_put(&path);
Jeff Layton96948fc2012-12-11 12:10:14 -0500107 if (retry_estale(error, lookup_flags)) {
108 lookup_flags |= LOOKUP_REVAL;
109 goto retry;
110 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500111 }
112 return error;
113}
Al Viro7ed1ee62010-03-23 10:37:36 -0400114
Al Viroc8b91ac2011-03-12 10:41:39 -0500115int fd_statfs(int fd, struct kstatfs *st)
116{
Linus Torvalds9d057462013-09-30 08:35:10 -0700117 struct fd f = fdget_raw(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -0500118 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400119 if (f.file) {
120 error = vfs_statfs(&f.file->f_path, st);
121 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -0500122 }
123 return error;
124}
Al Viro7ed1ee62010-03-23 10:37:36 -0400125
Al Viroc8b91ac2011-03-12 10:41:39 -0500126static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
127{
128 struct statfs buf;
129
130 if (sizeof(buf) == sizeof(*st))
131 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400132 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500133 if (sizeof buf.f_blocks == 4) {
134 if ((st->f_blocks | st->f_bfree | st->f_bavail |
135 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400136 0xffffffff00000000ULL)
137 return -EOVERFLOW;
138 /*
139 * f_files and f_ffree may be -1; it's okay to stuff
140 * that into 32 bits
141 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500142 if (st->f_files != -1 &&
143 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400144 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500145 if (st->f_ffree != -1 &&
146 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400147 return -EOVERFLOW;
148 }
149
Al Viroc8b91ac2011-03-12 10:41:39 -0500150 buf.f_type = st->f_type;
151 buf.f_bsize = st->f_bsize;
152 buf.f_blocks = st->f_blocks;
153 buf.f_bfree = st->f_bfree;
154 buf.f_bavail = st->f_bavail;
155 buf.f_files = st->f_files;
156 buf.f_ffree = st->f_ffree;
157 buf.f_fsid = st->f_fsid;
158 buf.f_namelen = st->f_namelen;
159 buf.f_frsize = st->f_frsize;
160 buf.f_flags = st->f_flags;
161 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400162 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500163 if (copy_to_user(p, &buf, sizeof(buf)))
164 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400165 return 0;
166}
167
Al Viroc8b91ac2011-03-12 10:41:39 -0500168static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400169{
Al Viroc8b91ac2011-03-12 10:41:39 -0500170 struct statfs64 buf;
171 if (sizeof(buf) == sizeof(*st))
172 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400173 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500174 buf.f_type = st->f_type;
175 buf.f_bsize = st->f_bsize;
176 buf.f_blocks = st->f_blocks;
177 buf.f_bfree = st->f_bfree;
178 buf.f_bavail = st->f_bavail;
179 buf.f_files = st->f_files;
180 buf.f_ffree = st->f_ffree;
181 buf.f_fsid = st->f_fsid;
182 buf.f_namelen = st->f_namelen;
183 buf.f_frsize = st->f_frsize;
184 buf.f_flags = st->f_flags;
185 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400186 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500187 if (copy_to_user(p, &buf, sizeof(buf)))
188 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400189 return 0;
190}
191
192SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
193{
Al Viroc8b91ac2011-03-12 10:41:39 -0500194 struct kstatfs st;
195 int error = user_statfs(pathname, &st);
196 if (!error)
197 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400198 return error;
199}
200
201SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
202{
Al Viroc8b91ac2011-03-12 10:41:39 -0500203 struct kstatfs st;
204 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400205 if (sz != sizeof(*buf))
206 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500207 error = user_statfs(pathname, &st);
208 if (!error)
209 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400210 return error;
211}
212
213SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
214{
Al Viroc8b91ac2011-03-12 10:41:39 -0500215 struct kstatfs st;
216 int error = fd_statfs(fd, &st);
217 if (!error)
218 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400219 return error;
220}
221
222SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
223{
Al Viroc8b91ac2011-03-12 10:41:39 -0500224 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400225 int error;
226
227 if (sz != sizeof(*buf))
228 return -EINVAL;
229
Al Viroc8b91ac2011-03-12 10:41:39 -0500230 error = fd_statfs(fd, &st);
231 if (!error)
232 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400233 return error;
234}
235
Al Viro53fd88ab62017-10-14 23:00:54 -0400236static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400237{
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100238 struct super_block *s = user_get_super(dev, false);
Al Viro7ed1ee62010-03-23 10:37:36 -0400239 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400240 if (!s)
241 return -EINVAL;
242
Al Virocf31e702012-01-02 22:28:36 -0500243 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400244 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500245 return err;
246}
247
248SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
249{
250 struct ustat tmp;
251 struct kstatfs sbuf;
252 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400253 if (err)
254 return err;
255
256 memset(&tmp,0,sizeof(struct ustat));
257 tmp.f_tfree = sbuf.f_bfree;
Heiko Carstens96c0a6a2021-02-10 21:51:02 +0100258 if (IS_ENABLED(CONFIG_ARCH_32BIT_USTAT_F_TINODE))
259 tmp.f_tinode = min_t(u64, sbuf.f_ffree, UINT_MAX);
260 else
261 tmp.f_tinode = sbuf.f_ffree;
Al Viro7ed1ee62010-03-23 10:37:36 -0400262
263 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
264}
Al Viro4ada54e2017-04-08 18:08:15 -0400265
266#ifdef CONFIG_COMPAT
267static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
268{
Al Viroae2a97622017-06-03 21:41:51 -0400269 struct compat_statfs buf;
Al Viro4ada54e2017-04-08 18:08:15 -0400270 if (sizeof ubuf->f_blocks == 4) {
271 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
272 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
273 return -EOVERFLOW;
274 /* f_files and f_ffree may be -1; it's okay
275 * to stuff that into 32 bits */
276 if (kbuf->f_files != 0xffffffffffffffffULL
277 && (kbuf->f_files & 0xffffffff00000000ULL))
278 return -EOVERFLOW;
279 if (kbuf->f_ffree != 0xffffffffffffffffULL
280 && (kbuf->f_ffree & 0xffffffff00000000ULL))
281 return -EOVERFLOW;
282 }
Al Viroae2a97622017-06-03 21:41:51 -0400283 memset(&buf, 0, sizeof(struct compat_statfs));
284 buf.f_type = kbuf->f_type;
285 buf.f_bsize = kbuf->f_bsize;
286 buf.f_blocks = kbuf->f_blocks;
287 buf.f_bfree = kbuf->f_bfree;
288 buf.f_bavail = kbuf->f_bavail;
289 buf.f_files = kbuf->f_files;
290 buf.f_ffree = kbuf->f_ffree;
291 buf.f_namelen = kbuf->f_namelen;
292 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
293 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
294 buf.f_frsize = kbuf->f_frsize;
295 buf.f_flags = kbuf->f_flags;
296 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
Al Viro4ada54e2017-04-08 18:08:15 -0400297 return -EFAULT;
298 return 0;
299}
300
301/*
302 * The following statfs calls are copies of code from fs/statfs.c and
303 * should be checked against those from time to time
304 */
305COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
306{
307 struct kstatfs tmp;
308 int error = user_statfs(pathname, &tmp);
309 if (!error)
310 error = put_compat_statfs(buf, &tmp);
311 return error;
312}
313
314COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
315{
316 struct kstatfs tmp;
317 int error = fd_statfs(fd, &tmp);
318 if (!error)
319 error = put_compat_statfs(buf, &tmp);
320 return error;
321}
322
323static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
324{
Al Viroae2a97622017-06-03 21:41:51 -0400325 struct compat_statfs64 buf;
Eric Sandeencc3a7bf2019-10-02 16:17:54 -0500326
327 if ((kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
328 return -EOVERFLOW;
329
Al Viroae2a97622017-06-03 21:41:51 -0400330 memset(&buf, 0, sizeof(struct compat_statfs64));
331 buf.f_type = kbuf->f_type;
332 buf.f_bsize = kbuf->f_bsize;
333 buf.f_blocks = kbuf->f_blocks;
334 buf.f_bfree = kbuf->f_bfree;
335 buf.f_bavail = kbuf->f_bavail;
336 buf.f_files = kbuf->f_files;
337 buf.f_ffree = kbuf->f_ffree;
338 buf.f_namelen = kbuf->f_namelen;
339 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
340 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
341 buf.f_frsize = kbuf->f_frsize;
342 buf.f_flags = kbuf->f_flags;
343 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
Al Viro4ada54e2017-04-08 18:08:15 -0400344 return -EFAULT;
345 return 0;
346}
347
Mark Rutland9b54bf92018-07-11 14:56:51 +0100348int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
Al Viro4ada54e2017-04-08 18:08:15 -0400349{
350 struct kstatfs tmp;
351 int error;
352
353 if (sz != sizeof(*buf))
354 return -EINVAL;
355
356 error = user_statfs(pathname, &tmp);
357 if (!error)
358 error = put_compat_statfs64(buf, &tmp);
359 return error;
360}
361
Mark Rutland9b54bf92018-07-11 14:56:51 +0100362COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
363{
364 return kcompat_sys_statfs64(pathname, sz, buf);
365}
366
367int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user * buf)
Al Viro4ada54e2017-04-08 18:08:15 -0400368{
369 struct kstatfs tmp;
370 int error;
371
372 if (sz != sizeof(*buf))
373 return -EINVAL;
374
375 error = fd_statfs(fd, &tmp);
376 if (!error)
377 error = put_compat_statfs64(buf, &tmp);
378 return error;
379}
380
Mark Rutland9b54bf92018-07-11 14:56:51 +0100381COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
382{
383 return kcompat_sys_fstatfs64(fd, sz, buf);
384}
385
Al Viro4ada54e2017-04-08 18:08:15 -0400386/*
387 * This is a copy of sys_ustat, just dealing with a structure layout.
388 * Given how simple this syscall is that apporach is more maintainable
389 * than the various conversion hacks.
390 */
391COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
392{
393 struct compat_ustat tmp;
394 struct kstatfs sbuf;
395 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
396 if (err)
397 return err;
398
399 memset(&tmp, 0, sizeof(struct compat_ustat));
400 tmp.f_tfree = sbuf.f_bfree;
401 tmp.f_tinode = sbuf.f_ffree;
402 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
403 return -EFAULT;
404 return 0;
405}
406#endif