1 #include <linux/syscalls.h>
2 #include <linux/export.h>
3 #include <linux/fs.h>
4 #include <linux/file.h>
5 #include <linux/mount.h>
6 #include <linux/namei.h>
7 #include <linux/statfs.h>
8 #include <linux/security.h>
9 #include <linux/uaccess.h>
10 #include "internal.h"
11 
flags_by_mnt(int mnt_flags)12 static int flags_by_mnt(int mnt_flags)
13 {
14 	int flags = 0;
15 
16 	if (mnt_flags & MNT_READONLY)
17 		flags |= ST_RDONLY;
18 	if (mnt_flags & MNT_NOSUID)
19 		flags |= ST_NOSUID;
20 	if (mnt_flags & MNT_NODEV)
21 		flags |= ST_NODEV;
22 	if (mnt_flags & MNT_NOEXEC)
23 		flags |= ST_NOEXEC;
24 	if (mnt_flags & MNT_NOATIME)
25 		flags |= ST_NOATIME;
26 	if (mnt_flags & MNT_NODIRATIME)
27 		flags |= ST_NODIRATIME;
28 	if (mnt_flags & MNT_RELATIME)
29 		flags |= ST_RELATIME;
30 	return flags;
31 }
32 
flags_by_sb(int s_flags)33 static int flags_by_sb(int s_flags)
34 {
35 	int flags = 0;
36 	if (s_flags & MS_SYNCHRONOUS)
37 		flags |= ST_SYNCHRONOUS;
38 	if (s_flags & MS_MANDLOCK)
39 		flags |= ST_MANDLOCK;
40 	return flags;
41 }
42 
calculate_f_flags(struct vfsmount * mnt)43 static int calculate_f_flags(struct vfsmount *mnt)
44 {
45 	return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
46 		flags_by_sb(mnt->mnt_sb->s_flags);
47 }
48 
statfs_by_dentry(struct dentry * dentry,struct kstatfs * buf)49 static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
50 {
51 	int retval;
52 
53 	if (!dentry->d_sb->s_op->statfs)
54 		return -ENOSYS;
55 
56 	memset(buf, 0, sizeof(*buf));
57 	retval = security_sb_statfs(dentry);
58 	if (retval)
59 		return retval;
60 	retval = dentry->d_sb->s_op->statfs(dentry, buf);
61 	if (retval == 0 && buf->f_frsize == 0)
62 		buf->f_frsize = buf->f_bsize;
63 	return retval;
64 }
65 
vfs_statfs(struct path * path,struct kstatfs * buf)66 int vfs_statfs(struct path *path, struct kstatfs *buf)
67 {
68 	int error;
69 
70 	error = statfs_by_dentry(path->dentry, buf);
71 	if (!error)
72 		buf->f_flags = calculate_f_flags(path->mnt);
73 	return error;
74 }
75 EXPORT_SYMBOL(vfs_statfs);
76 
user_statfs(const char __user * pathname,struct kstatfs * st)77 int user_statfs(const char __user *pathname, struct kstatfs *st)
78 {
79 	struct path path;
80 	int error = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
81 	if (!error) {
82 		error = vfs_statfs(&path, st);
83 		path_put(&path);
84 	}
85 	return error;
86 }
87 
fd_statfs(int fd,struct kstatfs * st)88 int fd_statfs(int fd, struct kstatfs *st)
89 {
90 	struct file *file = fget_raw(fd);
91 	int error = -EBADF;
92 	if (file) {
93 		error = vfs_statfs(&file->f_path, st);
94 		fput(file);
95 	}
96 	return error;
97 }
98 
do_statfs_native(struct kstatfs * st,struct statfs __user * p)99 static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
100 {
101 	struct statfs buf;
102 
103 	if (sizeof(buf) == sizeof(*st))
104 		memcpy(&buf, st, sizeof(*st));
105 	else {
106 		if (sizeof buf.f_blocks == 4) {
107 			if ((st->f_blocks | st->f_bfree | st->f_bavail |
108 			     st->f_bsize | st->f_frsize) &
109 			    0xffffffff00000000ULL)
110 				return -EOVERFLOW;
111 			/*
112 			 * f_files and f_ffree may be -1; it's okay to stuff
113 			 * that into 32 bits
114 			 */
115 			if (st->f_files != -1 &&
116 			    (st->f_files & 0xffffffff00000000ULL))
117 				return -EOVERFLOW;
118 			if (st->f_ffree != -1 &&
119 			    (st->f_ffree & 0xffffffff00000000ULL))
120 				return -EOVERFLOW;
121 		}
122 
123 		buf.f_type = st->f_type;
124 		buf.f_bsize = st->f_bsize;
125 		buf.f_blocks = st->f_blocks;
126 		buf.f_bfree = st->f_bfree;
127 		buf.f_bavail = st->f_bavail;
128 		buf.f_files = st->f_files;
129 		buf.f_ffree = st->f_ffree;
130 		buf.f_fsid = st->f_fsid;
131 		buf.f_namelen = st->f_namelen;
132 		buf.f_frsize = st->f_frsize;
133 		buf.f_flags = st->f_flags;
134 		memset(buf.f_spare, 0, sizeof(buf.f_spare));
135 	}
136 	if (copy_to_user(p, &buf, sizeof(buf)))
137 		return -EFAULT;
138 	return 0;
139 }
140 
do_statfs64(struct kstatfs * st,struct statfs64 __user * p)141 static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
142 {
143 	struct statfs64 buf;
144 	if (sizeof(buf) == sizeof(*st))
145 		memcpy(&buf, st, sizeof(*st));
146 	else {
147 		buf.f_type = st->f_type;
148 		buf.f_bsize = st->f_bsize;
149 		buf.f_blocks = st->f_blocks;
150 		buf.f_bfree = st->f_bfree;
151 		buf.f_bavail = st->f_bavail;
152 		buf.f_files = st->f_files;
153 		buf.f_ffree = st->f_ffree;
154 		buf.f_fsid = st->f_fsid;
155 		buf.f_namelen = st->f_namelen;
156 		buf.f_frsize = st->f_frsize;
157 		buf.f_flags = st->f_flags;
158 		memset(buf.f_spare, 0, sizeof(buf.f_spare));
159 	}
160 	if (copy_to_user(p, &buf, sizeof(buf)))
161 		return -EFAULT;
162 	return 0;
163 }
164 
SYSCALL_DEFINE2(statfs,const char __user *,pathname,struct statfs __user *,buf)165 SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
166 {
167 	struct kstatfs st;
168 	int error = user_statfs(pathname, &st);
169 	if (!error)
170 		error = do_statfs_native(&st, buf);
171 	return error;
172 }
173 
SYSCALL_DEFINE3(statfs64,const char __user *,pathname,size_t,sz,struct statfs64 __user *,buf)174 SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
175 {
176 	struct kstatfs st;
177 	int error;
178 	if (sz != sizeof(*buf))
179 		return -EINVAL;
180 	error = user_statfs(pathname, &st);
181 	if (!error)
182 		error = do_statfs64(&st, buf);
183 	return error;
184 }
185 
SYSCALL_DEFINE2(fstatfs,unsigned int,fd,struct statfs __user *,buf)186 SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
187 {
188 	struct kstatfs st;
189 	int error = fd_statfs(fd, &st);
190 	if (!error)
191 		error = do_statfs_native(&st, buf);
192 	return error;
193 }
194 
SYSCALL_DEFINE3(fstatfs64,unsigned int,fd,size_t,sz,struct statfs64 __user *,buf)195 SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
196 {
197 	struct kstatfs st;
198 	int error;
199 
200 	if (sz != sizeof(*buf))
201 		return -EINVAL;
202 
203 	error = fd_statfs(fd, &st);
204 	if (!error)
205 		error = do_statfs64(&st, buf);
206 	return error;
207 }
208 
vfs_ustat(dev_t dev,struct kstatfs * sbuf)209 int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
210 {
211 	struct super_block *s = user_get_super(dev);
212 	int err;
213 	if (!s)
214 		return -EINVAL;
215 
216 	err = statfs_by_dentry(s->s_root, sbuf);
217 	drop_super(s);
218 	return err;
219 }
220 
SYSCALL_DEFINE2(ustat,unsigned,dev,struct ustat __user *,ubuf)221 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
222 {
223 	struct ustat tmp;
224 	struct kstatfs sbuf;
225 	int err = vfs_ustat(new_decode_dev(dev), &sbuf);
226 	if (err)
227 		return err;
228 
229 	memset(&tmp,0,sizeof(struct ustat));
230 	tmp.f_tfree = sbuf.f_bfree;
231 	tmp.f_tinode = sbuf.f_ffree;
232 
233 	return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
234 }
235