compat.c 53 KB

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