compat.c 40 KB

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