blob: eea7af6f2f229e85f71e3ff3ac5d541e88956856 [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
Amir Goldsteinec86ff52019-01-10 19:04:38 +020070int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
71{
72 struct kstatfs st;
73 int error;
74
75 error = statfs_by_dentry(dentry, &st);
76 if (error)
77 return error;
78
79 *fsid = st.f_fsid;
80 return 0;
81}
82EXPORT_SYMBOL(vfs_get_fsid);
83
Al Virof0bb5aa2016-11-20 20:27:12 -050084int vfs_statfs(const struct path *path, struct kstatfs *buf)
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020085{
Christoph Hellwig365b1812010-07-07 18:53:25 +020086 int error;
87
88 error = statfs_by_dentry(path->dentry, buf);
89 if (!error)
90 buf->f_flags = calculate_f_flags(path->mnt);
91 return error;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020092}
Al Viro7ed1ee62010-03-23 10:37:36 -040093EXPORT_SYMBOL(vfs_statfs);
94
Al Viroc8b91ac2011-03-12 10:41:39 -050095int user_statfs(const char __user *pathname, struct kstatfs *st)
Al Viro7ed1ee62010-03-23 10:37:36 -040096{
Al Viroc8b91ac2011-03-12 10:41:39 -050097 struct path path;
Jeff Layton96948fc2012-12-11 12:10:14 -050098 int error;
99 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
100retry:
101 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -0500102 if (!error) {
103 error = vfs_statfs(&path, st);
104 path_put(&path);
Jeff Layton96948fc2012-12-11 12:10:14 -0500105 if (retry_estale(error, lookup_flags)) {
106 lookup_flags |= LOOKUP_REVAL;
107 goto retry;
108 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500109 }
110 return error;
111}
Al Viro7ed1ee62010-03-23 10:37:36 -0400112
Al Viroc8b91ac2011-03-12 10:41:39 -0500113int fd_statfs(int fd, struct kstatfs *st)
114{
Linus Torvalds9d057462013-09-30 08:35:10 -0700115 struct fd f = fdget_raw(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -0500116 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400117 if (f.file) {
118 error = vfs_statfs(&f.file->f_path, st);
119 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -0500120 }
121 return error;
122}
Al Viro7ed1ee62010-03-23 10:37:36 -0400123
Al Viroc8b91ac2011-03-12 10:41:39 -0500124static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
125{
126 struct statfs buf;
127
128 if (sizeof(buf) == sizeof(*st))
129 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400130 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500131 if (sizeof buf.f_blocks == 4) {
132 if ((st->f_blocks | st->f_bfree | st->f_bavail |
133 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400134 0xffffffff00000000ULL)
135 return -EOVERFLOW;
136 /*
137 * f_files and f_ffree may be -1; it's okay to stuff
138 * that into 32 bits
139 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500140 if (st->f_files != -1 &&
141 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400142 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500143 if (st->f_ffree != -1 &&
144 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400145 return -EOVERFLOW;
146 }
147
Al Viroc8b91ac2011-03-12 10:41:39 -0500148 buf.f_type = st->f_type;
149 buf.f_bsize = st->f_bsize;
150 buf.f_blocks = st->f_blocks;
151 buf.f_bfree = st->f_bfree;
152 buf.f_bavail = st->f_bavail;
153 buf.f_files = st->f_files;
154 buf.f_ffree = st->f_ffree;
155 buf.f_fsid = st->f_fsid;
156 buf.f_namelen = st->f_namelen;
157 buf.f_frsize = st->f_frsize;
158 buf.f_flags = st->f_flags;
159 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400160 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500161 if (copy_to_user(p, &buf, sizeof(buf)))
162 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400163 return 0;
164}
165
Al Viroc8b91ac2011-03-12 10:41:39 -0500166static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400167{
Al Viroc8b91ac2011-03-12 10:41:39 -0500168 struct statfs64 buf;
169 if (sizeof(buf) == sizeof(*st))
170 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400171 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500172 buf.f_type = st->f_type;
173 buf.f_bsize = st->f_bsize;
174 buf.f_blocks = st->f_blocks;
175 buf.f_bfree = st->f_bfree;
176 buf.f_bavail = st->f_bavail;
177 buf.f_files = st->f_files;
178 buf.f_ffree = st->f_ffree;
179 buf.f_fsid = st->f_fsid;
180 buf.f_namelen = st->f_namelen;
181 buf.f_frsize = st->f_frsize;
182 buf.f_flags = st->f_flags;
183 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400184 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500185 if (copy_to_user(p, &buf, sizeof(buf)))
186 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400187 return 0;
188}
189
190SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
191{
Al Viroc8b91ac2011-03-12 10:41:39 -0500192 struct kstatfs st;
193 int error = user_statfs(pathname, &st);
194 if (!error)
195 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400196 return error;
197}
198
199SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
200{
Al Viroc8b91ac2011-03-12 10:41:39 -0500201 struct kstatfs st;
202 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400203 if (sz != sizeof(*buf))
204 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500205 error = user_statfs(pathname, &st);
206 if (!error)
207 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400208 return error;
209}
210
211SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
212{
Al Viroc8b91ac2011-03-12 10:41:39 -0500213 struct kstatfs st;
214 int error = fd_statfs(fd, &st);
215 if (!error)
216 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400217 return error;
218}
219
220SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
221{
Al Viroc8b91ac2011-03-12 10:41:39 -0500222 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400223 int error;
224
225 if (sz != sizeof(*buf))
226 return -EINVAL;
227
Al Viroc8b91ac2011-03-12 10:41:39 -0500228 error = fd_statfs(fd, &st);
229 if (!error)
230 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400231 return error;
232}
233
Al Viro53fd88ab62017-10-14 23:00:54 -0400234static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400235{
Al Virocf31e702012-01-02 22:28:36 -0500236 struct super_block *s = user_get_super(dev);
Al Viro7ed1ee62010-03-23 10:37:36 -0400237 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400238 if (!s)
239 return -EINVAL;
240
Al Virocf31e702012-01-02 22:28:36 -0500241 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400242 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500243 return err;
244}
245
246SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
247{
248 struct ustat tmp;
249 struct kstatfs sbuf;
250 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400251 if (err)
252 return err;
253
254 memset(&tmp,0,sizeof(struct ustat));
255 tmp.f_tfree = sbuf.f_bfree;
256 tmp.f_tinode = sbuf.f_ffree;
257
258 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
259}
Al Viro4ada54e2017-04-08 18:08:15 -0400260
261#ifdef CONFIG_COMPAT
262static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
263{
Al Viroae2a97622017-06-03 21:41:51 -0400264 struct compat_statfs buf;
Al Viro4ada54e2017-04-08 18:08:15 -0400265 if (sizeof ubuf->f_blocks == 4) {
266 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
267 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
268 return -EOVERFLOW;
269 /* f_files and f_ffree may be -1; it's okay
270 * to stuff that into 32 bits */
271 if (kbuf->f_files != 0xffffffffffffffffULL
272 && (kbuf->f_files & 0xffffffff00000000ULL))
273 return -EOVERFLOW;
274 if (kbuf->f_ffree != 0xffffffffffffffffULL
275 && (kbuf->f_ffree & 0xffffffff00000000ULL))
276 return -EOVERFLOW;
277 }
Al Viroae2a97622017-06-03 21:41:51 -0400278 memset(&buf, 0, sizeof(struct compat_statfs));
279 buf.f_type = kbuf->f_type;
280 buf.f_bsize = kbuf->f_bsize;
281 buf.f_blocks = kbuf->f_blocks;
282 buf.f_bfree = kbuf->f_bfree;
283 buf.f_bavail = kbuf->f_bavail;
284 buf.f_files = kbuf->f_files;
285 buf.f_ffree = kbuf->f_ffree;
286 buf.f_namelen = kbuf->f_namelen;
287 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
288 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
289 buf.f_frsize = kbuf->f_frsize;
290 buf.f_flags = kbuf->f_flags;
291 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
Al Viro4ada54e2017-04-08 18:08:15 -0400292 return -EFAULT;
293 return 0;
294}
295
296/*
297 * The following statfs calls are copies of code from fs/statfs.c and
298 * should be checked against those from time to time
299 */
300COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
301{
302 struct kstatfs tmp;
303 int error = user_statfs(pathname, &tmp);
304 if (!error)
305 error = put_compat_statfs(buf, &tmp);
306 return error;
307}
308
309COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
310{
311 struct kstatfs tmp;
312 int error = fd_statfs(fd, &tmp);
313 if (!error)
314 error = put_compat_statfs(buf, &tmp);
315 return error;
316}
317
318static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
319{
Al Viroae2a97622017-06-03 21:41:51 -0400320 struct compat_statfs64 buf;
Al Viro4ada54e2017-04-08 18:08:15 -0400321 if (sizeof(ubuf->f_bsize) == 4) {
322 if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
323 kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
324 return -EOVERFLOW;
325 /* f_files and f_ffree may be -1; it's okay
326 * to stuff that into 32 bits */
327 if (kbuf->f_files != 0xffffffffffffffffULL
328 && (kbuf->f_files & 0xffffffff00000000ULL))
329 return -EOVERFLOW;
330 if (kbuf->f_ffree != 0xffffffffffffffffULL
331 && (kbuf->f_ffree & 0xffffffff00000000ULL))
332 return -EOVERFLOW;
333 }
Al Viroae2a97622017-06-03 21:41:51 -0400334 memset(&buf, 0, sizeof(struct compat_statfs64));
335 buf.f_type = kbuf->f_type;
336 buf.f_bsize = kbuf->f_bsize;
337 buf.f_blocks = kbuf->f_blocks;
338 buf.f_bfree = kbuf->f_bfree;
339 buf.f_bavail = kbuf->f_bavail;
340 buf.f_files = kbuf->f_files;
341 buf.f_ffree = kbuf->f_ffree;
342 buf.f_namelen = kbuf->f_namelen;
343 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
344 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
345 buf.f_frsize = kbuf->f_frsize;
346 buf.f_flags = kbuf->f_flags;
347 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
Al Viro4ada54e2017-04-08 18:08:15 -0400348 return -EFAULT;
349 return 0;
350}
351
Mark Rutland9b54bf92018-07-11 14:56:51 +0100352int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
Al Viro4ada54e2017-04-08 18:08:15 -0400353{
354 struct kstatfs tmp;
355 int error;
356
357 if (sz != sizeof(*buf))
358 return -EINVAL;
359
360 error = user_statfs(pathname, &tmp);
361 if (!error)
362 error = put_compat_statfs64(buf, &tmp);
363 return error;
364}
365
Mark Rutland9b54bf92018-07-11 14:56:51 +0100366COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
367{
368 return kcompat_sys_statfs64(pathname, sz, buf);
369}
370
371int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user * buf)
Al Viro4ada54e2017-04-08 18:08:15 -0400372{
373 struct kstatfs tmp;
374 int error;
375
376 if (sz != sizeof(*buf))
377 return -EINVAL;
378
379 error = fd_statfs(fd, &tmp);
380 if (!error)
381 error = put_compat_statfs64(buf, &tmp);
382 return error;
383}
384
Mark Rutland9b54bf92018-07-11 14:56:51 +0100385COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
386{
387 return kcompat_sys_fstatfs64(fd, sz, buf);
388}
389
Al Viro4ada54e2017-04-08 18:08:15 -0400390/*
391 * This is a copy of sys_ustat, just dealing with a structure layout.
392 * Given how simple this syscall is that apporach is more maintainable
393 * than the various conversion hacks.
394 */
395COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
396{
397 struct compat_ustat tmp;
398 struct kstatfs sbuf;
399 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
400 if (err)
401 return err;
402
403 memset(&tmp, 0, sizeof(struct compat_ustat));
404 tmp.f_tfree = sbuf.f_bfree;
405 tmp.f_tinode = sbuf.f_ffree;
406 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
407 return -EFAULT;
408 return 0;
409}
410#endif