compat.c 37 KB

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