compat.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*
  2. * linux/fs/compat.c
  3. *
  4. * Kernel compatibililty routines for e.g. 32 bit syscall support
  5. * on 64 bit kernels.
  6. *
  7. * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
  8. * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
  9. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  10. * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
  11. * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/stddef.h>
  18. #include <linux/kernel.h>
  19. #include <linux/linkage.h>
  20. #include <linux/compat.h>
  21. #include <linux/errno.h>
  22. #include <linux/time.h>
  23. #include <linux/cred.h>
  24. #include <linux/fs.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/namei.h>
  27. #include <linux/file.h>
  28. #include <linux/fdtable.h>
  29. #include <linux/vfs.h>
  30. #include <linux/ioctl.h>
  31. #include <linux/init.h>
  32. #include <linux/ncp_mount.h>
  33. #include <linux/nfs4_mount.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/ctype.h>
  36. #include <linux/dirent.h>
  37. #include <linux/fsnotify.h>
  38. #include <linux/highuid.h>
  39. #include <linux/personality.h>
  40. #include <linux/rwsem.h>
  41. #include <linux/tsacct_kern.h>
  42. #include <linux/security.h>
  43. #include <linux/highmem.h>
  44. #include <linux/signal.h>
  45. #include <linux/poll.h>
  46. #include <linux/mm.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/slab.h>
  49. #include <linux/pagemap.h>
  50. #include <linux/aio.h>
  51. #include <linux/uaccess.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/ioctls.h>
  54. #include "internal.h"
  55. /*
  56. * Not all architectures have sys_utime, so implement this in terms
  57. * of sys_utimes.
  58. */
  59. COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
  60. struct compat_utimbuf __user *, t)
  61. {
  62. struct timespec tv[2];
  63. if (t) {
  64. if (get_user(tv[0].tv_sec, &t->actime) ||
  65. get_user(tv[1].tv_sec, &t->modtime))
  66. return -EFAULT;
  67. tv[0].tv_nsec = 0;
  68. tv[1].tv_nsec = 0;
  69. }
  70. return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  71. }
  72. COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
  73. {
  74. struct timespec tv[2];
  75. if (t) {
  76. if (compat_get_timespec(&tv[0], &t[0]) ||
  77. compat_get_timespec(&tv[1], &t[1]))
  78. return -EFAULT;
  79. if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
  80. return 0;
  81. }
  82. return do_utimes(dfd, filename, t ? tv : NULL, flags);
  83. }
  84. COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
  85. {
  86. struct timespec tv[2];
  87. if (t) {
  88. if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
  89. get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
  90. get_user(tv[1].tv_sec, &t[1].tv_sec) ||
  91. get_user(tv[1].tv_nsec, &t[1].tv_usec))
  92. return -EFAULT;
  93. if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
  94. tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
  95. return -EINVAL;
  96. tv[0].tv_nsec *= 1000;
  97. tv[1].tv_nsec *= 1000;
  98. }
  99. return do_utimes(dfd, filename, t ? tv : NULL, 0);
  100. }
  101. COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t)
  102. {
  103. return compat_sys_futimesat(AT_FDCWD, filename, t);
  104. }
  105. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  106. {
  107. struct compat_stat tmp;
  108. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  109. return -EOVERFLOW;
  110. memset(&tmp, 0, sizeof(tmp));
  111. tmp.st_dev = old_encode_dev(stat->dev);
  112. tmp.st_ino = stat->ino;
  113. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  114. return -EOVERFLOW;
  115. tmp.st_mode = stat->mode;
  116. tmp.st_nlink = stat->nlink;
  117. if (tmp.st_nlink != stat->nlink)
  118. return -EOVERFLOW;
  119. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  120. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  121. tmp.st_rdev = old_encode_dev(stat->rdev);
  122. if ((u64) stat->size > MAX_NON_LFS)
  123. return -EOVERFLOW;
  124. tmp.st_size = stat->size;
  125. tmp.st_atime = stat->atime.tv_sec;
  126. tmp.st_atime_nsec = stat->atime.tv_nsec;
  127. tmp.st_mtime = stat->mtime.tv_sec;
  128. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  129. tmp.st_ctime = stat->ctime.tv_sec;
  130. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  131. tmp.st_blocks = stat->blocks;
  132. tmp.st_blksize = stat->blksize;
  133. return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  134. }
  135. COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
  136. struct compat_stat __user *, statbuf)
  137. {
  138. struct kstat stat;
  139. int error;
  140. error = vfs_stat(filename, &stat);
  141. if (error)
  142. return error;
  143. return cp_compat_stat(&stat, statbuf);
  144. }
  145. COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  146. struct compat_stat __user *, statbuf)
  147. {
  148. struct kstat stat;
  149. int error;
  150. error = vfs_lstat(filename, &stat);
  151. if (error)
  152. return error;
  153. return cp_compat_stat(&stat, statbuf);
  154. }
  155. #ifndef __ARCH_WANT_STAT64
  156. COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
  157. const char __user *, filename,
  158. struct compat_stat __user *, statbuf, int, flag)
  159. {
  160. struct kstat stat;
  161. int error;
  162. error = vfs_fstatat(dfd, filename, &stat, flag);
  163. if (error)
  164. return error;
  165. return cp_compat_stat(&stat, statbuf);
  166. }
  167. #endif
  168. COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
  169. struct compat_stat __user *, statbuf)
  170. {
  171. struct kstat stat;
  172. int error = vfs_fstat(fd, &stat);
  173. if (!error)
  174. error = cp_compat_stat(&stat, statbuf);
  175. return error;
  176. }
  177. static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
  178. {
  179. if (sizeof ubuf->f_blocks == 4) {
  180. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  181. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  182. return -EOVERFLOW;
  183. /* f_files and f_ffree may be -1; it's okay
  184. * to stuff that into 32 bits */
  185. if (kbuf->f_files != 0xffffffffffffffffULL
  186. && (kbuf->f_files & 0xffffffff00000000ULL))
  187. return -EOVERFLOW;
  188. if (kbuf->f_ffree != 0xffffffffffffffffULL
  189. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  190. return -EOVERFLOW;
  191. }
  192. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  193. __put_user(kbuf->f_type, &ubuf->f_type) ||
  194. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  195. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  196. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  197. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  198. __put_user(kbuf->f_files, &ubuf->f_files) ||
  199. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  200. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  201. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  202. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  203. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  204. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  205. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  206. return -EFAULT;
  207. return 0;
  208. }
  209. /*
  210. * The following statfs calls are copies of code from fs/statfs.c and
  211. * should be checked against those from time to time
  212. */
  213. COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
  214. {
  215. struct kstatfs tmp;
  216. int error = user_statfs(pathname, &tmp);
  217. if (!error)
  218. error = put_compat_statfs(buf, &tmp);
  219. return error;
  220. }
  221. COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
  222. {
  223. struct kstatfs tmp;
  224. int error = fd_statfs(fd, &tmp);
  225. if (!error)
  226. error = put_compat_statfs(buf, &tmp);
  227. return error;
  228. }
  229. static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
  230. {
  231. if (sizeof(ubuf->f_bsize) == 4) {
  232. if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
  233. kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
  234. return -EOVERFLOW;
  235. /* f_files and f_ffree may be -1; it's okay
  236. * to stuff that into 32 bits */
  237. if (kbuf->f_files != 0xffffffffffffffffULL
  238. && (kbuf->f_files & 0xffffffff00000000ULL))
  239. return -EOVERFLOW;
  240. if (kbuf->f_ffree != 0xffffffffffffffffULL
  241. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  242. return -EOVERFLOW;
  243. }
  244. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  245. __put_user(kbuf->f_type, &ubuf->f_type) ||
  246. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  247. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  248. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  249. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  250. __put_user(kbuf->f_files, &ubuf->f_files) ||
  251. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  252. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  253. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  254. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  255. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  256. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  257. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  258. return -EFAULT;
  259. return 0;
  260. }
  261. COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  262. {
  263. struct kstatfs tmp;
  264. int error;
  265. if (sz != sizeof(*buf))
  266. return -EINVAL;
  267. error = user_statfs(pathname, &tmp);
  268. if (!error)
  269. error = put_compat_statfs64(buf, &tmp);
  270. return error;
  271. }
  272. COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  273. {
  274. struct kstatfs tmp;
  275. int error;
  276. if (sz != sizeof(*buf))
  277. return -EINVAL;
  278. error = fd_statfs(fd, &tmp);
  279. if (!error)
  280. error = put_compat_statfs64(buf, &tmp);
  281. return error;
  282. }
  283. /*
  284. * This is a copy of sys_ustat, just dealing with a structure layout.
  285. * Given how simple this syscall is that apporach is more maintainable
  286. * than the various conversion hacks.
  287. */
  288. COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
  289. {
  290. struct compat_ustat tmp;
  291. struct kstatfs sbuf;
  292. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  293. if (err)
  294. return err;
  295. memset(&tmp, 0, sizeof(struct compat_ustat));
  296. tmp.f_tfree = sbuf.f_bfree;
  297. tmp.f_tinode = sbuf.f_ffree;
  298. if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
  299. return -EFAULT;
  300. return 0;
  301. }
  302. static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  303. {
  304. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  305. __get_user(kfl->l_type, &ufl->l_type) ||
  306. __get_user(kfl->l_whence, &ufl->l_whence) ||
  307. __get_user(kfl->l_start, &ufl->l_start) ||
  308. __get_user(kfl->l_len, &ufl->l_len) ||
  309. __get_user(kfl->l_pid, &ufl->l_pid))
  310. return -EFAULT;
  311. return 0;
  312. }
  313. static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  314. {
  315. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  316. __put_user(kfl->l_type, &ufl->l_type) ||
  317. __put_user(kfl->l_whence, &ufl->l_whence) ||
  318. __put_user(kfl->l_start, &ufl->l_start) ||
  319. __put_user(kfl->l_len, &ufl->l_len) ||
  320. __put_user(kfl->l_pid, &ufl->l_pid))
  321. return -EFAULT;
  322. return 0;
  323. }
  324. #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
  325. static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  326. {
  327. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  328. __get_user(kfl->l_type, &ufl->l_type) ||
  329. __get_user(kfl->l_whence, &ufl->l_whence) ||
  330. __get_user(kfl->l_start, &ufl->l_start) ||
  331. __get_user(kfl->l_len, &ufl->l_len) ||
  332. __get_user(kfl->l_pid, &ufl->l_pid))
  333. return -EFAULT;
  334. return 0;
  335. }
  336. #endif
  337. #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
  338. static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  339. {
  340. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  341. __put_user(kfl->l_type, &ufl->l_type) ||
  342. __put_user(kfl->l_whence, &ufl->l_whence) ||
  343. __put_user(kfl->l_start, &ufl->l_start) ||
  344. __put_user(kfl->l_len, &ufl->l_len) ||
  345. __put_user(kfl->l_pid, &ufl->l_pid))
  346. return -EFAULT;
  347. return 0;
  348. }
  349. #endif
  350. static unsigned int
  351. convert_fcntl_cmd(unsigned int cmd)
  352. {
  353. switch (cmd) {
  354. case F_GETLK64:
  355. return F_GETLK;
  356. case F_SETLK64:
  357. return F_SETLK;
  358. case F_SETLKW64:
  359. return F_SETLKW;
  360. }
  361. return cmd;
  362. }
  363. COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  364. compat_ulong_t, arg)
  365. {
  366. mm_segment_t old_fs;
  367. struct flock f;
  368. long ret;
  369. unsigned int conv_cmd;
  370. switch (cmd) {
  371. case F_GETLK:
  372. case F_SETLK:
  373. case F_SETLKW:
  374. ret = get_compat_flock(&f, compat_ptr(arg));
  375. if (ret != 0)
  376. break;
  377. old_fs = get_fs();
  378. set_fs(KERNEL_DS);
  379. ret = sys_fcntl(fd, cmd, (unsigned long)&f);
  380. set_fs(old_fs);
  381. if (cmd == F_GETLK && ret == 0) {
  382. /* GETLK was successful and we need to return the data...
  383. * but it needs to fit in the compat structure.
  384. * l_start shouldn't be too big, unless the original
  385. * start + end is greater than COMPAT_OFF_T_MAX, in which
  386. * case the app was asking for trouble, so we return
  387. * -EOVERFLOW in that case.
  388. * l_len could be too big, in which case we just truncate it,
  389. * and only allow the app to see that part of the conflicting
  390. * lock that might make sense to it anyway
  391. */
  392. if (f.l_start > COMPAT_OFF_T_MAX)
  393. ret = -EOVERFLOW;
  394. if (f.l_len > COMPAT_OFF_T_MAX)
  395. f.l_len = COMPAT_OFF_T_MAX;
  396. if (ret == 0)
  397. ret = put_compat_flock(&f, compat_ptr(arg));
  398. }
  399. break;
  400. case F_GETLK64:
  401. case F_SETLK64:
  402. case F_SETLKW64:
  403. case F_OFD_GETLK:
  404. case F_OFD_SETLK:
  405. case F_OFD_SETLKW:
  406. ret = get_compat_flock64(&f, compat_ptr(arg));
  407. if (ret != 0)
  408. break;
  409. old_fs = get_fs();
  410. set_fs(KERNEL_DS);
  411. conv_cmd = convert_fcntl_cmd(cmd);
  412. ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
  413. set_fs(old_fs);
  414. if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
  415. /* need to return lock information - see above for commentary */
  416. if (f.l_start > COMPAT_LOFF_T_MAX)
  417. ret = -EOVERFLOW;
  418. if (f.l_len > COMPAT_LOFF_T_MAX)
  419. f.l_len = COMPAT_LOFF_T_MAX;
  420. if (ret == 0)
  421. ret = put_compat_flock64(&f, compat_ptr(arg));
  422. }
  423. break;
  424. default:
  425. ret = sys_fcntl(fd, cmd, arg);
  426. break;
  427. }
  428. return ret;
  429. }
  430. COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
  431. compat_ulong_t, arg)
  432. {
  433. switch (cmd) {
  434. case F_GETLK64:
  435. case F_SETLK64:
  436. case F_SETLKW64:
  437. case F_OFD_GETLK:
  438. case F_OFD_SETLK:
  439. case F_OFD_SETLKW:
  440. return -EINVAL;
  441. }
  442. return compat_sys_fcntl64(fd, cmd, arg);
  443. }
  444. /* A write operation does a read from user space and vice versa */
  445. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  446. ssize_t compat_rw_copy_check_uvector(int type,
  447. const struct compat_iovec __user *uvector, unsigned long nr_segs,
  448. unsigned long fast_segs, struct iovec *fast_pointer,
  449. struct iovec **ret_pointer)
  450. {
  451. compat_ssize_t tot_len;
  452. struct iovec *iov = *ret_pointer = fast_pointer;
  453. ssize_t ret = 0;
  454. int seg;
  455. /*
  456. * SuS says "The readv() function *may* fail if the iovcnt argument
  457. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  458. * traditionally returned zero for zero segments, so...
  459. */
  460. if (nr_segs == 0)
  461. goto out;
  462. ret = -EINVAL;
  463. if (nr_segs > UIO_MAXIOV)
  464. goto out;
  465. if (nr_segs > fast_segs) {
  466. ret = -ENOMEM;
  467. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  468. if (iov == NULL)
  469. goto out;
  470. }
  471. *ret_pointer = iov;
  472. ret = -EFAULT;
  473. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  474. goto out;
  475. /*
  476. * Single unix specification:
  477. * We should -EINVAL if an element length is not >= 0 and fitting an
  478. * ssize_t.
  479. *
  480. * In Linux, the total length is limited to MAX_RW_COUNT, there is
  481. * no overflow possibility.
  482. */
  483. tot_len = 0;
  484. ret = -EINVAL;
  485. for (seg = 0; seg < nr_segs; seg++) {
  486. compat_uptr_t buf;
  487. compat_ssize_t len;
  488. if (__get_user(len, &uvector->iov_len) ||
  489. __get_user(buf, &uvector->iov_base)) {
  490. ret = -EFAULT;
  491. goto out;
  492. }
  493. if (len < 0) /* size_t not fitting in compat_ssize_t .. */
  494. goto out;
  495. if (type >= 0 &&
  496. !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
  497. ret = -EFAULT;
  498. goto out;
  499. }
  500. if (len > MAX_RW_COUNT - tot_len)
  501. len = MAX_RW_COUNT - tot_len;
  502. tot_len += len;
  503. iov->iov_base = compat_ptr(buf);
  504. iov->iov_len = (compat_size_t) len;
  505. uvector++;
  506. iov++;
  507. }
  508. ret = tot_len;
  509. out:
  510. return ret;
  511. }
  512. struct compat_ncp_mount_data {
  513. compat_int_t version;
  514. compat_uint_t ncp_fd;
  515. __compat_uid_t mounted_uid;
  516. compat_pid_t wdog_pid;
  517. unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
  518. compat_uint_t time_out;
  519. compat_uint_t retry_count;
  520. compat_uint_t flags;
  521. __compat_uid_t uid;
  522. __compat_gid_t gid;
  523. compat_mode_t file_mode;
  524. compat_mode_t dir_mode;
  525. };
  526. struct compat_ncp_mount_data_v4 {
  527. compat_int_t version;
  528. compat_ulong_t flags;
  529. compat_ulong_t mounted_uid;
  530. compat_long_t wdog_pid;
  531. compat_uint_t ncp_fd;
  532. compat_uint_t time_out;
  533. compat_uint_t retry_count;
  534. compat_ulong_t uid;
  535. compat_ulong_t gid;
  536. compat_ulong_t file_mode;
  537. compat_ulong_t dir_mode;
  538. };
  539. static void *do_ncp_super_data_conv(void *raw_data)
  540. {
  541. int version = *(unsigned int *)raw_data;
  542. if (version == 3) {
  543. struct compat_ncp_mount_data *c_n = raw_data;
  544. struct ncp_mount_data *n = raw_data;
  545. n->dir_mode = c_n->dir_mode;
  546. n->file_mode = c_n->file_mode;
  547. n->gid = c_n->gid;
  548. n->uid = c_n->uid;
  549. memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
  550. n->wdog_pid = c_n->wdog_pid;
  551. n->mounted_uid = c_n->mounted_uid;
  552. } else if (version == 4) {
  553. struct compat_ncp_mount_data_v4 *c_n = raw_data;
  554. struct ncp_mount_data_v4 *n = raw_data;
  555. n->dir_mode = c_n->dir_mode;
  556. n->file_mode = c_n->file_mode;
  557. n->gid = c_n->gid;
  558. n->uid = c_n->uid;
  559. n->retry_count = c_n->retry_count;
  560. n->time_out = c_n->time_out;
  561. n->ncp_fd = c_n->ncp_fd;
  562. n->wdog_pid = c_n->wdog_pid;
  563. n->mounted_uid = c_n->mounted_uid;
  564. n->flags = c_n->flags;
  565. } else if (version != 5) {
  566. return NULL;
  567. }
  568. return raw_data;
  569. }
  570. struct compat_nfs_string {
  571. compat_uint_t len;
  572. compat_uptr_t data;
  573. };
  574. static inline void compat_nfs_string(struct nfs_string *dst,
  575. struct compat_nfs_string *src)
  576. {
  577. dst->data = compat_ptr(src->data);
  578. dst->len = src->len;
  579. }
  580. struct compat_nfs4_mount_data_v1 {
  581. compat_int_t version;
  582. compat_int_t flags;
  583. compat_int_t rsize;
  584. compat_int_t wsize;
  585. compat_int_t timeo;
  586. compat_int_t retrans;
  587. compat_int_t acregmin;
  588. compat_int_t acregmax;
  589. compat_int_t acdirmin;
  590. compat_int_t acdirmax;
  591. struct compat_nfs_string client_addr;
  592. struct compat_nfs_string mnt_path;
  593. struct compat_nfs_string hostname;
  594. compat_uint_t host_addrlen;
  595. compat_uptr_t host_addr;
  596. compat_int_t proto;
  597. compat_int_t auth_flavourlen;
  598. compat_uptr_t auth_flavours;
  599. };
  600. static int do_nfs4_super_data_conv(void *raw_data)
  601. {
  602. int version = *(compat_uint_t *) raw_data;
  603. if (version == 1) {
  604. struct compat_nfs4_mount_data_v1 *raw = raw_data;
  605. struct nfs4_mount_data *real = raw_data;
  606. /* copy the fields backwards */
  607. real->auth_flavours = compat_ptr(raw->auth_flavours);
  608. real->auth_flavourlen = raw->auth_flavourlen;
  609. real->proto = raw->proto;
  610. real->host_addr = compat_ptr(raw->host_addr);
  611. real->host_addrlen = raw->host_addrlen;
  612. compat_nfs_string(&real->hostname, &raw->hostname);
  613. compat_nfs_string(&real->mnt_path, &raw->mnt_path);
  614. compat_nfs_string(&real->client_addr, &raw->client_addr);
  615. real->acdirmax = raw->acdirmax;
  616. real->acdirmin = raw->acdirmin;
  617. real->acregmax = raw->acregmax;
  618. real->acregmin = raw->acregmin;
  619. real->retrans = raw->retrans;
  620. real->timeo = raw->timeo;
  621. real->wsize = raw->wsize;
  622. real->rsize = raw->rsize;
  623. real->flags = raw->flags;
  624. real->version = raw->version;
  625. }
  626. return 0;
  627. }
  628. #define NCPFS_NAME "ncpfs"
  629. #define NFS4_NAME "nfs4"
  630. COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
  631. const char __user *, dir_name,
  632. const char __user *, type, compat_ulong_t, flags,
  633. const void __user *, data)
  634. {
  635. char *kernel_type;
  636. void *options;
  637. char *kernel_dev;
  638. int retval;
  639. kernel_type = copy_mount_string(type);
  640. retval = PTR_ERR(kernel_type);
  641. if (IS_ERR(kernel_type))
  642. goto out;
  643. kernel_dev = copy_mount_string(dev_name);
  644. retval = PTR_ERR(kernel_dev);
  645. if (IS_ERR(kernel_dev))
  646. goto out1;
  647. options = copy_mount_options(data);
  648. retval = PTR_ERR(options);
  649. if (IS_ERR(options))
  650. goto out2;
  651. if (kernel_type && options) {
  652. if (!strcmp(kernel_type, NCPFS_NAME)) {
  653. do_ncp_super_data_conv(options);
  654. } else if (!strcmp(kernel_type, NFS4_NAME)) {
  655. retval = -EINVAL;
  656. if (do_nfs4_super_data_conv(options))
  657. goto out3;
  658. }
  659. }
  660. retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  661. out3:
  662. kfree(options);
  663. out2:
  664. kfree(kernel_dev);
  665. out1:
  666. kfree(kernel_type);
  667. out:
  668. return retval;
  669. }
  670. struct compat_old_linux_dirent {
  671. compat_ulong_t d_ino;
  672. compat_ulong_t d_offset;
  673. unsigned short d_namlen;
  674. char d_name[1];
  675. };
  676. struct compat_readdir_callback {
  677. struct dir_context ctx;
  678. struct compat_old_linux_dirent __user *dirent;
  679. int result;
  680. };
  681. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  682. int namlen, loff_t offset, u64 ino,
  683. unsigned int d_type)
  684. {
  685. struct compat_readdir_callback *buf =
  686. container_of(ctx, struct compat_readdir_callback, ctx);
  687. struct compat_old_linux_dirent __user *dirent;
  688. compat_ulong_t d_ino;
  689. if (buf->result)
  690. return -EINVAL;
  691. d_ino = ino;
  692. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  693. buf->result = -EOVERFLOW;
  694. return -EOVERFLOW;
  695. }
  696. buf->result++;
  697. dirent = buf->dirent;
  698. if (!access_ok(VERIFY_WRITE, dirent,
  699. (unsigned long)(dirent->d_name + namlen + 1) -
  700. (unsigned long)dirent))
  701. goto efault;
  702. if ( __put_user(d_ino, &dirent->d_ino) ||
  703. __put_user(offset, &dirent->d_offset) ||
  704. __put_user(namlen, &dirent->d_namlen) ||
  705. __copy_to_user(dirent->d_name, name, namlen) ||
  706. __put_user(0, dirent->d_name + namlen))
  707. goto efault;
  708. return 0;
  709. efault:
  710. buf->result = -EFAULT;
  711. return -EFAULT;
  712. }
  713. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  714. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  715. {
  716. int error;
  717. struct fd f = fdget_pos(fd);
  718. struct compat_readdir_callback buf = {
  719. .ctx.actor = compat_fillonedir,
  720. .dirent = dirent
  721. };
  722. if (!f.file)
  723. return -EBADF;
  724. error = iterate_dir(f.file, &buf.ctx);
  725. if (buf.result)
  726. error = buf.result;
  727. fdput_pos(f);
  728. return error;
  729. }
  730. struct compat_linux_dirent {
  731. compat_ulong_t d_ino;
  732. compat_ulong_t d_off;
  733. unsigned short d_reclen;
  734. char d_name[1];
  735. };
  736. struct compat_getdents_callback {
  737. struct dir_context ctx;
  738. struct compat_linux_dirent __user *current_dir;
  739. struct compat_linux_dirent __user *previous;
  740. int count;
  741. int error;
  742. };
  743. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  744. loff_t offset, u64 ino, unsigned int d_type)
  745. {
  746. struct compat_linux_dirent __user * dirent;
  747. struct compat_getdents_callback *buf =
  748. container_of(ctx, struct compat_getdents_callback, ctx);
  749. compat_ulong_t d_ino;
  750. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  751. namlen + 2, sizeof(compat_long_t));
  752. buf->error = -EINVAL; /* only used if we fail.. */
  753. if (reclen > buf->count)
  754. return -EINVAL;
  755. d_ino = ino;
  756. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  757. buf->error = -EOVERFLOW;
  758. return -EOVERFLOW;
  759. }
  760. dirent = buf->previous;
  761. if (dirent) {
  762. if (signal_pending(current))
  763. return -EINTR;
  764. if (__put_user(offset, &dirent->d_off))
  765. goto efault;
  766. }
  767. dirent = buf->current_dir;
  768. if (__put_user(d_ino, &dirent->d_ino))
  769. goto efault;
  770. if (__put_user(reclen, &dirent->d_reclen))
  771. goto efault;
  772. if (copy_to_user(dirent->d_name, name, namlen))
  773. goto efault;
  774. if (__put_user(0, dirent->d_name + namlen))
  775. goto efault;
  776. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  777. goto efault;
  778. buf->previous = dirent;
  779. dirent = (void __user *)dirent + reclen;
  780. buf->current_dir = dirent;
  781. buf->count -= reclen;
  782. return 0;
  783. efault:
  784. buf->error = -EFAULT;
  785. return -EFAULT;
  786. }
  787. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  788. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  789. {
  790. struct fd f;
  791. struct compat_linux_dirent __user * lastdirent;
  792. struct compat_getdents_callback buf = {
  793. .ctx.actor = compat_filldir,
  794. .current_dir = dirent,
  795. .count = count
  796. };
  797. int error;
  798. if (!access_ok(VERIFY_WRITE, dirent, count))
  799. return -EFAULT;
  800. f = fdget_pos(fd);
  801. if (!f.file)
  802. return -EBADF;
  803. error = iterate_dir(f.file, &buf.ctx);
  804. if (error >= 0)
  805. error = buf.error;
  806. lastdirent = buf.previous;
  807. if (lastdirent) {
  808. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  809. error = -EFAULT;
  810. else
  811. error = count - buf.count;
  812. }
  813. fdput_pos(f);
  814. return error;
  815. }
  816. #ifdef __ARCH_WANT_COMPAT_SYS_GETDENTS64
  817. struct compat_getdents_callback64 {
  818. struct dir_context ctx;
  819. struct linux_dirent64 __user *current_dir;
  820. struct linux_dirent64 __user *previous;
  821. int count;
  822. int error;
  823. };
  824. static int compat_filldir64(struct dir_context *ctx, const char *name,
  825. int namlen, loff_t offset, u64 ino,
  826. unsigned int d_type)
  827. {
  828. struct linux_dirent64 __user *dirent;
  829. struct compat_getdents_callback64 *buf =
  830. container_of(ctx, struct compat_getdents_callback64, ctx);
  831. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  832. sizeof(u64));
  833. u64 off;
  834. buf->error = -EINVAL; /* only used if we fail.. */
  835. if (reclen > buf->count)
  836. return -EINVAL;
  837. dirent = buf->previous;
  838. if (dirent) {
  839. if (signal_pending(current))
  840. return -EINTR;
  841. if (__put_user_unaligned(offset, &dirent->d_off))
  842. goto efault;
  843. }
  844. dirent = buf->current_dir;
  845. if (__put_user_unaligned(ino, &dirent->d_ino))
  846. goto efault;
  847. off = 0;
  848. if (__put_user_unaligned(off, &dirent->d_off))
  849. goto efault;
  850. if (__put_user(reclen, &dirent->d_reclen))
  851. goto efault;
  852. if (__put_user(d_type, &dirent->d_type))
  853. goto efault;
  854. if (copy_to_user(dirent->d_name, name, namlen))
  855. goto efault;
  856. if (__put_user(0, dirent->d_name + namlen))
  857. goto efault;
  858. buf->previous = dirent;
  859. dirent = (void __user *)dirent + reclen;
  860. buf->current_dir = dirent;
  861. buf->count -= reclen;
  862. return 0;
  863. efault:
  864. buf->error = -EFAULT;
  865. return -EFAULT;
  866. }
  867. COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  868. struct linux_dirent64 __user *, dirent, unsigned int, count)
  869. {
  870. struct fd f;
  871. struct linux_dirent64 __user * lastdirent;
  872. struct compat_getdents_callback64 buf = {
  873. .ctx.actor = compat_filldir64,
  874. .current_dir = dirent,
  875. .count = count
  876. };
  877. int error;
  878. if (!access_ok(VERIFY_WRITE, dirent, count))
  879. return -EFAULT;
  880. f = fdget_pos(fd);
  881. if (!f.file)
  882. return -EBADF;
  883. error = iterate_dir(f.file, &buf.ctx);
  884. if (error >= 0)
  885. error = buf.error;
  886. lastdirent = buf.previous;
  887. if (lastdirent) {
  888. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  889. if (__put_user_unaligned(d_off, &lastdirent->d_off))
  890. error = -EFAULT;
  891. else
  892. error = count - buf.count;
  893. }
  894. fdput_pos(f);
  895. return error;
  896. }
  897. #endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */
  898. /*
  899. * Exactly like fs/open.c:sys_open(), except that it doesn't set the
  900. * O_LARGEFILE flag.
  901. */
  902. COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  903. {
  904. return do_sys_open(AT_FDCWD, filename, flags, mode);
  905. }
  906. /*
  907. * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
  908. * O_LARGEFILE flag.
  909. */
  910. COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
  911. {
  912. return do_sys_open(dfd, filename, flags, mode);
  913. }
  914. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  915. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  916. int timeval, int ret)
  917. {
  918. struct timespec ts;
  919. if (!p)
  920. return ret;
  921. if (current->personality & STICKY_TIMEOUTS)
  922. goto sticky;
  923. /* No update for zero timeout */
  924. if (!end_time->tv_sec && !end_time->tv_nsec)
  925. return ret;
  926. ktime_get_ts(&ts);
  927. ts = timespec_sub(*end_time, ts);
  928. if (ts.tv_sec < 0)
  929. ts.tv_sec = ts.tv_nsec = 0;
  930. if (timeval) {
  931. struct compat_timeval rtv;
  932. rtv.tv_sec = ts.tv_sec;
  933. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  934. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  935. return ret;
  936. } else {
  937. struct compat_timespec rts;
  938. rts.tv_sec = ts.tv_sec;
  939. rts.tv_nsec = ts.tv_nsec;
  940. if (!copy_to_user(p, &rts, sizeof(rts)))
  941. return ret;
  942. }
  943. /*
  944. * If an application puts its timeval in read-only memory, we
  945. * don't want the Linux-specific update to the timeval to
  946. * cause a fault after the select has completed
  947. * successfully. However, because we're not updating the
  948. * timeval, we can't restart the system call.
  949. */
  950. sticky:
  951. if (ret == -ERESTARTNOHAND)
  952. ret = -EINTR;
  953. return ret;
  954. }
  955. /*
  956. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  957. * 64-bit unsigned longs.
  958. */
  959. static
  960. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  961. unsigned long *fdset)
  962. {
  963. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  964. if (ufdset) {
  965. unsigned long odd;
  966. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  967. return -EFAULT;
  968. odd = nr & 1UL;
  969. nr &= ~1UL;
  970. while (nr) {
  971. unsigned long h, l;
  972. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  973. return -EFAULT;
  974. ufdset += 2;
  975. *fdset++ = h << 32 | l;
  976. nr -= 2;
  977. }
  978. if (odd && __get_user(*fdset, ufdset))
  979. return -EFAULT;
  980. } else {
  981. /* Tricky, must clear full unsigned long in the
  982. * kernel fdset at the end, this makes sure that
  983. * actually happens.
  984. */
  985. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  986. }
  987. return 0;
  988. }
  989. static
  990. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  991. unsigned long *fdset)
  992. {
  993. unsigned long odd;
  994. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  995. if (!ufdset)
  996. return 0;
  997. odd = nr & 1UL;
  998. nr &= ~1UL;
  999. while (nr) {
  1000. unsigned long h, l;
  1001. l = *fdset++;
  1002. h = l >> 32;
  1003. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1004. return -EFAULT;
  1005. ufdset += 2;
  1006. nr -= 2;
  1007. }
  1008. if (odd && __put_user(*fdset, ufdset))
  1009. return -EFAULT;
  1010. return 0;
  1011. }
  1012. /*
  1013. * This is a virtual copy of sys_select from fs/select.c and probably
  1014. * should be compared to it from time to time
  1015. */
  1016. /*
  1017. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1018. * like to be certain this leads to no problems. So I return
  1019. * EINTR just for safety.
  1020. *
  1021. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1022. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1023. */
  1024. int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1025. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1026. struct timespec *end_time)
  1027. {
  1028. fd_set_bits fds;
  1029. void *bits;
  1030. int size, max_fds, ret = -EINVAL;
  1031. struct fdtable *fdt;
  1032. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1033. if (n < 0)
  1034. goto out_nofds;
  1035. /* max_fds can increase, so grab it once to avoid race */
  1036. rcu_read_lock();
  1037. fdt = files_fdtable(current->files);
  1038. max_fds = fdt->max_fds;
  1039. rcu_read_unlock();
  1040. if (n > max_fds)
  1041. n = max_fds;
  1042. /*
  1043. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1044. * since we used fdset we need to allocate memory in units of
  1045. * long-words.
  1046. */
  1047. size = FDS_BYTES(n);
  1048. bits = stack_fds;
  1049. if (size > sizeof(stack_fds) / 6) {
  1050. bits = kmalloc(6 * size, GFP_KERNEL);
  1051. ret = -ENOMEM;
  1052. if (!bits)
  1053. goto out_nofds;
  1054. }
  1055. fds.in = (unsigned long *) bits;
  1056. fds.out = (unsigned long *) (bits + size);
  1057. fds.ex = (unsigned long *) (bits + 2*size);
  1058. fds.res_in = (unsigned long *) (bits + 3*size);
  1059. fds.res_out = (unsigned long *) (bits + 4*size);
  1060. fds.res_ex = (unsigned long *) (bits + 5*size);
  1061. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1062. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1063. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1064. goto out;
  1065. zero_fd_set(n, fds.res_in);
  1066. zero_fd_set(n, fds.res_out);
  1067. zero_fd_set(n, fds.res_ex);
  1068. ret = do_select(n, &fds, end_time);
  1069. if (ret < 0)
  1070. goto out;
  1071. if (!ret) {
  1072. ret = -ERESTARTNOHAND;
  1073. if (signal_pending(current))
  1074. goto out;
  1075. ret = 0;
  1076. }
  1077. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1078. compat_set_fd_set(n, outp, fds.res_out) ||
  1079. compat_set_fd_set(n, exp, fds.res_ex))
  1080. ret = -EFAULT;
  1081. out:
  1082. if (bits != stack_fds)
  1083. kfree(bits);
  1084. out_nofds:
  1085. return ret;
  1086. }
  1087. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1088. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1089. struct compat_timeval __user *, tvp)
  1090. {
  1091. struct timespec end_time, *to = NULL;
  1092. struct compat_timeval tv;
  1093. int ret;
  1094. if (tvp) {
  1095. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1096. return -EFAULT;
  1097. to = &end_time;
  1098. if (poll_select_set_timeout(to,
  1099. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1100. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1101. return -EINVAL;
  1102. }
  1103. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1104. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1105. return ret;
  1106. }
  1107. struct compat_sel_arg_struct {
  1108. compat_ulong_t n;
  1109. compat_uptr_t inp;
  1110. compat_uptr_t outp;
  1111. compat_uptr_t exp;
  1112. compat_uptr_t tvp;
  1113. };
  1114. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1115. {
  1116. struct compat_sel_arg_struct a;
  1117. if (copy_from_user(&a, arg, sizeof(a)))
  1118. return -EFAULT;
  1119. return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1120. compat_ptr(a.exp), compat_ptr(a.tvp));
  1121. }
  1122. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1123. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1124. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1125. compat_size_t sigsetsize)
  1126. {
  1127. compat_sigset_t ss32;
  1128. sigset_t ksigmask, sigsaved;
  1129. struct compat_timespec ts;
  1130. struct timespec end_time, *to = NULL;
  1131. int ret;
  1132. if (tsp) {
  1133. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1134. return -EFAULT;
  1135. to = &end_time;
  1136. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1137. return -EINVAL;
  1138. }
  1139. if (sigmask) {
  1140. if (sigsetsize != sizeof(compat_sigset_t))
  1141. return -EINVAL;
  1142. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1143. return -EFAULT;
  1144. sigset_from_compat(&ksigmask, &ss32);
  1145. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1146. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1147. }
  1148. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1149. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1150. if (ret == -ERESTARTNOHAND) {
  1151. /*
  1152. * Don't restore the signal mask yet. Let do_signal() deliver
  1153. * the signal on the way back to userspace, before the signal
  1154. * mask is restored.
  1155. */
  1156. if (sigmask) {
  1157. memcpy(&current->saved_sigmask, &sigsaved,
  1158. sizeof(sigsaved));
  1159. set_restore_sigmask();
  1160. }
  1161. } else if (sigmask)
  1162. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1163. return ret;
  1164. }
  1165. COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
  1166. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1167. struct compat_timespec __user *, tsp, void __user *, sig)
  1168. {
  1169. compat_size_t sigsetsize = 0;
  1170. compat_uptr_t up = 0;
  1171. if (sig) {
  1172. if (!access_ok(VERIFY_READ, sig,
  1173. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1174. __get_user(up, (compat_uptr_t __user *)sig) ||
  1175. __get_user(sigsetsize,
  1176. (compat_size_t __user *)(sig+sizeof(up))))
  1177. return -EFAULT;
  1178. }
  1179. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1180. sigsetsize);
  1181. }
  1182. COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
  1183. unsigned int, nfds, struct compat_timespec __user *, tsp,
  1184. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1185. {
  1186. compat_sigset_t ss32;
  1187. sigset_t ksigmask, sigsaved;
  1188. struct compat_timespec ts;
  1189. struct timespec end_time, *to = NULL;
  1190. int ret;
  1191. if (tsp) {
  1192. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1193. return -EFAULT;
  1194. to = &end_time;
  1195. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1196. return -EINVAL;
  1197. }
  1198. if (sigmask) {
  1199. if (sigsetsize != sizeof(compat_sigset_t))
  1200. return -EINVAL;
  1201. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1202. return -EFAULT;
  1203. sigset_from_compat(&ksigmask, &ss32);
  1204. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1205. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1206. }
  1207. ret = do_sys_poll(ufds, nfds, to);
  1208. /* We can restart this syscall, usually */
  1209. if (ret == -EINTR) {
  1210. /*
  1211. * Don't restore the signal mask yet. Let do_signal() deliver
  1212. * the signal on the way back to userspace, before the signal
  1213. * mask is restored.
  1214. */
  1215. if (sigmask) {
  1216. memcpy(&current->saved_sigmask, &sigsaved,
  1217. sizeof(sigsaved));
  1218. set_restore_sigmask();
  1219. }
  1220. ret = -ERESTARTNOHAND;
  1221. } else if (sigmask)
  1222. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1223. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1224. return ret;
  1225. }
  1226. #ifdef CONFIG_FHANDLE
  1227. /*
  1228. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  1229. * doesn't set the O_LARGEFILE flag.
  1230. */
  1231. COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  1232. struct file_handle __user *, handle, int, flags)
  1233. {
  1234. return do_handle_open(mountdirfd, handle, flags);
  1235. }
  1236. #endif