blob: 56f655f757ffb36cb53c9f60f35695652d929529 [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;
32 return flags;
33}
34
35static int flags_by_sb(int s_flags)
36{
37 int flags = 0;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080038 if (s_flags & SB_SYNCHRONOUS)
Christoph Hellwig365b1812010-07-07 18:53:25 +020039 flags |= ST_SYNCHRONOUS;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080040 if (s_flags & SB_MANDLOCK)
Christoph Hellwig365b1812010-07-07 18:53:25 +020041 flags |= ST_MANDLOCK;
Linus Torvalds1751e8a2017-11-27 13:05:09 -080042 if (s_flags & SB_RDONLY)
Carlos Maiolinoa8e2b632017-06-29 11:25:40 +020043 flags |= ST_RDONLY;
Christoph Hellwig365b1812010-07-07 18:53:25 +020044 return flags;
45}
46
47static int calculate_f_flags(struct vfsmount *mnt)
48{
49 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
50 flags_by_sb(mnt->mnt_sb->s_flags);
51}
52
Al Virocf31e702012-01-02 22:28:36 -050053static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
Al Viro7ed1ee62010-03-23 10:37:36 -040054{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020055 int retval;
Al Viro7ed1ee62010-03-23 10:37:36 -040056
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020057 if (!dentry->d_sb->s_op->statfs)
58 return -ENOSYS;
59
60 memset(buf, 0, sizeof(*buf));
61 retval = security_sb_statfs(dentry);
62 if (retval)
63 return retval;
64 retval = dentry->d_sb->s_op->statfs(dentry, buf);
65 if (retval == 0 && buf->f_frsize == 0)
66 buf->f_frsize = buf->f_bsize;
Al Viro7ed1ee62010-03-23 10:37:36 -040067 return retval;
68}
69
Al Virof0bb5aa2016-11-20 20:27:12 -050070int vfs_statfs(const struct path *path, struct kstatfs *buf)
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020071{
Christoph Hellwig365b1812010-07-07 18:53:25 +020072 int error;
73
74 error = statfs_by_dentry(path->dentry, buf);
75 if (!error)
76 buf->f_flags = calculate_f_flags(path->mnt);
77 return error;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020078}
Al Viro7ed1ee62010-03-23 10:37:36 -040079EXPORT_SYMBOL(vfs_statfs);
80
Al Viroc8b91ac2011-03-12 10:41:39 -050081int user_statfs(const char __user *pathname, struct kstatfs *st)
Al Viro7ed1ee62010-03-23 10:37:36 -040082{
Al Viroc8b91ac2011-03-12 10:41:39 -050083 struct path path;
Jeff Layton96948fc2012-12-11 12:10:14 -050084 int error;
85 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
86retry:
87 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -050088 if (!error) {
89 error = vfs_statfs(&path, st);
90 path_put(&path);
Jeff Layton96948fc2012-12-11 12:10:14 -050091 if (retry_estale(error, lookup_flags)) {
92 lookup_flags |= LOOKUP_REVAL;
93 goto retry;
94 }
Al Viroc8b91ac2011-03-12 10:41:39 -050095 }
96 return error;
97}
Al Viro7ed1ee62010-03-23 10:37:36 -040098
Al Viroc8b91ac2011-03-12 10:41:39 -050099int fd_statfs(int fd, struct kstatfs *st)
100{
Linus Torvalds9d057462013-09-30 08:35:10 -0700101 struct fd f = fdget_raw(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -0500102 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400103 if (f.file) {
104 error = vfs_statfs(&f.file->f_path, st);
105 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -0500106 }
107 return error;
108}
Al Viro7ed1ee62010-03-23 10:37:36 -0400109
Al Viroc8b91ac2011-03-12 10:41:39 -0500110static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
111{
112 struct statfs buf;
113
114 if (sizeof(buf) == sizeof(*st))
115 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400116 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500117 if (sizeof buf.f_blocks == 4) {
118 if ((st->f_blocks | st->f_bfree | st->f_bavail |
119 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400120 0xffffffff00000000ULL)
121 return -EOVERFLOW;
122 /*
123 * f_files and f_ffree may be -1; it's okay to stuff
124 * that into 32 bits
125 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500126 if (st->f_files != -1 &&
127 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400128 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500129 if (st->f_ffree != -1 &&
130 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400131 return -EOVERFLOW;
132 }
133
Al Viroc8b91ac2011-03-12 10:41:39 -0500134 buf.f_type = st->f_type;
135 buf.f_bsize = st->f_bsize;
136 buf.f_blocks = st->f_blocks;
137 buf.f_bfree = st->f_bfree;
138 buf.f_bavail = st->f_bavail;
139 buf.f_files = st->f_files;
140 buf.f_ffree = st->f_ffree;
141 buf.f_fsid = st->f_fsid;
142 buf.f_namelen = st->f_namelen;
143 buf.f_frsize = st->f_frsize;
144 buf.f_flags = st->f_flags;
145 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400146 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500147 if (copy_to_user(p, &buf, sizeof(buf)))
148 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400149 return 0;
150}
151
Al Viroc8b91ac2011-03-12 10:41:39 -0500152static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400153{
Al Viroc8b91ac2011-03-12 10:41:39 -0500154 struct statfs64 buf;
155 if (sizeof(buf) == sizeof(*st))
156 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400157 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500158 buf.f_type = st->f_type;
159 buf.f_bsize = st->f_bsize;
160 buf.f_blocks = st->f_blocks;
161 buf.f_bfree = st->f_bfree;
162 buf.f_bavail = st->f_bavail;
163 buf.f_files = st->f_files;
164 buf.f_ffree = st->f_ffree;
165 buf.f_fsid = st->f_fsid;
166 buf.f_namelen = st->f_namelen;
167 buf.f_frsize = st->f_frsize;
168 buf.f_flags = st->f_flags;
169 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400170 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500171 if (copy_to_user(p, &buf, sizeof(buf)))
172 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400173 return 0;
174}
175
176SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
177{
Al Viroc8b91ac2011-03-12 10:41:39 -0500178 struct kstatfs st;
179 int error = user_statfs(pathname, &st);
180 if (!error)
181 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400182 return error;
183}
184
185SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
186{
Al Viroc8b91ac2011-03-12 10:41:39 -0500187 struct kstatfs st;
188 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400189 if (sz != sizeof(*buf))
190 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500191 error = user_statfs(pathname, &st);
192 if (!error)
193 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400194 return error;
195}
196
197SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
198{
Al Viroc8b91ac2011-03-12 10:41:39 -0500199 struct kstatfs st;
200 int error = fd_statfs(fd, &st);
201 if (!error)
202 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400203 return error;
204}
205
206SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
207{
Al Viroc8b91ac2011-03-12 10:41:39 -0500208 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400209 int error;
210
211 if (sz != sizeof(*buf))
212 return -EINVAL;
213
Al Viroc8b91ac2011-03-12 10:41:39 -0500214 error = fd_statfs(fd, &st);
215 if (!error)
216 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400217 return error;
218}
219
Al Viro53fd88ab62017-10-14 23:00:54 -0400220static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400221{
Al Virocf31e702012-01-02 22:28:36 -0500222 struct super_block *s = user_get_super(dev);
Al Viro7ed1ee62010-03-23 10:37:36 -0400223 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400224 if (!s)
225 return -EINVAL;
226
Al Virocf31e702012-01-02 22:28:36 -0500227 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400228 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500229 return err;
230}
231
232SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
233{
234 struct ustat tmp;
235 struct kstatfs sbuf;
236 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400237 if (err)
238 return err;
239
240 memset(&tmp,0,sizeof(struct ustat));
241 tmp.f_tfree = sbuf.f_bfree;
242 tmp.f_tinode = sbuf.f_ffree;
243
244 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
245}
Al Viro4ada54e2017-04-08 18:08:15 -0400246
247#ifdef CONFIG_COMPAT
248static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
249{
Al Viroae2a97622017-06-03 21:41:51 -0400250 struct compat_statfs buf;
Al Viro4ada54e2017-04-08 18:08:15 -0400251 if (sizeof ubuf->f_blocks == 4) {
252 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
253 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
254 return -EOVERFLOW;
255 /* f_files and f_ffree may be -1; it's okay
256 * to stuff that into 32 bits */
257 if (kbuf->f_files != 0xffffffffffffffffULL
258 && (kbuf->f_files & 0xffffffff00000000ULL))
259 return -EOVERFLOW;
260 if (kbuf->f_ffree != 0xffffffffffffffffULL
261 && (kbuf->f_ffree & 0xffffffff00000000ULL))
262 return -EOVERFLOW;
263 }
Al Viroae2a97622017-06-03 21:41:51 -0400264 memset(&buf, 0, sizeof(struct compat_statfs));
265 buf.f_type = kbuf->f_type;
266 buf.f_bsize = kbuf->f_bsize;
267 buf.f_blocks = kbuf->f_blocks;
268 buf.f_bfree = kbuf->f_bfree;
269 buf.f_bavail = kbuf->f_bavail;
270 buf.f_files = kbuf->f_files;
271 buf.f_ffree = kbuf->f_ffree;
272 buf.f_namelen = kbuf->f_namelen;
273 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
274 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
275 buf.f_frsize = kbuf->f_frsize;
276 buf.f_flags = kbuf->f_flags;
277 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
Al Viro4ada54e2017-04-08 18:08:15 -0400278 return -EFAULT;
279 return 0;
280}
281
282/*
283 * The following statfs calls are copies of code from fs/statfs.c and
284 * should be checked against those from time to time
285 */
286COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
287{
288 struct kstatfs tmp;
289 int error = user_statfs(pathname, &tmp);
290 if (!error)
291 error = put_compat_statfs(buf, &tmp);
292 return error;
293}
294
295COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
296{
297 struct kstatfs tmp;
298 int error = fd_statfs(fd, &tmp);
299 if (!error)
300 error = put_compat_statfs(buf, &tmp);
301 return error;
302}
303
304static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
305{
Al Viroae2a97622017-06-03 21:41:51 -0400306 struct compat_statfs64 buf;
Eric Sandeeneffad572019-10-02 16:17:54 -0500307
308 if ((kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
309 return -EOVERFLOW;
310
Al Viroae2a97622017-06-03 21:41:51 -0400311 memset(&buf, 0, sizeof(struct compat_statfs64));
312 buf.f_type = kbuf->f_type;
313 buf.f_bsize = kbuf->f_bsize;
314 buf.f_blocks = kbuf->f_blocks;
315 buf.f_bfree = kbuf->f_bfree;
316 buf.f_bavail = kbuf->f_bavail;
317 buf.f_files = kbuf->f_files;
318 buf.f_ffree = kbuf->f_ffree;
319 buf.f_namelen = kbuf->f_namelen;
320 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
321 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
322 buf.f_frsize = kbuf->f_frsize;
323 buf.f_flags = kbuf->f_flags;
324 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
Al Viro4ada54e2017-04-08 18:08:15 -0400325 return -EFAULT;
326 return 0;
327}
328
Mark Rutland9b54bf92018-07-11 14:56:51 +0100329int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
Al Viro4ada54e2017-04-08 18:08:15 -0400330{
331 struct kstatfs tmp;
332 int error;
333
334 if (sz != sizeof(*buf))
335 return -EINVAL;
336
337 error = user_statfs(pathname, &tmp);
338 if (!error)
339 error = put_compat_statfs64(buf, &tmp);
340 return error;
341}
342
Mark Rutland9b54bf92018-07-11 14:56:51 +0100343COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
344{
345 return kcompat_sys_statfs64(pathname, sz, buf);
346}
347
348int kcompat_sys_fstatfs64(unsigned int fd, 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 = fd_statfs(fd, &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(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
363{
364 return kcompat_sys_fstatfs64(fd, sz, buf);
365}
366
Al Viro4ada54e2017-04-08 18:08:15 -0400367/*
368 * This is a copy of sys_ustat, just dealing with a structure layout.
369 * Given how simple this syscall is that apporach is more maintainable
370 * than the various conversion hacks.
371 */
372COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
373{
374 struct compat_ustat tmp;
375 struct kstatfs sbuf;
376 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
377 if (err)
378 return err;
379
380 memset(&tmp, 0, sizeof(struct compat_ustat));
381 tmp.f_tfree = sbuf.f_bfree;
382 tmp.f_tinode = sbuf.f_ffree;
383 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
384 return -EFAULT;
385 return 0;
386}
387#endif