compat.c 37 KB

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