compat.c 44 KB

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