compat.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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/module.h>
  36. #include <linux/dirent.h>
  37. #include <linux/fsnotify.h>
  38. #include <linux/highuid.h>
  39. #include <linux/nfsd/syscall.h>
  40. #include <linux/personality.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/tsacct_kern.h>
  43. #include <linux/security.h>
  44. #include <linux/highmem.h>
  45. #include <linux/signal.h>
  46. #include <linux/poll.h>
  47. #include <linux/mm.h>
  48. #include <linux/eventpoll.h>
  49. #include <linux/fs_struct.h>
  50. #include <linux/slab.h>
  51. #include <linux/pagemap.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/mmu_context.h>
  54. #include <asm/ioctls.h>
  55. #include "internal.h"
  56. int compat_log = 1;
  57. int compat_printk(const char *fmt, ...)
  58. {
  59. va_list ap;
  60. int ret;
  61. if (!compat_log)
  62. return 0;
  63. va_start(ap, fmt);
  64. ret = vprintk(fmt, ap);
  65. va_end(ap);
  66. return ret;
  67. }
  68. #include "read_write.h"
  69. /*
  70. * Not all architectures have sys_utime, so implement this in terms
  71. * of sys_utimes.
  72. */
  73. asmlinkage long compat_sys_utime(const char __user *filename,
  74. struct compat_utimbuf __user *t)
  75. {
  76. struct timespec tv[2];
  77. if (t) {
  78. if (get_user(tv[0].tv_sec, &t->actime) ||
  79. get_user(tv[1].tv_sec, &t->modtime))
  80. return -EFAULT;
  81. tv[0].tv_nsec = 0;
  82. tv[1].tv_nsec = 0;
  83. }
  84. return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  85. }
  86. asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags)
  87. {
  88. struct timespec tv[2];
  89. if (t) {
  90. if (get_compat_timespec(&tv[0], &t[0]) ||
  91. get_compat_timespec(&tv[1], &t[1]))
  92. return -EFAULT;
  93. if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
  94. return 0;
  95. }
  96. return do_utimes(dfd, filename, t ? tv : NULL, flags);
  97. }
  98. asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t)
  99. {
  100. struct timespec tv[2];
  101. if (t) {
  102. if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
  103. get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
  104. get_user(tv[1].tv_sec, &t[1].tv_sec) ||
  105. get_user(tv[1].tv_nsec, &t[1].tv_usec))
  106. return -EFAULT;
  107. if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
  108. tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
  109. return -EINVAL;
  110. tv[0].tv_nsec *= 1000;
  111. tv[1].tv_nsec *= 1000;
  112. }
  113. return do_utimes(dfd, filename, t ? tv : NULL, 0);
  114. }
  115. asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t)
  116. {
  117. return compat_sys_futimesat(AT_FDCWD, filename, t);
  118. }
  119. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  120. {
  121. compat_ino_t ino = stat->ino;
  122. typeof(ubuf->st_uid) uid = 0;
  123. typeof(ubuf->st_gid) gid = 0;
  124. int err;
  125. SET_UID(uid, stat->uid);
  126. SET_GID(gid, stat->gid);
  127. if ((u64) stat->size > MAX_NON_LFS ||
  128. !old_valid_dev(stat->dev) ||
  129. !old_valid_dev(stat->rdev))
  130. return -EOVERFLOW;
  131. if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
  132. return -EOVERFLOW;
  133. if (clear_user(ubuf, sizeof(*ubuf)))
  134. return -EFAULT;
  135. err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
  136. err |= __put_user(ino, &ubuf->st_ino);
  137. err |= __put_user(stat->mode, &ubuf->st_mode);
  138. err |= __put_user(stat->nlink, &ubuf->st_nlink);
  139. err |= __put_user(uid, &ubuf->st_uid);
  140. err |= __put_user(gid, &ubuf->st_gid);
  141. err |= __put_user(old_encode_dev(stat->rdev), &ubuf->st_rdev);
  142. err |= __put_user(stat->size, &ubuf->st_size);
  143. err |= __put_user(stat->atime.tv_sec, &ubuf->st_atime);
  144. err |= __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec);
  145. err |= __put_user(stat->mtime.tv_sec, &ubuf->st_mtime);
  146. err |= __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec);
  147. err |= __put_user(stat->ctime.tv_sec, &ubuf->st_ctime);
  148. err |= __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec);
  149. err |= __put_user(stat->blksize, &ubuf->st_blksize);
  150. err |= __put_user(stat->blocks, &ubuf->st_blocks);
  151. return err;
  152. }
  153. asmlinkage long compat_sys_newstat(const char __user * filename,
  154. struct compat_stat __user *statbuf)
  155. {
  156. struct kstat stat;
  157. int error;
  158. error = vfs_stat(filename, &stat);
  159. if (error)
  160. return error;
  161. return cp_compat_stat(&stat, statbuf);
  162. }
  163. asmlinkage long compat_sys_newlstat(const char __user * filename,
  164. struct compat_stat __user *statbuf)
  165. {
  166. struct kstat stat;
  167. int error;
  168. error = vfs_lstat(filename, &stat);
  169. if (error)
  170. return error;
  171. return cp_compat_stat(&stat, statbuf);
  172. }
  173. #ifndef __ARCH_WANT_STAT64
  174. asmlinkage long compat_sys_newfstatat(unsigned int dfd,
  175. const char __user *filename,
  176. struct compat_stat __user *statbuf, int flag)
  177. {
  178. struct kstat stat;
  179. int error;
  180. error = vfs_fstatat(dfd, filename, &stat, flag);
  181. if (error)
  182. return error;
  183. return cp_compat_stat(&stat, statbuf);
  184. }
  185. #endif
  186. asmlinkage long compat_sys_newfstat(unsigned int fd,
  187. struct compat_stat __user * statbuf)
  188. {
  189. struct kstat stat;
  190. int error = vfs_fstat(fd, &stat);
  191. if (!error)
  192. error = cp_compat_stat(&stat, statbuf);
  193. return error;
  194. }
  195. static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
  196. {
  197. if (sizeof ubuf->f_blocks == 4) {
  198. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  199. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  200. return -EOVERFLOW;
  201. /* f_files and f_ffree may be -1; it's okay
  202. * to stuff that into 32 bits */
  203. if (kbuf->f_files != 0xffffffffffffffffULL
  204. && (kbuf->f_files & 0xffffffff00000000ULL))
  205. return -EOVERFLOW;
  206. if (kbuf->f_ffree != 0xffffffffffffffffULL
  207. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  208. return -EOVERFLOW;
  209. }
  210. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  211. __put_user(kbuf->f_type, &ubuf->f_type) ||
  212. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  213. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  214. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  215. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  216. __put_user(kbuf->f_files, &ubuf->f_files) ||
  217. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  218. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  219. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  220. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  221. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  222. __put_user(0, &ubuf->f_spare[0]) ||
  223. __put_user(0, &ubuf->f_spare[1]) ||
  224. __put_user(0, &ubuf->f_spare[2]) ||
  225. __put_user(0, &ubuf->f_spare[3]) ||
  226. __put_user(0, &ubuf->f_spare[4]))
  227. return -EFAULT;
  228. return 0;
  229. }
  230. /*
  231. * The following statfs calls are copies of code from fs/statfs.c and
  232. * should be checked against those from time to time
  233. */
  234. asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
  235. {
  236. struct path path;
  237. int error;
  238. error = user_path(pathname, &path);
  239. if (!error) {
  240. struct kstatfs tmp;
  241. error = vfs_statfs(&path, &tmp);
  242. if (!error)
  243. error = put_compat_statfs(buf, &tmp);
  244. path_put(&path);
  245. }
  246. return error;
  247. }
  248. asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
  249. {
  250. struct file * file;
  251. struct kstatfs tmp;
  252. int error;
  253. error = -EBADF;
  254. file = fget(fd);
  255. if (!file)
  256. goto out;
  257. error = vfs_statfs(&file->f_path, &tmp);
  258. if (!error)
  259. error = put_compat_statfs(buf, &tmp);
  260. fput(file);
  261. out:
  262. return error;
  263. }
  264. static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
  265. {
  266. if (sizeof ubuf->f_blocks == 4) {
  267. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  268. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  269. return -EOVERFLOW;
  270. /* f_files and f_ffree may be -1; it's okay
  271. * to stuff that into 32 bits */
  272. if (kbuf->f_files != 0xffffffffffffffffULL
  273. && (kbuf->f_files & 0xffffffff00000000ULL))
  274. return -EOVERFLOW;
  275. if (kbuf->f_ffree != 0xffffffffffffffffULL
  276. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  277. return -EOVERFLOW;
  278. }
  279. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  280. __put_user(kbuf->f_type, &ubuf->f_type) ||
  281. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  282. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  283. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  284. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  285. __put_user(kbuf->f_files, &ubuf->f_files) ||
  286. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  287. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  288. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  289. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  290. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  291. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  292. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  293. return -EFAULT;
  294. return 0;
  295. }
  296. asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
  297. {
  298. struct path path;
  299. int error;
  300. if (sz != sizeof(*buf))
  301. return -EINVAL;
  302. error = user_path(pathname, &path);
  303. if (!error) {
  304. struct kstatfs tmp;
  305. error = vfs_statfs(&path, &tmp);
  306. if (!error)
  307. error = put_compat_statfs64(buf, &tmp);
  308. path_put(&path);
  309. }
  310. return error;
  311. }
  312. asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
  313. {
  314. struct file * file;
  315. struct kstatfs tmp;
  316. int error;
  317. if (sz != sizeof(*buf))
  318. return -EINVAL;
  319. error = -EBADF;
  320. file = fget(fd);
  321. if (!file)
  322. goto out;
  323. error = vfs_statfs(&file->f_path, &tmp);
  324. if (!error)
  325. error = put_compat_statfs64(buf, &tmp);
  326. fput(file);
  327. out:
  328. return error;
  329. }
  330. /*
  331. * This is a copy of sys_ustat, just dealing with a structure layout.
  332. * Given how simple this syscall is that apporach is more maintainable
  333. * than the various conversion hacks.
  334. */
  335. asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u)
  336. {
  337. struct super_block *sb;
  338. struct compat_ustat tmp;
  339. struct kstatfs sbuf;
  340. int err;
  341. sb = user_get_super(new_decode_dev(dev));
  342. if (!sb)
  343. return -EINVAL;
  344. err = statfs_by_dentry(sb->s_root, &sbuf);
  345. drop_super(sb);
  346. if (err)
  347. return err;
  348. memset(&tmp, 0, sizeof(struct compat_ustat));
  349. tmp.f_tfree = sbuf.f_bfree;
  350. tmp.f_tinode = sbuf.f_ffree;
  351. if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
  352. return -EFAULT;
  353. return 0;
  354. }
  355. static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  356. {
  357. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  358. __get_user(kfl->l_type, &ufl->l_type) ||
  359. __get_user(kfl->l_whence, &ufl->l_whence) ||
  360. __get_user(kfl->l_start, &ufl->l_start) ||
  361. __get_user(kfl->l_len, &ufl->l_len) ||
  362. __get_user(kfl->l_pid, &ufl->l_pid))
  363. return -EFAULT;
  364. return 0;
  365. }
  366. static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  367. {
  368. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  369. __put_user(kfl->l_type, &ufl->l_type) ||
  370. __put_user(kfl->l_whence, &ufl->l_whence) ||
  371. __put_user(kfl->l_start, &ufl->l_start) ||
  372. __put_user(kfl->l_len, &ufl->l_len) ||
  373. __put_user(kfl->l_pid, &ufl->l_pid))
  374. return -EFAULT;
  375. return 0;
  376. }
  377. #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
  378. static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  379. {
  380. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  381. __get_user(kfl->l_type, &ufl->l_type) ||
  382. __get_user(kfl->l_whence, &ufl->l_whence) ||
  383. __get_user(kfl->l_start, &ufl->l_start) ||
  384. __get_user(kfl->l_len, &ufl->l_len) ||
  385. __get_user(kfl->l_pid, &ufl->l_pid))
  386. return -EFAULT;
  387. return 0;
  388. }
  389. #endif
  390. #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
  391. static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  392. {
  393. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  394. __put_user(kfl->l_type, &ufl->l_type) ||
  395. __put_user(kfl->l_whence, &ufl->l_whence) ||
  396. __put_user(kfl->l_start, &ufl->l_start) ||
  397. __put_user(kfl->l_len, &ufl->l_len) ||
  398. __put_user(kfl->l_pid, &ufl->l_pid))
  399. return -EFAULT;
  400. return 0;
  401. }
  402. #endif
  403. asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
  404. unsigned long arg)
  405. {
  406. mm_segment_t old_fs;
  407. struct flock f;
  408. long ret;
  409. switch (cmd) {
  410. case F_GETLK:
  411. case F_SETLK:
  412. case F_SETLKW:
  413. ret = get_compat_flock(&f, compat_ptr(arg));
  414. if (ret != 0)
  415. break;
  416. old_fs = get_fs();
  417. set_fs(KERNEL_DS);
  418. ret = sys_fcntl(fd, cmd, (unsigned long)&f);
  419. set_fs(old_fs);
  420. if (cmd == F_GETLK && ret == 0) {
  421. /* GETLK was successful and we need to return the data...
  422. * but it needs to fit in the compat structure.
  423. * l_start shouldn't be too big, unless the original
  424. * start + end is greater than COMPAT_OFF_T_MAX, in which
  425. * case the app was asking for trouble, so we return
  426. * -EOVERFLOW in that case.
  427. * l_len could be too big, in which case we just truncate it,
  428. * and only allow the app to see that part of the conflicting
  429. * lock that might make sense to it anyway
  430. */
  431. if (f.l_start > COMPAT_OFF_T_MAX)
  432. ret = -EOVERFLOW;
  433. if (f.l_len > COMPAT_OFF_T_MAX)
  434. f.l_len = COMPAT_OFF_T_MAX;
  435. if (ret == 0)
  436. ret = put_compat_flock(&f, compat_ptr(arg));
  437. }
  438. break;
  439. case F_GETLK64:
  440. case F_SETLK64:
  441. case F_SETLKW64:
  442. ret = get_compat_flock64(&f, compat_ptr(arg));
  443. if (ret != 0)
  444. break;
  445. old_fs = get_fs();
  446. set_fs(KERNEL_DS);
  447. ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
  448. ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
  449. (unsigned long)&f);
  450. set_fs(old_fs);
  451. if (cmd == F_GETLK64 && ret == 0) {
  452. /* need to return lock information - see above for commentary */
  453. if (f.l_start > COMPAT_LOFF_T_MAX)
  454. ret = -EOVERFLOW;
  455. if (f.l_len > COMPAT_LOFF_T_MAX)
  456. f.l_len = COMPAT_LOFF_T_MAX;
  457. if (ret == 0)
  458. ret = put_compat_flock64(&f, compat_ptr(arg));
  459. }
  460. break;
  461. default:
  462. ret = sys_fcntl(fd, cmd, arg);
  463. break;
  464. }
  465. return ret;
  466. }
  467. asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
  468. unsigned long arg)
  469. {
  470. if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
  471. return -EINVAL;
  472. return compat_sys_fcntl64(fd, cmd, arg);
  473. }
  474. asmlinkage long
  475. compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
  476. {
  477. long ret;
  478. aio_context_t ctx64;
  479. mm_segment_t oldfs = get_fs();
  480. if (unlikely(get_user(ctx64, ctx32p)))
  481. return -EFAULT;
  482. set_fs(KERNEL_DS);
  483. /* The __user pointer cast is valid because of the set_fs() */
  484. ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
  485. set_fs(oldfs);
  486. /* truncating is ok because it's a user address */
  487. if (!ret)
  488. ret = put_user((u32) ctx64, ctx32p);
  489. return ret;
  490. }
  491. asmlinkage long
  492. compat_sys_io_getevents(aio_context_t ctx_id,
  493. unsigned long min_nr,
  494. unsigned long nr,
  495. struct io_event __user *events,
  496. struct compat_timespec __user *timeout)
  497. {
  498. long ret;
  499. struct timespec t;
  500. struct timespec __user *ut = NULL;
  501. ret = -EFAULT;
  502. if (unlikely(!access_ok(VERIFY_WRITE, events,
  503. nr * sizeof(struct io_event))))
  504. goto out;
  505. if (timeout) {
  506. if (get_compat_timespec(&t, timeout))
  507. goto out;
  508. ut = compat_alloc_user_space(sizeof(*ut));
  509. if (copy_to_user(ut, &t, sizeof(t)) )
  510. goto out;
  511. }
  512. ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
  513. out:
  514. return ret;
  515. }
  516. /* A write operation does a read from user space and vice versa */
  517. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  518. ssize_t compat_rw_copy_check_uvector(int type,
  519. const struct compat_iovec __user *uvector, unsigned long nr_segs,
  520. unsigned long fast_segs, struct iovec *fast_pointer,
  521. struct iovec **ret_pointer)
  522. {
  523. compat_ssize_t tot_len;
  524. struct iovec *iov = *ret_pointer = fast_pointer;
  525. ssize_t ret = 0;
  526. int seg;
  527. /*
  528. * SuS says "The readv() function *may* fail if the iovcnt argument
  529. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  530. * traditionally returned zero for zero segments, so...
  531. */
  532. if (nr_segs == 0)
  533. goto out;
  534. ret = -EINVAL;
  535. if (nr_segs > UIO_MAXIOV || nr_segs < 0)
  536. goto out;
  537. if (nr_segs > fast_segs) {
  538. ret = -ENOMEM;
  539. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  540. if (iov == NULL)
  541. goto out;
  542. }
  543. *ret_pointer = iov;
  544. /*
  545. * Single unix specification:
  546. * We should -EINVAL if an element length is not >= 0 and fitting an
  547. * ssize_t.
  548. *
  549. * In Linux, the total length is limited to MAX_RW_COUNT, there is
  550. * no overflow possibility.
  551. */
  552. tot_len = 0;
  553. ret = -EINVAL;
  554. for (seg = 0; seg < nr_segs; seg++) {
  555. compat_uptr_t buf;
  556. compat_ssize_t len;
  557. if (__get_user(len, &uvector->iov_len) ||
  558. __get_user(buf, &uvector->iov_base)) {
  559. ret = -EFAULT;
  560. goto out;
  561. }
  562. if (len < 0) /* size_t not fitting in compat_ssize_t .. */
  563. goto out;
  564. if (!access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
  565. ret = -EFAULT;
  566. goto out;
  567. }
  568. if (len > MAX_RW_COUNT - tot_len)
  569. len = MAX_RW_COUNT - tot_len;
  570. tot_len += len;
  571. iov->iov_base = compat_ptr(buf);
  572. iov->iov_len = (compat_size_t) len;
  573. uvector++;
  574. iov++;
  575. }
  576. ret = tot_len;
  577. out:
  578. return ret;
  579. }
  580. static inline long
  581. copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
  582. {
  583. compat_uptr_t uptr;
  584. int i;
  585. for (i = 0; i < nr; ++i) {
  586. if (get_user(uptr, ptr32 + i))
  587. return -EFAULT;
  588. if (put_user(compat_ptr(uptr), ptr64 + i))
  589. return -EFAULT;
  590. }
  591. return 0;
  592. }
  593. #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
  594. asmlinkage long
  595. compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
  596. {
  597. struct iocb __user * __user *iocb64;
  598. long ret;
  599. if (unlikely(nr < 0))
  600. return -EINVAL;
  601. if (nr > MAX_AIO_SUBMITS)
  602. nr = MAX_AIO_SUBMITS;
  603. iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
  604. ret = copy_iocb(nr, iocb, iocb64);
  605. if (!ret)
  606. ret = do_io_submit(ctx_id, nr, iocb64, 1);
  607. return ret;
  608. }
  609. struct compat_ncp_mount_data {
  610. compat_int_t version;
  611. compat_uint_t ncp_fd;
  612. __compat_uid_t mounted_uid;
  613. compat_pid_t wdog_pid;
  614. unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
  615. compat_uint_t time_out;
  616. compat_uint_t retry_count;
  617. compat_uint_t flags;
  618. __compat_uid_t uid;
  619. __compat_gid_t gid;
  620. compat_mode_t file_mode;
  621. compat_mode_t dir_mode;
  622. };
  623. struct compat_ncp_mount_data_v4 {
  624. compat_int_t version;
  625. compat_ulong_t flags;
  626. compat_ulong_t mounted_uid;
  627. compat_long_t wdog_pid;
  628. compat_uint_t ncp_fd;
  629. compat_uint_t time_out;
  630. compat_uint_t retry_count;
  631. compat_ulong_t uid;
  632. compat_ulong_t gid;
  633. compat_ulong_t file_mode;
  634. compat_ulong_t dir_mode;
  635. };
  636. static void *do_ncp_super_data_conv(void *raw_data)
  637. {
  638. int version = *(unsigned int *)raw_data;
  639. if (version == 3) {
  640. struct compat_ncp_mount_data *c_n = raw_data;
  641. struct ncp_mount_data *n = raw_data;
  642. n->dir_mode = c_n->dir_mode;
  643. n->file_mode = c_n->file_mode;
  644. n->gid = c_n->gid;
  645. n->uid = c_n->uid;
  646. memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
  647. n->wdog_pid = c_n->wdog_pid;
  648. n->mounted_uid = c_n->mounted_uid;
  649. } else if (version == 4) {
  650. struct compat_ncp_mount_data_v4 *c_n = raw_data;
  651. struct ncp_mount_data_v4 *n = raw_data;
  652. n->dir_mode = c_n->dir_mode;
  653. n->file_mode = c_n->file_mode;
  654. n->gid = c_n->gid;
  655. n->uid = c_n->uid;
  656. n->retry_count = c_n->retry_count;
  657. n->time_out = c_n->time_out;
  658. n->ncp_fd = c_n->ncp_fd;
  659. n->wdog_pid = c_n->wdog_pid;
  660. n->mounted_uid = c_n->mounted_uid;
  661. n->flags = c_n->flags;
  662. } else if (version != 5) {
  663. return NULL;
  664. }
  665. return raw_data;
  666. }
  667. struct compat_nfs_string {
  668. compat_uint_t len;
  669. compat_uptr_t data;
  670. };
  671. static inline void compat_nfs_string(struct nfs_string *dst,
  672. struct compat_nfs_string *src)
  673. {
  674. dst->data = compat_ptr(src->data);
  675. dst->len = src->len;
  676. }
  677. struct compat_nfs4_mount_data_v1 {
  678. compat_int_t version;
  679. compat_int_t flags;
  680. compat_int_t rsize;
  681. compat_int_t wsize;
  682. compat_int_t timeo;
  683. compat_int_t retrans;
  684. compat_int_t acregmin;
  685. compat_int_t acregmax;
  686. compat_int_t acdirmin;
  687. compat_int_t acdirmax;
  688. struct compat_nfs_string client_addr;
  689. struct compat_nfs_string mnt_path;
  690. struct compat_nfs_string hostname;
  691. compat_uint_t host_addrlen;
  692. compat_uptr_t host_addr;
  693. compat_int_t proto;
  694. compat_int_t auth_flavourlen;
  695. compat_uptr_t auth_flavours;
  696. };
  697. static int do_nfs4_super_data_conv(void *raw_data)
  698. {
  699. int version = *(compat_uint_t *) raw_data;
  700. if (version == 1) {
  701. struct compat_nfs4_mount_data_v1 *raw = raw_data;
  702. struct nfs4_mount_data *real = raw_data;
  703. /* copy the fields backwards */
  704. real->auth_flavours = compat_ptr(raw->auth_flavours);
  705. real->auth_flavourlen = raw->auth_flavourlen;
  706. real->proto = raw->proto;
  707. real->host_addr = compat_ptr(raw->host_addr);
  708. real->host_addrlen = raw->host_addrlen;
  709. compat_nfs_string(&real->hostname, &raw->hostname);
  710. compat_nfs_string(&real->mnt_path, &raw->mnt_path);
  711. compat_nfs_string(&real->client_addr, &raw->client_addr);
  712. real->acdirmax = raw->acdirmax;
  713. real->acdirmin = raw->acdirmin;
  714. real->acregmax = raw->acregmax;
  715. real->acregmin = raw->acregmin;
  716. real->retrans = raw->retrans;
  717. real->timeo = raw->timeo;
  718. real->wsize = raw->wsize;
  719. real->rsize = raw->rsize;
  720. real->flags = raw->flags;
  721. real->version = raw->version;
  722. }
  723. return 0;
  724. }
  725. #define NCPFS_NAME "ncpfs"
  726. #define NFS4_NAME "nfs4"
  727. asmlinkage long compat_sys_mount(const char __user * dev_name,
  728. const char __user * dir_name,
  729. const char __user * type, unsigned long flags,
  730. const void __user * data)
  731. {
  732. char *kernel_type;
  733. unsigned long data_page;
  734. char *kernel_dev;
  735. char *dir_page;
  736. int retval;
  737. retval = copy_mount_string(type, &kernel_type);
  738. if (retval < 0)
  739. goto out;
  740. dir_page = getname(dir_name);
  741. retval = PTR_ERR(dir_page);
  742. if (IS_ERR(dir_page))
  743. goto out1;
  744. retval = copy_mount_string(dev_name, &kernel_dev);
  745. if (retval < 0)
  746. goto out2;
  747. retval = copy_mount_options(data, &data_page);
  748. if (retval < 0)
  749. goto out3;
  750. retval = -EINVAL;
  751. if (kernel_type && data_page) {
  752. if (!strcmp(kernel_type, NCPFS_NAME)) {
  753. do_ncp_super_data_conv((void *)data_page);
  754. } else if (!strcmp(kernel_type, NFS4_NAME)) {
  755. if (do_nfs4_super_data_conv((void *) data_page))
  756. goto out4;
  757. }
  758. }
  759. retval = do_mount(kernel_dev, dir_page, kernel_type,
  760. flags, (void*)data_page);
  761. out4:
  762. free_page(data_page);
  763. out3:
  764. kfree(kernel_dev);
  765. out2:
  766. putname(dir_page);
  767. out1:
  768. kfree(kernel_type);
  769. out:
  770. return retval;
  771. }
  772. struct compat_old_linux_dirent {
  773. compat_ulong_t d_ino;
  774. compat_ulong_t d_offset;
  775. unsigned short d_namlen;
  776. char d_name[1];
  777. };
  778. struct compat_readdir_callback {
  779. struct compat_old_linux_dirent __user *dirent;
  780. int result;
  781. };
  782. static int compat_fillonedir(void *__buf, const char *name, int namlen,
  783. loff_t offset, u64 ino, unsigned int d_type)
  784. {
  785. struct compat_readdir_callback *buf = __buf;
  786. struct compat_old_linux_dirent __user *dirent;
  787. compat_ulong_t d_ino;
  788. if (buf->result)
  789. return -EINVAL;
  790. d_ino = ino;
  791. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  792. buf->result = -EOVERFLOW;
  793. return -EOVERFLOW;
  794. }
  795. buf->result++;
  796. dirent = buf->dirent;
  797. if (!access_ok(VERIFY_WRITE, dirent,
  798. (unsigned long)(dirent->d_name + namlen + 1) -
  799. (unsigned long)dirent))
  800. goto efault;
  801. if ( __put_user(d_ino, &dirent->d_ino) ||
  802. __put_user(offset, &dirent->d_offset) ||
  803. __put_user(namlen, &dirent->d_namlen) ||
  804. __copy_to_user(dirent->d_name, name, namlen) ||
  805. __put_user(0, dirent->d_name + namlen))
  806. goto efault;
  807. return 0;
  808. efault:
  809. buf->result = -EFAULT;
  810. return -EFAULT;
  811. }
  812. asmlinkage long compat_sys_old_readdir(unsigned int fd,
  813. struct compat_old_linux_dirent __user *dirent, unsigned int count)
  814. {
  815. int error;
  816. struct file *file;
  817. struct compat_readdir_callback buf;
  818. error = -EBADF;
  819. file = fget(fd);
  820. if (!file)
  821. goto out;
  822. buf.result = 0;
  823. buf.dirent = dirent;
  824. error = vfs_readdir(file, compat_fillonedir, &buf);
  825. if (buf.result)
  826. error = buf.result;
  827. fput(file);
  828. out:
  829. return error;
  830. }
  831. struct compat_linux_dirent {
  832. compat_ulong_t d_ino;
  833. compat_ulong_t d_off;
  834. unsigned short d_reclen;
  835. char d_name[1];
  836. };
  837. struct compat_getdents_callback {
  838. struct compat_linux_dirent __user *current_dir;
  839. struct compat_linux_dirent __user *previous;
  840. int count;
  841. int error;
  842. };
  843. static int compat_filldir(void *__buf, const char *name, int namlen,
  844. loff_t offset, u64 ino, unsigned int d_type)
  845. {
  846. struct compat_linux_dirent __user * dirent;
  847. struct compat_getdents_callback *buf = __buf;
  848. compat_ulong_t d_ino;
  849. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  850. namlen + 2, sizeof(compat_long_t));
  851. buf->error = -EINVAL; /* only used if we fail.. */
  852. if (reclen > buf->count)
  853. return -EINVAL;
  854. d_ino = ino;
  855. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  856. buf->error = -EOVERFLOW;
  857. return -EOVERFLOW;
  858. }
  859. dirent = buf->previous;
  860. if (dirent) {
  861. if (__put_user(offset, &dirent->d_off))
  862. goto efault;
  863. }
  864. dirent = buf->current_dir;
  865. if (__put_user(d_ino, &dirent->d_ino))
  866. goto efault;
  867. if (__put_user(reclen, &dirent->d_reclen))
  868. goto efault;
  869. if (copy_to_user(dirent->d_name, name, namlen))
  870. goto efault;
  871. if (__put_user(0, dirent->d_name + namlen))
  872. goto efault;
  873. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  874. goto efault;
  875. buf->previous = dirent;
  876. dirent = (void __user *)dirent + reclen;
  877. buf->current_dir = dirent;
  878. buf->count -= reclen;
  879. return 0;
  880. efault:
  881. buf->error = -EFAULT;
  882. return -EFAULT;
  883. }
  884. asmlinkage long compat_sys_getdents(unsigned int fd,
  885. struct compat_linux_dirent __user *dirent, unsigned int count)
  886. {
  887. struct file * file;
  888. struct compat_linux_dirent __user * lastdirent;
  889. struct compat_getdents_callback buf;
  890. int error;
  891. error = -EFAULT;
  892. if (!access_ok(VERIFY_WRITE, dirent, count))
  893. goto out;
  894. error = -EBADF;
  895. file = fget(fd);
  896. if (!file)
  897. goto out;
  898. buf.current_dir = dirent;
  899. buf.previous = NULL;
  900. buf.count = count;
  901. buf.error = 0;
  902. error = vfs_readdir(file, compat_filldir, &buf);
  903. if (error >= 0)
  904. error = buf.error;
  905. lastdirent = buf.previous;
  906. if (lastdirent) {
  907. if (put_user(file->f_pos, &lastdirent->d_off))
  908. error = -EFAULT;
  909. else
  910. error = count - buf.count;
  911. }
  912. fput(file);
  913. out:
  914. return error;
  915. }
  916. #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
  917. struct compat_getdents_callback64 {
  918. struct linux_dirent64 __user *current_dir;
  919. struct linux_dirent64 __user *previous;
  920. int count;
  921. int error;
  922. };
  923. static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  924. u64 ino, unsigned int d_type)
  925. {
  926. struct linux_dirent64 __user *dirent;
  927. struct compat_getdents_callback64 *buf = __buf;
  928. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  929. sizeof(u64));
  930. u64 off;
  931. buf->error = -EINVAL; /* only used if we fail.. */
  932. if (reclen > buf->count)
  933. return -EINVAL;
  934. dirent = buf->previous;
  935. if (dirent) {
  936. if (__put_user_unaligned(offset, &dirent->d_off))
  937. goto efault;
  938. }
  939. dirent = buf->current_dir;
  940. if (__put_user_unaligned(ino, &dirent->d_ino))
  941. goto efault;
  942. off = 0;
  943. if (__put_user_unaligned(off, &dirent->d_off))
  944. goto efault;
  945. if (__put_user(reclen, &dirent->d_reclen))
  946. goto efault;
  947. if (__put_user(d_type, &dirent->d_type))
  948. goto efault;
  949. if (copy_to_user(dirent->d_name, name, namlen))
  950. goto efault;
  951. if (__put_user(0, dirent->d_name + namlen))
  952. goto efault;
  953. buf->previous = dirent;
  954. dirent = (void __user *)dirent + reclen;
  955. buf->current_dir = dirent;
  956. buf->count -= reclen;
  957. return 0;
  958. efault:
  959. buf->error = -EFAULT;
  960. return -EFAULT;
  961. }
  962. asmlinkage long compat_sys_getdents64(unsigned int fd,
  963. struct linux_dirent64 __user * dirent, unsigned int count)
  964. {
  965. struct file * file;
  966. struct linux_dirent64 __user * lastdirent;
  967. struct compat_getdents_callback64 buf;
  968. int error;
  969. error = -EFAULT;
  970. if (!access_ok(VERIFY_WRITE, dirent, count))
  971. goto out;
  972. error = -EBADF;
  973. file = fget(fd);
  974. if (!file)
  975. goto out;
  976. buf.current_dir = dirent;
  977. buf.previous = NULL;
  978. buf.count = count;
  979. buf.error = 0;
  980. error = vfs_readdir(file, compat_filldir64, &buf);
  981. if (error >= 0)
  982. error = buf.error;
  983. lastdirent = buf.previous;
  984. if (lastdirent) {
  985. typeof(lastdirent->d_off) d_off = file->f_pos;
  986. if (__put_user_unaligned(d_off, &lastdirent->d_off))
  987. error = -EFAULT;
  988. else
  989. error = count - buf.count;
  990. }
  991. fput(file);
  992. out:
  993. return error;
  994. }
  995. #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
  996. static ssize_t compat_do_readv_writev(int type, struct file *file,
  997. const struct compat_iovec __user *uvector,
  998. unsigned long nr_segs, loff_t *pos)
  999. {
  1000. compat_ssize_t tot_len;
  1001. struct iovec iovstack[UIO_FASTIOV];
  1002. struct iovec *iov = iovstack;
  1003. ssize_t ret;
  1004. io_fn_t fn;
  1005. iov_fn_t fnv;
  1006. ret = -EINVAL;
  1007. if (!file->f_op)
  1008. goto out;
  1009. ret = -EFAULT;
  1010. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  1011. goto out;
  1012. tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
  1013. UIO_FASTIOV, iovstack, &iov);
  1014. if (tot_len == 0) {
  1015. ret = 0;
  1016. goto out;
  1017. }
  1018. ret = rw_verify_area(type, file, pos, tot_len);
  1019. if (ret < 0)
  1020. goto out;
  1021. fnv = NULL;
  1022. if (type == READ) {
  1023. fn = file->f_op->read;
  1024. fnv = file->f_op->aio_read;
  1025. } else {
  1026. fn = (io_fn_t)file->f_op->write;
  1027. fnv = file->f_op->aio_write;
  1028. }
  1029. if (fnv)
  1030. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  1031. pos, fnv);
  1032. else
  1033. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  1034. out:
  1035. if (iov != iovstack)
  1036. kfree(iov);
  1037. if ((ret + (type == READ)) > 0) {
  1038. if (type == READ)
  1039. fsnotify_access(file);
  1040. else
  1041. fsnotify_modify(file);
  1042. }
  1043. return ret;
  1044. }
  1045. static size_t compat_readv(struct file *file,
  1046. const struct compat_iovec __user *vec,
  1047. unsigned long vlen, loff_t *pos)
  1048. {
  1049. ssize_t ret = -EBADF;
  1050. if (!(file->f_mode & FMODE_READ))
  1051. goto out;
  1052. ret = -EINVAL;
  1053. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  1054. goto out;
  1055. ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
  1056. out:
  1057. if (ret > 0)
  1058. add_rchar(current, ret);
  1059. inc_syscr(current);
  1060. return ret;
  1061. }
  1062. asmlinkage ssize_t
  1063. compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec,
  1064. unsigned long vlen)
  1065. {
  1066. struct file *file;
  1067. int fput_needed;
  1068. ssize_t ret;
  1069. file = fget_light(fd, &fput_needed);
  1070. if (!file)
  1071. return -EBADF;
  1072. ret = compat_readv(file, vec, vlen, &file->f_pos);
  1073. fput_light(file, fput_needed);
  1074. return ret;
  1075. }
  1076. asmlinkage ssize_t
  1077. compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
  1078. unsigned long vlen, u32 pos_low, u32 pos_high)
  1079. {
  1080. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1081. struct file *file;
  1082. int fput_needed;
  1083. ssize_t ret;
  1084. if (pos < 0)
  1085. return -EINVAL;
  1086. file = fget_light(fd, &fput_needed);
  1087. if (!file)
  1088. return -EBADF;
  1089. ret = -ESPIPE;
  1090. if (file->f_mode & FMODE_PREAD)
  1091. ret = compat_readv(file, vec, vlen, &pos);
  1092. fput_light(file, fput_needed);
  1093. return ret;
  1094. }
  1095. static size_t compat_writev(struct file *file,
  1096. const struct compat_iovec __user *vec,
  1097. unsigned long vlen, loff_t *pos)
  1098. {
  1099. ssize_t ret = -EBADF;
  1100. if (!(file->f_mode & FMODE_WRITE))
  1101. goto out;
  1102. ret = -EINVAL;
  1103. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  1104. goto out;
  1105. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
  1106. out:
  1107. if (ret > 0)
  1108. add_wchar(current, ret);
  1109. inc_syscw(current);
  1110. return ret;
  1111. }
  1112. asmlinkage ssize_t
  1113. compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec,
  1114. unsigned long vlen)
  1115. {
  1116. struct file *file;
  1117. int fput_needed;
  1118. ssize_t ret;
  1119. file = fget_light(fd, &fput_needed);
  1120. if (!file)
  1121. return -EBADF;
  1122. ret = compat_writev(file, vec, vlen, &file->f_pos);
  1123. fput_light(file, fput_needed);
  1124. return ret;
  1125. }
  1126. asmlinkage ssize_t
  1127. compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec,
  1128. unsigned long vlen, u32 pos_low, u32 pos_high)
  1129. {
  1130. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1131. struct file *file;
  1132. int fput_needed;
  1133. ssize_t ret;
  1134. if (pos < 0)
  1135. return -EINVAL;
  1136. file = fget_light(fd, &fput_needed);
  1137. if (!file)
  1138. return -EBADF;
  1139. ret = -ESPIPE;
  1140. if (file->f_mode & FMODE_PWRITE)
  1141. ret = compat_writev(file, vec, vlen, &pos);
  1142. fput_light(file, fput_needed);
  1143. return ret;
  1144. }
  1145. asmlinkage long
  1146. compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
  1147. unsigned int nr_segs, unsigned int flags)
  1148. {
  1149. unsigned i;
  1150. struct iovec __user *iov;
  1151. if (nr_segs > UIO_MAXIOV)
  1152. return -EINVAL;
  1153. iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  1154. for (i = 0; i < nr_segs; i++) {
  1155. struct compat_iovec v;
  1156. if (get_user(v.iov_base, &iov32[i].iov_base) ||
  1157. get_user(v.iov_len, &iov32[i].iov_len) ||
  1158. put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  1159. put_user(v.iov_len, &iov[i].iov_len))
  1160. return -EFAULT;
  1161. }
  1162. return sys_vmsplice(fd, iov, nr_segs, flags);
  1163. }
  1164. /*
  1165. * Exactly like fs/open.c:sys_open(), except that it doesn't set the
  1166. * O_LARGEFILE flag.
  1167. */
  1168. asmlinkage long
  1169. compat_sys_open(const char __user *filename, int flags, int mode)
  1170. {
  1171. return do_sys_open(AT_FDCWD, filename, flags, mode);
  1172. }
  1173. /*
  1174. * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
  1175. * O_LARGEFILE flag.
  1176. */
  1177. asmlinkage long
  1178. compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
  1179. {
  1180. return do_sys_open(dfd, filename, flags, mode);
  1181. }
  1182. /*
  1183. * compat_count() counts the number of arguments/envelopes. It is basically
  1184. * a copy of count() from fs/exec.c, except that it works with 32 bit argv
  1185. * and envp pointers.
  1186. */
  1187. static int compat_count(compat_uptr_t __user *argv, int max)
  1188. {
  1189. int i = 0;
  1190. if (argv != NULL) {
  1191. for (;;) {
  1192. compat_uptr_t p;
  1193. if (get_user(p, argv))
  1194. return -EFAULT;
  1195. if (!p)
  1196. break;
  1197. argv++;
  1198. if (i++ >= max)
  1199. return -E2BIG;
  1200. if (fatal_signal_pending(current))
  1201. return -ERESTARTNOHAND;
  1202. cond_resched();
  1203. }
  1204. }
  1205. return i;
  1206. }
  1207. /*
  1208. * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
  1209. * except that it works with 32 bit argv and envp pointers.
  1210. */
  1211. static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
  1212. struct linux_binprm *bprm)
  1213. {
  1214. struct page *kmapped_page = NULL;
  1215. char *kaddr = NULL;
  1216. unsigned long kpos = 0;
  1217. int ret;
  1218. while (argc-- > 0) {
  1219. compat_uptr_t str;
  1220. int len;
  1221. unsigned long pos;
  1222. if (get_user(str, argv+argc) ||
  1223. !(len = strnlen_user(compat_ptr(str), MAX_ARG_STRLEN))) {
  1224. ret = -EFAULT;
  1225. goto out;
  1226. }
  1227. if (len > MAX_ARG_STRLEN) {
  1228. ret = -E2BIG;
  1229. goto out;
  1230. }
  1231. /* We're going to work our way backwords. */
  1232. pos = bprm->p;
  1233. str += len;
  1234. bprm->p -= len;
  1235. while (len > 0) {
  1236. int offset, bytes_to_copy;
  1237. if (fatal_signal_pending(current)) {
  1238. ret = -ERESTARTNOHAND;
  1239. goto out;
  1240. }
  1241. cond_resched();
  1242. offset = pos % PAGE_SIZE;
  1243. if (offset == 0)
  1244. offset = PAGE_SIZE;
  1245. bytes_to_copy = offset;
  1246. if (bytes_to_copy > len)
  1247. bytes_to_copy = len;
  1248. offset -= bytes_to_copy;
  1249. pos -= bytes_to_copy;
  1250. str -= bytes_to_copy;
  1251. len -= bytes_to_copy;
  1252. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  1253. struct page *page;
  1254. page = get_arg_page(bprm, pos, 1);
  1255. if (!page) {
  1256. ret = -E2BIG;
  1257. goto out;
  1258. }
  1259. if (kmapped_page) {
  1260. flush_kernel_dcache_page(kmapped_page);
  1261. kunmap(kmapped_page);
  1262. put_page(kmapped_page);
  1263. }
  1264. kmapped_page = page;
  1265. kaddr = kmap(kmapped_page);
  1266. kpos = pos & PAGE_MASK;
  1267. flush_cache_page(bprm->vma, kpos,
  1268. page_to_pfn(kmapped_page));
  1269. }
  1270. if (copy_from_user(kaddr+offset, compat_ptr(str),
  1271. bytes_to_copy)) {
  1272. ret = -EFAULT;
  1273. goto out;
  1274. }
  1275. }
  1276. }
  1277. ret = 0;
  1278. out:
  1279. if (kmapped_page) {
  1280. flush_kernel_dcache_page(kmapped_page);
  1281. kunmap(kmapped_page);
  1282. put_page(kmapped_page);
  1283. }
  1284. return ret;
  1285. }
  1286. /*
  1287. * compat_do_execve() is mostly a copy of do_execve(), with the exception
  1288. * that it processes 32 bit argv and envp pointers.
  1289. */
  1290. int compat_do_execve(char * filename,
  1291. compat_uptr_t __user *argv,
  1292. compat_uptr_t __user *envp,
  1293. struct pt_regs * regs)
  1294. {
  1295. struct linux_binprm *bprm;
  1296. struct file *file;
  1297. struct files_struct *displaced;
  1298. bool clear_in_exec;
  1299. int retval;
  1300. retval = unshare_files(&displaced);
  1301. if (retval)
  1302. goto out_ret;
  1303. retval = -ENOMEM;
  1304. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1305. if (!bprm)
  1306. goto out_files;
  1307. retval = prepare_bprm_creds(bprm);
  1308. if (retval)
  1309. goto out_free;
  1310. retval = check_unsafe_exec(bprm);
  1311. if (retval < 0)
  1312. goto out_free;
  1313. clear_in_exec = retval;
  1314. current->in_execve = 1;
  1315. file = open_exec(filename);
  1316. retval = PTR_ERR(file);
  1317. if (IS_ERR(file))
  1318. goto out_unmark;
  1319. sched_exec();
  1320. bprm->file = file;
  1321. bprm->filename = filename;
  1322. bprm->interp = filename;
  1323. retval = bprm_mm_init(bprm);
  1324. if (retval)
  1325. goto out_file;
  1326. bprm->argc = compat_count(argv, MAX_ARG_STRINGS);
  1327. if ((retval = bprm->argc) < 0)
  1328. goto out;
  1329. bprm->envc = compat_count(envp, MAX_ARG_STRINGS);
  1330. if ((retval = bprm->envc) < 0)
  1331. goto out;
  1332. retval = prepare_binprm(bprm);
  1333. if (retval < 0)
  1334. goto out;
  1335. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1336. if (retval < 0)
  1337. goto out;
  1338. bprm->exec = bprm->p;
  1339. retval = compat_copy_strings(bprm->envc, envp, bprm);
  1340. if (retval < 0)
  1341. goto out;
  1342. retval = compat_copy_strings(bprm->argc, argv, bprm);
  1343. if (retval < 0)
  1344. goto out;
  1345. retval = search_binary_handler(bprm, regs);
  1346. if (retval < 0)
  1347. goto out;
  1348. /* execve succeeded */
  1349. current->fs->in_exec = 0;
  1350. current->in_execve = 0;
  1351. acct_update_integrals(current);
  1352. free_bprm(bprm);
  1353. if (displaced)
  1354. put_files_struct(displaced);
  1355. return retval;
  1356. out:
  1357. if (bprm->mm) {
  1358. acct_arg_size(bprm, 0);
  1359. mmput(bprm->mm);
  1360. }
  1361. out_file:
  1362. if (bprm->file) {
  1363. allow_write_access(bprm->file);
  1364. fput(bprm->file);
  1365. }
  1366. out_unmark:
  1367. if (clear_in_exec)
  1368. current->fs->in_exec = 0;
  1369. current->in_execve = 0;
  1370. out_free:
  1371. free_bprm(bprm);
  1372. out_files:
  1373. if (displaced)
  1374. reset_files_struct(displaced);
  1375. out_ret:
  1376. return retval;
  1377. }
  1378. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  1379. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  1380. int timeval, int ret)
  1381. {
  1382. struct timespec ts;
  1383. if (!p)
  1384. return ret;
  1385. if (current->personality & STICKY_TIMEOUTS)
  1386. goto sticky;
  1387. /* No update for zero timeout */
  1388. if (!end_time->tv_sec && !end_time->tv_nsec)
  1389. return ret;
  1390. ktime_get_ts(&ts);
  1391. ts = timespec_sub(*end_time, ts);
  1392. if (ts.tv_sec < 0)
  1393. ts.tv_sec = ts.tv_nsec = 0;
  1394. if (timeval) {
  1395. struct compat_timeval rtv;
  1396. rtv.tv_sec = ts.tv_sec;
  1397. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  1398. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  1399. return ret;
  1400. } else {
  1401. struct compat_timespec rts;
  1402. rts.tv_sec = ts.tv_sec;
  1403. rts.tv_nsec = ts.tv_nsec;
  1404. if (!copy_to_user(p, &rts, sizeof(rts)))
  1405. return ret;
  1406. }
  1407. /*
  1408. * If an application puts its timeval in read-only memory, we
  1409. * don't want the Linux-specific update to the timeval to
  1410. * cause a fault after the select has completed
  1411. * successfully. However, because we're not updating the
  1412. * timeval, we can't restart the system call.
  1413. */
  1414. sticky:
  1415. if (ret == -ERESTARTNOHAND)
  1416. ret = -EINTR;
  1417. return ret;
  1418. }
  1419. /*
  1420. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  1421. * 64-bit unsigned longs.
  1422. */
  1423. static
  1424. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1425. unsigned long *fdset)
  1426. {
  1427. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1428. if (ufdset) {
  1429. unsigned long odd;
  1430. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  1431. return -EFAULT;
  1432. odd = nr & 1UL;
  1433. nr &= ~1UL;
  1434. while (nr) {
  1435. unsigned long h, l;
  1436. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  1437. return -EFAULT;
  1438. ufdset += 2;
  1439. *fdset++ = h << 32 | l;
  1440. nr -= 2;
  1441. }
  1442. if (odd && __get_user(*fdset, ufdset))
  1443. return -EFAULT;
  1444. } else {
  1445. /* Tricky, must clear full unsigned long in the
  1446. * kernel fdset at the end, this makes sure that
  1447. * actually happens.
  1448. */
  1449. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  1450. }
  1451. return 0;
  1452. }
  1453. static
  1454. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1455. unsigned long *fdset)
  1456. {
  1457. unsigned long odd;
  1458. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1459. if (!ufdset)
  1460. return 0;
  1461. odd = nr & 1UL;
  1462. nr &= ~1UL;
  1463. while (nr) {
  1464. unsigned long h, l;
  1465. l = *fdset++;
  1466. h = l >> 32;
  1467. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1468. return -EFAULT;
  1469. ufdset += 2;
  1470. nr -= 2;
  1471. }
  1472. if (odd && __put_user(*fdset, ufdset))
  1473. return -EFAULT;
  1474. return 0;
  1475. }
  1476. /*
  1477. * This is a virtual copy of sys_select from fs/select.c and probably
  1478. * should be compared to it from time to time
  1479. */
  1480. /*
  1481. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1482. * like to be certain this leads to no problems. So I return
  1483. * EINTR just for safety.
  1484. *
  1485. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1486. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1487. */
  1488. #define MAX_SELECT_SECONDS \
  1489. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  1490. int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1491. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1492. struct timespec *end_time)
  1493. {
  1494. fd_set_bits fds;
  1495. void *bits;
  1496. int size, max_fds, ret = -EINVAL;
  1497. struct fdtable *fdt;
  1498. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1499. if (n < 0)
  1500. goto out_nofds;
  1501. /* max_fds can increase, so grab it once to avoid race */
  1502. rcu_read_lock();
  1503. fdt = files_fdtable(current->files);
  1504. max_fds = fdt->max_fds;
  1505. rcu_read_unlock();
  1506. if (n > max_fds)
  1507. n = max_fds;
  1508. /*
  1509. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1510. * since we used fdset we need to allocate memory in units of
  1511. * long-words.
  1512. */
  1513. size = FDS_BYTES(n);
  1514. bits = stack_fds;
  1515. if (size > sizeof(stack_fds) / 6) {
  1516. bits = kmalloc(6 * size, GFP_KERNEL);
  1517. ret = -ENOMEM;
  1518. if (!bits)
  1519. goto out_nofds;
  1520. }
  1521. fds.in = (unsigned long *) bits;
  1522. fds.out = (unsigned long *) (bits + size);
  1523. fds.ex = (unsigned long *) (bits + 2*size);
  1524. fds.res_in = (unsigned long *) (bits + 3*size);
  1525. fds.res_out = (unsigned long *) (bits + 4*size);
  1526. fds.res_ex = (unsigned long *) (bits + 5*size);
  1527. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1528. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1529. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1530. goto out;
  1531. zero_fd_set(n, fds.res_in);
  1532. zero_fd_set(n, fds.res_out);
  1533. zero_fd_set(n, fds.res_ex);
  1534. ret = do_select(n, &fds, end_time);
  1535. if (ret < 0)
  1536. goto out;
  1537. if (!ret) {
  1538. ret = -ERESTARTNOHAND;
  1539. if (signal_pending(current))
  1540. goto out;
  1541. ret = 0;
  1542. }
  1543. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1544. compat_set_fd_set(n, outp, fds.res_out) ||
  1545. compat_set_fd_set(n, exp, fds.res_ex))
  1546. ret = -EFAULT;
  1547. out:
  1548. if (bits != stack_fds)
  1549. kfree(bits);
  1550. out_nofds:
  1551. return ret;
  1552. }
  1553. asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
  1554. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1555. struct compat_timeval __user *tvp)
  1556. {
  1557. struct timespec end_time, *to = NULL;
  1558. struct compat_timeval tv;
  1559. int ret;
  1560. if (tvp) {
  1561. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1562. return -EFAULT;
  1563. to = &end_time;
  1564. if (poll_select_set_timeout(to,
  1565. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1566. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1567. return -EINVAL;
  1568. }
  1569. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1570. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1571. return ret;
  1572. }
  1573. struct compat_sel_arg_struct {
  1574. compat_ulong_t n;
  1575. compat_uptr_t inp;
  1576. compat_uptr_t outp;
  1577. compat_uptr_t exp;
  1578. compat_uptr_t tvp;
  1579. };
  1580. asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
  1581. {
  1582. struct compat_sel_arg_struct a;
  1583. if (copy_from_user(&a, arg, sizeof(a)))
  1584. return -EFAULT;
  1585. return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1586. compat_ptr(a.exp), compat_ptr(a.tvp));
  1587. }
  1588. #ifdef HAVE_SET_RESTORE_SIGMASK
  1589. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1590. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1591. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1592. compat_size_t sigsetsize)
  1593. {
  1594. compat_sigset_t ss32;
  1595. sigset_t ksigmask, sigsaved;
  1596. struct compat_timespec ts;
  1597. struct timespec end_time, *to = NULL;
  1598. int ret;
  1599. if (tsp) {
  1600. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1601. return -EFAULT;
  1602. to = &end_time;
  1603. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1604. return -EINVAL;
  1605. }
  1606. if (sigmask) {
  1607. if (sigsetsize != sizeof(compat_sigset_t))
  1608. return -EINVAL;
  1609. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1610. return -EFAULT;
  1611. sigset_from_compat(&ksigmask, &ss32);
  1612. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1613. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1614. }
  1615. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1616. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1617. if (ret == -ERESTARTNOHAND) {
  1618. /*
  1619. * Don't restore the signal mask yet. Let do_signal() deliver
  1620. * the signal on the way back to userspace, before the signal
  1621. * mask is restored.
  1622. */
  1623. if (sigmask) {
  1624. memcpy(&current->saved_sigmask, &sigsaved,
  1625. sizeof(sigsaved));
  1626. set_restore_sigmask();
  1627. }
  1628. } else if (sigmask)
  1629. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1630. return ret;
  1631. }
  1632. asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
  1633. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1634. struct compat_timespec __user *tsp, void __user *sig)
  1635. {
  1636. compat_size_t sigsetsize = 0;
  1637. compat_uptr_t up = 0;
  1638. if (sig) {
  1639. if (!access_ok(VERIFY_READ, sig,
  1640. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1641. __get_user(up, (compat_uptr_t __user *)sig) ||
  1642. __get_user(sigsetsize,
  1643. (compat_size_t __user *)(sig+sizeof(up))))
  1644. return -EFAULT;
  1645. }
  1646. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1647. sigsetsize);
  1648. }
  1649. asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
  1650. unsigned int nfds, struct compat_timespec __user *tsp,
  1651. const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
  1652. {
  1653. compat_sigset_t ss32;
  1654. sigset_t ksigmask, sigsaved;
  1655. struct compat_timespec ts;
  1656. struct timespec end_time, *to = NULL;
  1657. int ret;
  1658. if (tsp) {
  1659. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1660. return -EFAULT;
  1661. to = &end_time;
  1662. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1663. return -EINVAL;
  1664. }
  1665. if (sigmask) {
  1666. if (sigsetsize != sizeof(compat_sigset_t))
  1667. return -EINVAL;
  1668. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1669. return -EFAULT;
  1670. sigset_from_compat(&ksigmask, &ss32);
  1671. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1672. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1673. }
  1674. ret = do_sys_poll(ufds, nfds, to);
  1675. /* We can restart this syscall, usually */
  1676. if (ret == -EINTR) {
  1677. /*
  1678. * Don't restore the signal mask yet. Let do_signal() deliver
  1679. * the signal on the way back to userspace, before the signal
  1680. * mask is restored.
  1681. */
  1682. if (sigmask) {
  1683. memcpy(&current->saved_sigmask, &sigsaved,
  1684. sizeof(sigsaved));
  1685. set_restore_sigmask();
  1686. }
  1687. ret = -ERESTARTNOHAND;
  1688. } else if (sigmask)
  1689. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1690. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1691. return ret;
  1692. }
  1693. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1694. #if (defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)) && !defined(CONFIG_NFSD_DEPRECATED)
  1695. /* Stuff for NFS server syscalls... */
  1696. struct compat_nfsctl_svc {
  1697. u16 svc32_port;
  1698. s32 svc32_nthreads;
  1699. };
  1700. struct compat_nfsctl_client {
  1701. s8 cl32_ident[NFSCLNT_IDMAX+1];
  1702. s32 cl32_naddr;
  1703. struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
  1704. s32 cl32_fhkeytype;
  1705. s32 cl32_fhkeylen;
  1706. u8 cl32_fhkey[NFSCLNT_KEYMAX];
  1707. };
  1708. struct compat_nfsctl_export {
  1709. char ex32_client[NFSCLNT_IDMAX+1];
  1710. char ex32_path[NFS_MAXPATHLEN+1];
  1711. compat_dev_t ex32_dev;
  1712. compat_ino_t ex32_ino;
  1713. compat_int_t ex32_flags;
  1714. __compat_uid_t ex32_anon_uid;
  1715. __compat_gid_t ex32_anon_gid;
  1716. };
  1717. struct compat_nfsctl_fdparm {
  1718. struct sockaddr gd32_addr;
  1719. s8 gd32_path[NFS_MAXPATHLEN+1];
  1720. compat_int_t gd32_version;
  1721. };
  1722. struct compat_nfsctl_fsparm {
  1723. struct sockaddr gd32_addr;
  1724. s8 gd32_path[NFS_MAXPATHLEN+1];
  1725. compat_int_t gd32_maxlen;
  1726. };
  1727. struct compat_nfsctl_arg {
  1728. compat_int_t ca32_version; /* safeguard */
  1729. union {
  1730. struct compat_nfsctl_svc u32_svc;
  1731. struct compat_nfsctl_client u32_client;
  1732. struct compat_nfsctl_export u32_export;
  1733. struct compat_nfsctl_fdparm u32_getfd;
  1734. struct compat_nfsctl_fsparm u32_getfs;
  1735. } u;
  1736. #define ca32_svc u.u32_svc
  1737. #define ca32_client u.u32_client
  1738. #define ca32_export u.u32_export
  1739. #define ca32_getfd u.u32_getfd
  1740. #define ca32_getfs u.u32_getfs
  1741. };
  1742. union compat_nfsctl_res {
  1743. __u8 cr32_getfh[NFS_FHSIZE];
  1744. struct knfsd_fh cr32_getfs;
  1745. };
  1746. static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
  1747. struct compat_nfsctl_arg __user *arg)
  1748. {
  1749. if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
  1750. get_user(karg->ca_version, &arg->ca32_version) ||
  1751. __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
  1752. __get_user(karg->ca_svc.svc_nthreads,
  1753. &arg->ca32_svc.svc32_nthreads))
  1754. return -EFAULT;
  1755. return 0;
  1756. }
  1757. static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
  1758. struct compat_nfsctl_arg __user *arg)
  1759. {
  1760. if (!access_ok(VERIFY_READ, &arg->ca32_client,
  1761. sizeof(arg->ca32_client)) ||
  1762. get_user(karg->ca_version, &arg->ca32_version) ||
  1763. __copy_from_user(&karg->ca_client.cl_ident[0],
  1764. &arg->ca32_client.cl32_ident[0],
  1765. NFSCLNT_IDMAX) ||
  1766. __get_user(karg->ca_client.cl_naddr,
  1767. &arg->ca32_client.cl32_naddr) ||
  1768. __copy_from_user(&karg->ca_client.cl_addrlist[0],
  1769. &arg->ca32_client.cl32_addrlist[0],
  1770. (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
  1771. __get_user(karg->ca_client.cl_fhkeytype,
  1772. &arg->ca32_client.cl32_fhkeytype) ||
  1773. __get_user(karg->ca_client.cl_fhkeylen,
  1774. &arg->ca32_client.cl32_fhkeylen) ||
  1775. __copy_from_user(&karg->ca_client.cl_fhkey[0],
  1776. &arg->ca32_client.cl32_fhkey[0],
  1777. NFSCLNT_KEYMAX))
  1778. return -EFAULT;
  1779. return 0;
  1780. }
  1781. static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
  1782. struct compat_nfsctl_arg __user *arg)
  1783. {
  1784. if (!access_ok(VERIFY_READ, &arg->ca32_export,
  1785. sizeof(arg->ca32_export)) ||
  1786. get_user(karg->ca_version, &arg->ca32_version) ||
  1787. __copy_from_user(&karg->ca_export.ex_client[0],
  1788. &arg->ca32_export.ex32_client[0],
  1789. NFSCLNT_IDMAX) ||
  1790. __copy_from_user(&karg->ca_export.ex_path[0],
  1791. &arg->ca32_export.ex32_path[0],
  1792. NFS_MAXPATHLEN) ||
  1793. __get_user(karg->ca_export.ex_dev,
  1794. &arg->ca32_export.ex32_dev) ||
  1795. __get_user(karg->ca_export.ex_ino,
  1796. &arg->ca32_export.ex32_ino) ||
  1797. __get_user(karg->ca_export.ex_flags,
  1798. &arg->ca32_export.ex32_flags) ||
  1799. __get_user(karg->ca_export.ex_anon_uid,
  1800. &arg->ca32_export.ex32_anon_uid) ||
  1801. __get_user(karg->ca_export.ex_anon_gid,
  1802. &arg->ca32_export.ex32_anon_gid))
  1803. return -EFAULT;
  1804. SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
  1805. SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
  1806. return 0;
  1807. }
  1808. static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
  1809. struct compat_nfsctl_arg __user *arg)
  1810. {
  1811. if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
  1812. sizeof(arg->ca32_getfd)) ||
  1813. get_user(karg->ca_version, &arg->ca32_version) ||
  1814. __copy_from_user(&karg->ca_getfd.gd_addr,
  1815. &arg->ca32_getfd.gd32_addr,
  1816. (sizeof(struct sockaddr))) ||
  1817. __copy_from_user(&karg->ca_getfd.gd_path,
  1818. &arg->ca32_getfd.gd32_path,
  1819. (NFS_MAXPATHLEN+1)) ||
  1820. __get_user(karg->ca_getfd.gd_version,
  1821. &arg->ca32_getfd.gd32_version))
  1822. return -EFAULT;
  1823. return 0;
  1824. }
  1825. static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
  1826. struct compat_nfsctl_arg __user *arg)
  1827. {
  1828. if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
  1829. get_user(karg->ca_version, &arg->ca32_version) ||
  1830. __copy_from_user(&karg->ca_getfs.gd_addr,
  1831. &arg->ca32_getfs.gd32_addr,
  1832. (sizeof(struct sockaddr))) ||
  1833. __copy_from_user(&karg->ca_getfs.gd_path,
  1834. &arg->ca32_getfs.gd32_path,
  1835. (NFS_MAXPATHLEN+1)) ||
  1836. __get_user(karg->ca_getfs.gd_maxlen,
  1837. &arg->ca32_getfs.gd32_maxlen))
  1838. return -EFAULT;
  1839. return 0;
  1840. }
  1841. /* This really doesn't need translations, we are only passing
  1842. * back a union which contains opaque nfs file handle data.
  1843. */
  1844. static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
  1845. union compat_nfsctl_res __user *res)
  1846. {
  1847. int err;
  1848. err = copy_to_user(res, kres, sizeof(*res));
  1849. return (err) ? -EFAULT : 0;
  1850. }
  1851. asmlinkage long compat_sys_nfsservctl(int cmd,
  1852. struct compat_nfsctl_arg __user *arg,
  1853. union compat_nfsctl_res __user *res)
  1854. {
  1855. struct nfsctl_arg *karg;
  1856. union nfsctl_res *kres;
  1857. mm_segment_t oldfs;
  1858. int err;
  1859. karg = kmalloc(sizeof(*karg), GFP_USER);
  1860. kres = kmalloc(sizeof(*kres), GFP_USER);
  1861. if(!karg || !kres) {
  1862. err = -ENOMEM;
  1863. goto done;
  1864. }
  1865. switch(cmd) {
  1866. case NFSCTL_SVC:
  1867. err = compat_nfs_svc_trans(karg, arg);
  1868. break;
  1869. case NFSCTL_ADDCLIENT:
  1870. err = compat_nfs_clnt_trans(karg, arg);
  1871. break;
  1872. case NFSCTL_DELCLIENT:
  1873. err = compat_nfs_clnt_trans(karg, arg);
  1874. break;
  1875. case NFSCTL_EXPORT:
  1876. case NFSCTL_UNEXPORT:
  1877. err = compat_nfs_exp_trans(karg, arg);
  1878. break;
  1879. case NFSCTL_GETFD:
  1880. err = compat_nfs_getfd_trans(karg, arg);
  1881. break;
  1882. case NFSCTL_GETFS:
  1883. err = compat_nfs_getfs_trans(karg, arg);
  1884. break;
  1885. default:
  1886. err = -EINVAL;
  1887. break;
  1888. }
  1889. if (err)
  1890. goto done;
  1891. oldfs = get_fs();
  1892. set_fs(KERNEL_DS);
  1893. /* The __user pointer casts are valid because of the set_fs() */
  1894. err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
  1895. set_fs(oldfs);
  1896. if (err)
  1897. goto done;
  1898. if((cmd == NFSCTL_GETFD) ||
  1899. (cmd == NFSCTL_GETFS))
  1900. err = compat_nfs_getfh_res_trans(kres, res);
  1901. done:
  1902. kfree(karg);
  1903. kfree(kres);
  1904. return err;
  1905. }
  1906. #else /* !NFSD */
  1907. long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
  1908. {
  1909. return sys_ni_syscall();
  1910. }
  1911. #endif
  1912. #ifdef CONFIG_EPOLL
  1913. #ifdef HAVE_SET_RESTORE_SIGMASK
  1914. asmlinkage long compat_sys_epoll_pwait(int epfd,
  1915. struct compat_epoll_event __user *events,
  1916. int maxevents, int timeout,
  1917. const compat_sigset_t __user *sigmask,
  1918. compat_size_t sigsetsize)
  1919. {
  1920. long err;
  1921. compat_sigset_t csigmask;
  1922. sigset_t ksigmask, sigsaved;
  1923. /*
  1924. * If the caller wants a certain signal mask to be set during the wait,
  1925. * we apply it here.
  1926. */
  1927. if (sigmask) {
  1928. if (sigsetsize != sizeof(compat_sigset_t))
  1929. return -EINVAL;
  1930. if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
  1931. return -EFAULT;
  1932. sigset_from_compat(&ksigmask, &csigmask);
  1933. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1934. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1935. }
  1936. err = sys_epoll_wait(epfd, events, maxevents, timeout);
  1937. /*
  1938. * If we changed the signal mask, we need to restore the original one.
  1939. * In case we've got a signal while waiting, we do not restore the
  1940. * signal mask yet, and we allow do_signal() to deliver the signal on
  1941. * the way back to userspace, before the signal mask is restored.
  1942. */
  1943. if (sigmask) {
  1944. if (err == -EINTR) {
  1945. memcpy(&current->saved_sigmask, &sigsaved,
  1946. sizeof(sigsaved));
  1947. set_restore_sigmask();
  1948. } else
  1949. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1950. }
  1951. return err;
  1952. }
  1953. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1954. #endif /* CONFIG_EPOLL */
  1955. #ifdef CONFIG_SIGNALFD
  1956. asmlinkage long compat_sys_signalfd4(int ufd,
  1957. const compat_sigset_t __user *sigmask,
  1958. compat_size_t sigsetsize, int flags)
  1959. {
  1960. compat_sigset_t ss32;
  1961. sigset_t tmp;
  1962. sigset_t __user *ksigmask;
  1963. if (sigsetsize != sizeof(compat_sigset_t))
  1964. return -EINVAL;
  1965. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1966. return -EFAULT;
  1967. sigset_from_compat(&tmp, &ss32);
  1968. ksigmask = compat_alloc_user_space(sizeof(sigset_t));
  1969. if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
  1970. return -EFAULT;
  1971. return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
  1972. }
  1973. asmlinkage long compat_sys_signalfd(int ufd,
  1974. const compat_sigset_t __user *sigmask,
  1975. compat_size_t sigsetsize)
  1976. {
  1977. return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
  1978. }
  1979. #endif /* CONFIG_SIGNALFD */
  1980. #ifdef CONFIG_TIMERFD
  1981. asmlinkage long compat_sys_timerfd_settime(int ufd, int flags,
  1982. const struct compat_itimerspec __user *utmr,
  1983. struct compat_itimerspec __user *otmr)
  1984. {
  1985. int error;
  1986. struct itimerspec t;
  1987. struct itimerspec __user *ut;
  1988. if (get_compat_itimerspec(&t, utmr))
  1989. return -EFAULT;
  1990. ut = compat_alloc_user_space(2 * sizeof(struct itimerspec));
  1991. if (copy_to_user(&ut[0], &t, sizeof(t)))
  1992. return -EFAULT;
  1993. error = sys_timerfd_settime(ufd, flags, &ut[0], &ut[1]);
  1994. if (!error && otmr)
  1995. error = (copy_from_user(&t, &ut[1], sizeof(struct itimerspec)) ||
  1996. put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
  1997. return error;
  1998. }
  1999. asmlinkage long compat_sys_timerfd_gettime(int ufd,
  2000. struct compat_itimerspec __user *otmr)
  2001. {
  2002. int error;
  2003. struct itimerspec t;
  2004. struct itimerspec __user *ut;
  2005. ut = compat_alloc_user_space(sizeof(struct itimerspec));
  2006. error = sys_timerfd_gettime(ufd, ut);
  2007. if (!error)
  2008. error = (copy_from_user(&t, ut, sizeof(struct itimerspec)) ||
  2009. put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
  2010. return error;
  2011. }
  2012. #endif /* CONFIG_TIMERFD */