compat.c 35 KB

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