statfs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/syscalls.h>
  3. #include <linux/export.h>
  4. #include <linux/fs.h>
  5. #include <linux/file.h>
  6. #include <linux/mount.h>
  7. #include <linux/namei.h>
  8. #include <linux/statfs.h>
  9. #include <linux/security.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/compat.h>
  12. #include "internal.h"
  13. static int flags_by_mnt(int mnt_flags)
  14. {
  15. int flags = 0;
  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. static int flags_by_sb(int s_flags)
  33. {
  34. int flags = 0;
  35. if (s_flags & SB_SYNCHRONOUS)
  36. flags |= ST_SYNCHRONOUS;
  37. if (s_flags & SB_MANDLOCK)
  38. flags |= ST_MANDLOCK;
  39. if (s_flags & SB_RDONLY)
  40. flags |= ST_RDONLY;
  41. return flags;
  42. }
  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. static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
  49. {
  50. int retval;
  51. if (!dentry->d_sb->s_op->statfs)
  52. return -ENOSYS;
  53. memset(buf, 0, sizeof(*buf));
  54. retval = security_sb_statfs(dentry);
  55. if (retval)
  56. return retval;
  57. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  58. if (retval == 0 && buf->f_frsize == 0)
  59. buf->f_frsize = buf->f_bsize;
  60. return retval;
  61. }
  62. int vfs_statfs(const struct path *path, struct kstatfs *buf)
  63. {
  64. int error;
  65. error = statfs_by_dentry(path->dentry, buf);
  66. if (!error)
  67. buf->f_flags = calculate_f_flags(path->mnt);
  68. return error;
  69. }
  70. EXPORT_SYMBOL(vfs_statfs);
  71. int user_statfs(const char __user *pathname, struct kstatfs *st)
  72. {
  73. struct path path;
  74. int error;
  75. unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
  76. retry:
  77. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  78. if (!error) {
  79. error = vfs_statfs(&path, st);
  80. path_put(&path);
  81. if (retry_estale(error, lookup_flags)) {
  82. lookup_flags |= LOOKUP_REVAL;
  83. goto retry;
  84. }
  85. }
  86. return error;
  87. }
  88. int fd_statfs(int fd, struct kstatfs *st)
  89. {
  90. struct fd f = fdget_raw(fd);
  91. int error = -EBADF;
  92. if (f.file) {
  93. error = vfs_statfs(&f.file->f_path, st);
  94. fdput(f);
  95. }
  96. return error;
  97. }
  98. static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
  99. {
  100. struct statfs buf;
  101. if (sizeof(buf) == sizeof(*st))
  102. memcpy(&buf, st, sizeof(*st));
  103. else {
  104. if (sizeof buf.f_blocks == 4) {
  105. if ((st->f_blocks | st->f_bfree | st->f_bavail |
  106. st->f_bsize | st->f_frsize) &
  107. 0xffffffff00000000ULL)
  108. return -EOVERFLOW;
  109. /*
  110. * f_files and f_ffree may be -1; it's okay to stuff
  111. * that into 32 bits
  112. */
  113. if (st->f_files != -1 &&
  114. (st->f_files & 0xffffffff00000000ULL))
  115. return -EOVERFLOW;
  116. if (st->f_ffree != -1 &&
  117. (st->f_ffree & 0xffffffff00000000ULL))
  118. return -EOVERFLOW;
  119. }
  120. buf.f_type = st->f_type;
  121. buf.f_bsize = st->f_bsize;
  122. buf.f_blocks = st->f_blocks;
  123. buf.f_bfree = st->f_bfree;
  124. buf.f_bavail = st->f_bavail;
  125. buf.f_files = st->f_files;
  126. buf.f_ffree = st->f_ffree;
  127. buf.f_fsid = st->f_fsid;
  128. buf.f_namelen = st->f_namelen;
  129. buf.f_frsize = st->f_frsize;
  130. buf.f_flags = st->f_flags;
  131. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  132. }
  133. if (copy_to_user(p, &buf, sizeof(buf)))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
  138. {
  139. struct statfs64 buf;
  140. if (sizeof(buf) == sizeof(*st))
  141. memcpy(&buf, st, sizeof(*st));
  142. else {
  143. buf.f_type = st->f_type;
  144. buf.f_bsize = st->f_bsize;
  145. buf.f_blocks = st->f_blocks;
  146. buf.f_bfree = st->f_bfree;
  147. buf.f_bavail = st->f_bavail;
  148. buf.f_files = st->f_files;
  149. buf.f_ffree = st->f_ffree;
  150. buf.f_fsid = st->f_fsid;
  151. buf.f_namelen = st->f_namelen;
  152. buf.f_frsize = st->f_frsize;
  153. buf.f_flags = st->f_flags;
  154. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  155. }
  156. if (copy_to_user(p, &buf, sizeof(buf)))
  157. return -EFAULT;
  158. return 0;
  159. }
  160. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  161. {
  162. struct kstatfs st;
  163. int error = user_statfs(pathname, &st);
  164. if (!error)
  165. error = do_statfs_native(&st, buf);
  166. return error;
  167. }
  168. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  169. {
  170. struct kstatfs st;
  171. int error;
  172. if (sz != sizeof(*buf))
  173. return -EINVAL;
  174. error = user_statfs(pathname, &st);
  175. if (!error)
  176. error = do_statfs64(&st, buf);
  177. return error;
  178. }
  179. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  180. {
  181. struct kstatfs st;
  182. int error = fd_statfs(fd, &st);
  183. if (!error)
  184. error = do_statfs_native(&st, buf);
  185. return error;
  186. }
  187. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  188. {
  189. struct kstatfs st;
  190. int error;
  191. if (sz != sizeof(*buf))
  192. return -EINVAL;
  193. error = fd_statfs(fd, &st);
  194. if (!error)
  195. error = do_statfs64(&st, buf);
  196. return error;
  197. }
  198. static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
  199. {
  200. struct super_block *s = user_get_super(dev);
  201. int err;
  202. if (!s)
  203. return -EINVAL;
  204. err = statfs_by_dentry(s->s_root, sbuf);
  205. drop_super(s);
  206. return err;
  207. }
  208. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  209. {
  210. struct ustat tmp;
  211. struct kstatfs sbuf;
  212. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  213. if (err)
  214. return err;
  215. memset(&tmp,0,sizeof(struct ustat));
  216. tmp.f_tfree = sbuf.f_bfree;
  217. tmp.f_tinode = sbuf.f_ffree;
  218. return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
  219. }
  220. #ifdef CONFIG_COMPAT
  221. static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
  222. {
  223. struct compat_statfs buf;
  224. if (sizeof ubuf->f_blocks == 4) {
  225. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  226. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  227. return -EOVERFLOW;
  228. /* f_files and f_ffree may be -1; it's okay
  229. * to stuff that into 32 bits */
  230. if (kbuf->f_files != 0xffffffffffffffffULL
  231. && (kbuf->f_files & 0xffffffff00000000ULL))
  232. return -EOVERFLOW;
  233. if (kbuf->f_ffree != 0xffffffffffffffffULL
  234. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  235. return -EOVERFLOW;
  236. }
  237. memset(&buf, 0, sizeof(struct compat_statfs));
  238. buf.f_type = kbuf->f_type;
  239. buf.f_bsize = kbuf->f_bsize;
  240. buf.f_blocks = kbuf->f_blocks;
  241. buf.f_bfree = kbuf->f_bfree;
  242. buf.f_bavail = kbuf->f_bavail;
  243. buf.f_files = kbuf->f_files;
  244. buf.f_ffree = kbuf->f_ffree;
  245. buf.f_namelen = kbuf->f_namelen;
  246. buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
  247. buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
  248. buf.f_frsize = kbuf->f_frsize;
  249. buf.f_flags = kbuf->f_flags;
  250. if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
  251. return -EFAULT;
  252. return 0;
  253. }
  254. /*
  255. * The following statfs calls are copies of code from fs/statfs.c and
  256. * should be checked against those from time to time
  257. */
  258. COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
  259. {
  260. struct kstatfs tmp;
  261. int error = user_statfs(pathname, &tmp);
  262. if (!error)
  263. error = put_compat_statfs(buf, &tmp);
  264. return error;
  265. }
  266. COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
  267. {
  268. struct kstatfs tmp;
  269. int error = fd_statfs(fd, &tmp);
  270. if (!error)
  271. error = put_compat_statfs(buf, &tmp);
  272. return error;
  273. }
  274. static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
  275. {
  276. struct compat_statfs64 buf;
  277. if ((kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  278. return -EOVERFLOW;
  279. memset(&buf, 0, sizeof(struct compat_statfs64));
  280. buf.f_type = kbuf->f_type;
  281. buf.f_bsize = kbuf->f_bsize;
  282. buf.f_blocks = kbuf->f_blocks;
  283. buf.f_bfree = kbuf->f_bfree;
  284. buf.f_bavail = kbuf->f_bavail;
  285. buf.f_files = kbuf->f_files;
  286. buf.f_ffree = kbuf->f_ffree;
  287. buf.f_namelen = kbuf->f_namelen;
  288. buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
  289. buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
  290. buf.f_frsize = kbuf->f_frsize;
  291. buf.f_flags = kbuf->f_flags;
  292. if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
  293. return -EFAULT;
  294. return 0;
  295. }
  296. int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
  297. {
  298. struct kstatfs tmp;
  299. int error;
  300. if (sz != sizeof(*buf))
  301. return -EINVAL;
  302. error = user_statfs(pathname, &tmp);
  303. if (!error)
  304. error = put_compat_statfs64(buf, &tmp);
  305. return error;
  306. }
  307. COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  308. {
  309. return kcompat_sys_statfs64(pathname, sz, buf);
  310. }
  311. int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user * buf)
  312. {
  313. struct kstatfs tmp;
  314. int error;
  315. if (sz != sizeof(*buf))
  316. return -EINVAL;
  317. error = fd_statfs(fd, &tmp);
  318. if (!error)
  319. error = put_compat_statfs64(buf, &tmp);
  320. return error;
  321. }
  322. COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  323. {
  324. return kcompat_sys_fstatfs64(fd, sz, buf);
  325. }
  326. /*
  327. * This is a copy of sys_ustat, just dealing with a structure layout.
  328. * Given how simple this syscall is that apporach is more maintainable
  329. * than the various conversion hacks.
  330. */
  331. COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
  332. {
  333. struct compat_ustat tmp;
  334. struct kstatfs sbuf;
  335. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  336. if (err)
  337. return err;
  338. memset(&tmp, 0, sizeof(struct compat_ustat));
  339. tmp.f_tfree = sbuf.f_bfree;
  340. tmp.f_tinode = sbuf.f_ffree;
  341. if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
  342. return -EFAULT;
  343. return 0;
  344. }
  345. #endif