compat.c 37 KB

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