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