compat.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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_path.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_path.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_path.dentry, filp->f_path.mnt, 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_path.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_path.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. error = -EFAULT;
  1007. if (__put_user_unaligned(d_off, &lastdirent->d_off))
  1008. goto out_putf;
  1009. error = count - buf.count;
  1010. }
  1011. out_putf:
  1012. fput(file);
  1013. out:
  1014. return error;
  1015. }
  1016. #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
  1017. static ssize_t compat_do_readv_writev(int type, struct file *file,
  1018. const struct compat_iovec __user *uvector,
  1019. unsigned long nr_segs, loff_t *pos)
  1020. {
  1021. compat_ssize_t tot_len;
  1022. struct iovec iovstack[UIO_FASTIOV];
  1023. struct iovec *iov=iovstack, *vector;
  1024. ssize_t ret;
  1025. int seg;
  1026. io_fn_t fn;
  1027. iov_fn_t fnv;
  1028. /*
  1029. * SuS says "The readv() function *may* fail if the iovcnt argument
  1030. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  1031. * traditionally returned zero for zero segments, so...
  1032. */
  1033. ret = 0;
  1034. if (nr_segs == 0)
  1035. goto out;
  1036. /*
  1037. * First get the "struct iovec" from user memory and
  1038. * verify all the pointers
  1039. */
  1040. ret = -EINVAL;
  1041. if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
  1042. goto out;
  1043. if (!file->f_op)
  1044. goto out;
  1045. if (nr_segs > UIO_FASTIOV) {
  1046. ret = -ENOMEM;
  1047. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  1048. if (!iov)
  1049. goto out;
  1050. }
  1051. ret = -EFAULT;
  1052. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  1053. goto out;
  1054. /*
  1055. * Single unix specification:
  1056. * We should -EINVAL if an element length is not >= 0 and fitting an
  1057. * ssize_t. The total length is fitting an ssize_t
  1058. *
  1059. * Be careful here because iov_len is a size_t not an ssize_t
  1060. */
  1061. tot_len = 0;
  1062. vector = iov;
  1063. ret = -EINVAL;
  1064. for (seg = 0 ; seg < nr_segs; seg++) {
  1065. compat_ssize_t tmp = tot_len;
  1066. compat_ssize_t len;
  1067. compat_uptr_t buf;
  1068. if (__get_user(len, &uvector->iov_len) ||
  1069. __get_user(buf, &uvector->iov_base)) {
  1070. ret = -EFAULT;
  1071. goto out;
  1072. }
  1073. if (len < 0) /* size_t not fitting an compat_ssize_t .. */
  1074. goto out;
  1075. tot_len += len;
  1076. if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
  1077. goto out;
  1078. vector->iov_base = compat_ptr(buf);
  1079. vector->iov_len = (compat_size_t) len;
  1080. uvector++;
  1081. vector++;
  1082. }
  1083. if (tot_len == 0) {
  1084. ret = 0;
  1085. goto out;
  1086. }
  1087. ret = rw_verify_area(type, file, pos, tot_len);
  1088. if (ret < 0)
  1089. goto out;
  1090. ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE);
  1091. if (ret)
  1092. goto out;
  1093. fnv = NULL;
  1094. if (type == READ) {
  1095. fn = file->f_op->read;
  1096. fnv = file->f_op->aio_read;
  1097. } else {
  1098. fn = (io_fn_t)file->f_op->write;
  1099. fnv = file->f_op->aio_write;
  1100. }
  1101. if (fnv)
  1102. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  1103. pos, fnv);
  1104. else
  1105. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  1106. out:
  1107. if (iov != iovstack)
  1108. kfree(iov);
  1109. if ((ret + (type == READ)) > 0) {
  1110. struct dentry *dentry = file->f_path.dentry;
  1111. if (type == READ)
  1112. fsnotify_access(dentry);
  1113. else
  1114. fsnotify_modify(dentry);
  1115. }
  1116. return ret;
  1117. }
  1118. asmlinkage ssize_t
  1119. compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
  1120. {
  1121. struct file *file;
  1122. ssize_t ret = -EBADF;
  1123. file = fget(fd);
  1124. if (!file)
  1125. return -EBADF;
  1126. if (!(file->f_mode & FMODE_READ))
  1127. goto out;
  1128. ret = -EINVAL;
  1129. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  1130. goto out;
  1131. ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
  1132. out:
  1133. fput(file);
  1134. return ret;
  1135. }
  1136. asmlinkage ssize_t
  1137. compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
  1138. {
  1139. struct file *file;
  1140. ssize_t ret = -EBADF;
  1141. file = fget(fd);
  1142. if (!file)
  1143. return -EBADF;
  1144. if (!(file->f_mode & FMODE_WRITE))
  1145. goto out;
  1146. ret = -EINVAL;
  1147. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  1148. goto out;
  1149. ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
  1150. out:
  1151. fput(file);
  1152. return ret;
  1153. }
  1154. asmlinkage long
  1155. compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
  1156. unsigned int nr_segs, unsigned int flags)
  1157. {
  1158. unsigned i;
  1159. struct iovec __user *iov;
  1160. if (nr_segs > UIO_MAXIOV)
  1161. return -EINVAL;
  1162. iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  1163. for (i = 0; i < nr_segs; i++) {
  1164. struct compat_iovec v;
  1165. if (get_user(v.iov_base, &iov32[i].iov_base) ||
  1166. get_user(v.iov_len, &iov32[i].iov_len) ||
  1167. put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  1168. put_user(v.iov_len, &iov[i].iov_len))
  1169. return -EFAULT;
  1170. }
  1171. return sys_vmsplice(fd, iov, nr_segs, flags);
  1172. }
  1173. /*
  1174. * Exactly like fs/open.c:sys_open(), except that it doesn't set the
  1175. * O_LARGEFILE flag.
  1176. */
  1177. asmlinkage long
  1178. compat_sys_open(const char __user *filename, int flags, int mode)
  1179. {
  1180. return do_sys_open(AT_FDCWD, filename, flags, mode);
  1181. }
  1182. /*
  1183. * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
  1184. * O_LARGEFILE flag.
  1185. */
  1186. asmlinkage long
  1187. compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
  1188. {
  1189. return do_sys_open(dfd, filename, flags, mode);
  1190. }
  1191. /*
  1192. * compat_count() counts the number of arguments/envelopes. It is basically
  1193. * a copy of count() from fs/exec.c, except that it works with 32 bit argv
  1194. * and envp pointers.
  1195. */
  1196. static int compat_count(compat_uptr_t __user *argv, int max)
  1197. {
  1198. int i = 0;
  1199. if (argv != NULL) {
  1200. for (;;) {
  1201. compat_uptr_t p;
  1202. if (get_user(p, argv))
  1203. return -EFAULT;
  1204. if (!p)
  1205. break;
  1206. argv++;
  1207. if(++i > max)
  1208. return -E2BIG;
  1209. }
  1210. }
  1211. return i;
  1212. }
  1213. /*
  1214. * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
  1215. * except that it works with 32 bit argv and envp pointers.
  1216. */
  1217. static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
  1218. struct linux_binprm *bprm)
  1219. {
  1220. struct page *kmapped_page = NULL;
  1221. char *kaddr = NULL;
  1222. int ret;
  1223. while (argc-- > 0) {
  1224. compat_uptr_t str;
  1225. int len;
  1226. unsigned long pos;
  1227. if (get_user(str, argv+argc) ||
  1228. !(len = strnlen_user(compat_ptr(str), bprm->p))) {
  1229. ret = -EFAULT;
  1230. goto out;
  1231. }
  1232. if (bprm->p < len) {
  1233. ret = -E2BIG;
  1234. goto out;
  1235. }
  1236. bprm->p -= len;
  1237. /* XXX: add architecture specific overflow check here. */
  1238. pos = bprm->p;
  1239. while (len > 0) {
  1240. int i, new, err;
  1241. int offset, bytes_to_copy;
  1242. struct page *page;
  1243. offset = pos % PAGE_SIZE;
  1244. i = pos/PAGE_SIZE;
  1245. page = bprm->page[i];
  1246. new = 0;
  1247. if (!page) {
  1248. page = alloc_page(GFP_HIGHUSER);
  1249. bprm->page[i] = page;
  1250. if (!page) {
  1251. ret = -ENOMEM;
  1252. goto out;
  1253. }
  1254. new = 1;
  1255. }
  1256. if (page != kmapped_page) {
  1257. if (kmapped_page)
  1258. kunmap(kmapped_page);
  1259. kmapped_page = page;
  1260. kaddr = kmap(kmapped_page);
  1261. }
  1262. if (new && offset)
  1263. memset(kaddr, 0, offset);
  1264. bytes_to_copy = PAGE_SIZE - offset;
  1265. if (bytes_to_copy > len) {
  1266. bytes_to_copy = len;
  1267. if (new)
  1268. memset(kaddr+offset+len, 0,
  1269. PAGE_SIZE-offset-len);
  1270. }
  1271. err = copy_from_user(kaddr+offset, compat_ptr(str),
  1272. bytes_to_copy);
  1273. if (err) {
  1274. ret = -EFAULT;
  1275. goto out;
  1276. }
  1277. pos += bytes_to_copy;
  1278. str += bytes_to_copy;
  1279. len -= bytes_to_copy;
  1280. }
  1281. }
  1282. ret = 0;
  1283. out:
  1284. if (kmapped_page)
  1285. kunmap(kmapped_page);
  1286. return ret;
  1287. }
  1288. #ifdef CONFIG_MMU
  1289. #define free_arg_pages(bprm) do { } while (0)
  1290. #else
  1291. static inline void free_arg_pages(struct linux_binprm *bprm)
  1292. {
  1293. int i;
  1294. for (i = 0; i < MAX_ARG_PAGES; i++) {
  1295. if (bprm->page[i])
  1296. __free_page(bprm->page[i]);
  1297. bprm->page[i] = NULL;
  1298. }
  1299. }
  1300. #endif /* CONFIG_MMU */
  1301. /*
  1302. * compat_do_execve() is mostly a copy of do_execve(), with the exception
  1303. * that it processes 32 bit argv and envp pointers.
  1304. */
  1305. int compat_do_execve(char * filename,
  1306. compat_uptr_t __user *argv,
  1307. compat_uptr_t __user *envp,
  1308. struct pt_regs * regs)
  1309. {
  1310. struct linux_binprm *bprm;
  1311. struct file *file;
  1312. int retval;
  1313. int i;
  1314. retval = -ENOMEM;
  1315. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1316. if (!bprm)
  1317. goto out_ret;
  1318. file = open_exec(filename);
  1319. retval = PTR_ERR(file);
  1320. if (IS_ERR(file))
  1321. goto out_kfree;
  1322. sched_exec();
  1323. bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  1324. bprm->file = file;
  1325. bprm->filename = filename;
  1326. bprm->interp = filename;
  1327. bprm->mm = mm_alloc();
  1328. retval = -ENOMEM;
  1329. if (!bprm->mm)
  1330. goto out_file;
  1331. retval = init_new_context(current, bprm->mm);
  1332. if (retval < 0)
  1333. goto out_mm;
  1334. bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
  1335. if ((retval = bprm->argc) < 0)
  1336. goto out_mm;
  1337. bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
  1338. if ((retval = bprm->envc) < 0)
  1339. goto out_mm;
  1340. retval = security_bprm_alloc(bprm);
  1341. if (retval)
  1342. goto out;
  1343. retval = prepare_binprm(bprm);
  1344. if (retval < 0)
  1345. goto out;
  1346. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1347. if (retval < 0)
  1348. goto out;
  1349. bprm->exec = bprm->p;
  1350. retval = compat_copy_strings(bprm->envc, envp, bprm);
  1351. if (retval < 0)
  1352. goto out;
  1353. retval = compat_copy_strings(bprm->argc, argv, bprm);
  1354. if (retval < 0)
  1355. goto out;
  1356. retval = search_binary_handler(bprm, regs);
  1357. if (retval >= 0) {
  1358. free_arg_pages(bprm);
  1359. /* execve success */
  1360. security_bprm_free(bprm);
  1361. acct_update_integrals(current);
  1362. kfree(bprm);
  1363. return retval;
  1364. }
  1365. out:
  1366. /* Something went wrong, return the inode and free the argument pages*/
  1367. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  1368. struct page * page = bprm->page[i];
  1369. if (page)
  1370. __free_page(page);
  1371. }
  1372. if (bprm->security)
  1373. security_bprm_free(bprm);
  1374. out_mm:
  1375. if (bprm->mm)
  1376. mmdrop(bprm->mm);
  1377. out_file:
  1378. if (bprm->file) {
  1379. allow_write_access(bprm->file);
  1380. fput(bprm->file);
  1381. }
  1382. out_kfree:
  1383. kfree(bprm);
  1384. out_ret:
  1385. return retval;
  1386. }
  1387. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  1388. #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
  1389. /*
  1390. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  1391. * 64-bit unsigned longs.
  1392. */
  1393. static
  1394. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1395. unsigned long *fdset)
  1396. {
  1397. nr = ROUND_UP(nr, __COMPAT_NFDBITS);
  1398. if (ufdset) {
  1399. unsigned long odd;
  1400. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  1401. return -EFAULT;
  1402. odd = nr & 1UL;
  1403. nr &= ~1UL;
  1404. while (nr) {
  1405. unsigned long h, l;
  1406. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  1407. return -EFAULT;
  1408. ufdset += 2;
  1409. *fdset++ = h << 32 | l;
  1410. nr -= 2;
  1411. }
  1412. if (odd && __get_user(*fdset, ufdset))
  1413. return -EFAULT;
  1414. } else {
  1415. /* Tricky, must clear full unsigned long in the
  1416. * kernel fdset at the end, this makes sure that
  1417. * actually happens.
  1418. */
  1419. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  1420. }
  1421. return 0;
  1422. }
  1423. static
  1424. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1425. unsigned long *fdset)
  1426. {
  1427. unsigned long odd;
  1428. nr = ROUND_UP(nr, __COMPAT_NFDBITS);
  1429. if (!ufdset)
  1430. return 0;
  1431. odd = nr & 1UL;
  1432. nr &= ~1UL;
  1433. while (nr) {
  1434. unsigned long h, l;
  1435. l = *fdset++;
  1436. h = l >> 32;
  1437. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1438. return -EFAULT;
  1439. ufdset += 2;
  1440. nr -= 2;
  1441. }
  1442. if (odd && __put_user(*fdset, ufdset))
  1443. return -EFAULT;
  1444. return 0;
  1445. }
  1446. /*
  1447. * This is a virtual copy of sys_select from fs/select.c and probably
  1448. * should be compared to it from time to time
  1449. */
  1450. /*
  1451. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1452. * like to be certain this leads to no problems. So I return
  1453. * EINTR just for safety.
  1454. *
  1455. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1456. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1457. */
  1458. #define MAX_SELECT_SECONDS \
  1459. ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
  1460. int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1461. compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout)
  1462. {
  1463. fd_set_bits fds;
  1464. char *bits;
  1465. int size, max_fds, ret = -EINVAL;
  1466. struct fdtable *fdt;
  1467. if (n < 0)
  1468. goto out_nofds;
  1469. /* max_fds can increase, so grab it once to avoid race */
  1470. rcu_read_lock();
  1471. fdt = files_fdtable(current->files);
  1472. max_fds = fdt->max_fds;
  1473. rcu_read_unlock();
  1474. if (n > max_fds)
  1475. n = max_fds;
  1476. /*
  1477. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1478. * since we used fdset we need to allocate memory in units of
  1479. * long-words.
  1480. */
  1481. ret = -ENOMEM;
  1482. size = FDS_BYTES(n);
  1483. bits = kmalloc(6 * size, GFP_KERNEL);
  1484. if (!bits)
  1485. goto out_nofds;
  1486. fds.in = (unsigned long *) bits;
  1487. fds.out = (unsigned long *) (bits + size);
  1488. fds.ex = (unsigned long *) (bits + 2*size);
  1489. fds.res_in = (unsigned long *) (bits + 3*size);
  1490. fds.res_out = (unsigned long *) (bits + 4*size);
  1491. fds.res_ex = (unsigned long *) (bits + 5*size);
  1492. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1493. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1494. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1495. goto out;
  1496. zero_fd_set(n, fds.res_in);
  1497. zero_fd_set(n, fds.res_out);
  1498. zero_fd_set(n, fds.res_ex);
  1499. ret = do_select(n, &fds, timeout);
  1500. if (ret < 0)
  1501. goto out;
  1502. if (!ret) {
  1503. ret = -ERESTARTNOHAND;
  1504. if (signal_pending(current))
  1505. goto out;
  1506. ret = 0;
  1507. }
  1508. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1509. compat_set_fd_set(n, outp, fds.res_out) ||
  1510. compat_set_fd_set(n, exp, fds.res_ex))
  1511. ret = -EFAULT;
  1512. out:
  1513. kfree(bits);
  1514. out_nofds:
  1515. return ret;
  1516. }
  1517. asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
  1518. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1519. struct compat_timeval __user *tvp)
  1520. {
  1521. s64 timeout = -1;
  1522. struct compat_timeval tv;
  1523. int ret;
  1524. if (tvp) {
  1525. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1526. return -EFAULT;
  1527. if (tv.tv_sec < 0 || tv.tv_usec < 0)
  1528. return -EINVAL;
  1529. /* Cast to u64 to make GCC stop complaining */
  1530. if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
  1531. timeout = -1; /* infinite */
  1532. else {
  1533. timeout = ROUND_UP(tv.tv_usec, 1000000/HZ);
  1534. timeout += tv.tv_sec * HZ;
  1535. }
  1536. }
  1537. ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
  1538. if (tvp) {
  1539. struct compat_timeval rtv;
  1540. if (current->personality & STICKY_TIMEOUTS)
  1541. goto sticky;
  1542. rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
  1543. rtv.tv_sec = timeout;
  1544. if (compat_timeval_compare(&rtv, &tv) >= 0)
  1545. rtv = tv;
  1546. if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
  1547. sticky:
  1548. /*
  1549. * If an application puts its timeval in read-only
  1550. * memory, we don't want the Linux-specific update to
  1551. * the timeval to cause a fault after the select has
  1552. * completed successfully. However, because we're not
  1553. * updating the timeval, we can't restart the system
  1554. * call.
  1555. */
  1556. if (ret == -ERESTARTNOHAND)
  1557. ret = -EINTR;
  1558. }
  1559. }
  1560. return ret;
  1561. }
  1562. #ifdef TIF_RESTORE_SIGMASK
  1563. asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp,
  1564. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1565. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1566. compat_size_t sigsetsize)
  1567. {
  1568. compat_sigset_t ss32;
  1569. sigset_t ksigmask, sigsaved;
  1570. s64 timeout = MAX_SCHEDULE_TIMEOUT;
  1571. struct compat_timespec ts;
  1572. int ret;
  1573. if (tsp) {
  1574. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1575. return -EFAULT;
  1576. if (ts.tv_sec < 0 || ts.tv_nsec < 0)
  1577. return -EINVAL;
  1578. }
  1579. if (sigmask) {
  1580. if (sigsetsize != sizeof(compat_sigset_t))
  1581. return -EINVAL;
  1582. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1583. return -EFAULT;
  1584. sigset_from_compat(&ksigmask, &ss32);
  1585. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1586. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1587. }
  1588. do {
  1589. if (tsp) {
  1590. if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) {
  1591. timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
  1592. timeout += ts.tv_sec * (unsigned long)HZ;
  1593. ts.tv_sec = 0;
  1594. ts.tv_nsec = 0;
  1595. } else {
  1596. ts.tv_sec -= MAX_SELECT_SECONDS;
  1597. timeout = MAX_SELECT_SECONDS * HZ;
  1598. }
  1599. }
  1600. ret = compat_core_sys_select(n, inp, outp, exp, &timeout);
  1601. } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec));
  1602. if (tsp) {
  1603. struct compat_timespec rts;
  1604. if (current->personality & STICKY_TIMEOUTS)
  1605. goto sticky;
  1606. rts.tv_sec = timeout / HZ;
  1607. rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ);
  1608. if (rts.tv_nsec >= NSEC_PER_SEC) {
  1609. rts.tv_sec++;
  1610. rts.tv_nsec -= NSEC_PER_SEC;
  1611. }
  1612. if (compat_timespec_compare(&rts, &ts) >= 0)
  1613. rts = ts;
  1614. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  1615. sticky:
  1616. /*
  1617. * If an application puts its timeval in read-only
  1618. * memory, we don't want the Linux-specific update to
  1619. * the timeval to cause a fault after the select has
  1620. * completed successfully. However, because we're not
  1621. * updating the timeval, we can't restart the system
  1622. * call.
  1623. */
  1624. if (ret == -ERESTARTNOHAND)
  1625. ret = -EINTR;
  1626. }
  1627. }
  1628. if (ret == -ERESTARTNOHAND) {
  1629. /*
  1630. * Don't restore the signal mask yet. Let do_signal() deliver
  1631. * the signal on the way back to userspace, before the signal
  1632. * mask is restored.
  1633. */
  1634. if (sigmask) {
  1635. memcpy(&current->saved_sigmask, &sigsaved,
  1636. sizeof(sigsaved));
  1637. set_thread_flag(TIF_RESTORE_SIGMASK);
  1638. }
  1639. } else if (sigmask)
  1640. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1641. return ret;
  1642. }
  1643. asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
  1644. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1645. struct compat_timespec __user *tsp, void __user *sig)
  1646. {
  1647. compat_size_t sigsetsize = 0;
  1648. compat_uptr_t up = 0;
  1649. if (sig) {
  1650. if (!access_ok(VERIFY_READ, sig,
  1651. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1652. __get_user(up, (compat_uptr_t __user *)sig) ||
  1653. __get_user(sigsetsize,
  1654. (compat_size_t __user *)(sig+sizeof(up))))
  1655. return -EFAULT;
  1656. }
  1657. return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up),
  1658. sigsetsize);
  1659. }
  1660. asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
  1661. unsigned int nfds, struct compat_timespec __user *tsp,
  1662. const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
  1663. {
  1664. compat_sigset_t ss32;
  1665. sigset_t ksigmask, sigsaved;
  1666. struct compat_timespec ts;
  1667. s64 timeout = -1;
  1668. int ret;
  1669. if (tsp) {
  1670. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1671. return -EFAULT;
  1672. /* We assume that ts.tv_sec is always lower than
  1673. the number of seconds that can be expressed in
  1674. an s64. Otherwise the compiler bitches at us */
  1675. timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ);
  1676. timeout += ts.tv_sec * HZ;
  1677. }
  1678. if (sigmask) {
  1679. if (sigsetsize != sizeof(compat_sigset_t))
  1680. return -EINVAL;
  1681. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1682. return -EFAULT;
  1683. sigset_from_compat(&ksigmask, &ss32);
  1684. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1685. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1686. }
  1687. ret = do_sys_poll(ufds, nfds, &timeout);
  1688. /* We can restart this syscall, usually */
  1689. if (ret == -EINTR) {
  1690. /*
  1691. * Don't restore the signal mask yet. Let do_signal() deliver
  1692. * the signal on the way back to userspace, before the signal
  1693. * mask is restored.
  1694. */
  1695. if (sigmask) {
  1696. memcpy(&current->saved_sigmask, &sigsaved,
  1697. sizeof(sigsaved));
  1698. set_thread_flag(TIF_RESTORE_SIGMASK);
  1699. }
  1700. ret = -ERESTARTNOHAND;
  1701. } else if (sigmask)
  1702. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1703. if (tsp && timeout >= 0) {
  1704. struct compat_timespec rts;
  1705. if (current->personality & STICKY_TIMEOUTS)
  1706. goto sticky;
  1707. /* Yes, we know it's actually an s64, but it's also positive. */
  1708. rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) *
  1709. 1000;
  1710. rts.tv_sec = timeout;
  1711. if (compat_timespec_compare(&rts, &ts) >= 0)
  1712. rts = ts;
  1713. if (copy_to_user(tsp, &rts, sizeof(rts))) {
  1714. sticky:
  1715. /*
  1716. * If an application puts its timeval in read-only
  1717. * memory, we don't want the Linux-specific update to
  1718. * the timeval to cause a fault after the select has
  1719. * completed successfully. However, because we're not
  1720. * updating the timeval, we can't restart the system
  1721. * call.
  1722. */
  1723. if (ret == -ERESTARTNOHAND && timeout >= 0)
  1724. ret = -EINTR;
  1725. }
  1726. }
  1727. return ret;
  1728. }
  1729. #endif /* TIF_RESTORE_SIGMASK */
  1730. #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
  1731. /* Stuff for NFS server syscalls... */
  1732. struct compat_nfsctl_svc {
  1733. u16 svc32_port;
  1734. s32 svc32_nthreads;
  1735. };
  1736. struct compat_nfsctl_client {
  1737. s8 cl32_ident[NFSCLNT_IDMAX+1];
  1738. s32 cl32_naddr;
  1739. struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
  1740. s32 cl32_fhkeytype;
  1741. s32 cl32_fhkeylen;
  1742. u8 cl32_fhkey[NFSCLNT_KEYMAX];
  1743. };
  1744. struct compat_nfsctl_export {
  1745. char ex32_client[NFSCLNT_IDMAX+1];
  1746. char ex32_path[NFS_MAXPATHLEN+1];
  1747. compat_dev_t ex32_dev;
  1748. compat_ino_t ex32_ino;
  1749. compat_int_t ex32_flags;
  1750. __compat_uid_t ex32_anon_uid;
  1751. __compat_gid_t ex32_anon_gid;
  1752. };
  1753. struct compat_nfsctl_fdparm {
  1754. struct sockaddr gd32_addr;
  1755. s8 gd32_path[NFS_MAXPATHLEN+1];
  1756. compat_int_t gd32_version;
  1757. };
  1758. struct compat_nfsctl_fsparm {
  1759. struct sockaddr gd32_addr;
  1760. s8 gd32_path[NFS_MAXPATHLEN+1];
  1761. compat_int_t gd32_maxlen;
  1762. };
  1763. struct compat_nfsctl_arg {
  1764. compat_int_t ca32_version; /* safeguard */
  1765. union {
  1766. struct compat_nfsctl_svc u32_svc;
  1767. struct compat_nfsctl_client u32_client;
  1768. struct compat_nfsctl_export u32_export;
  1769. struct compat_nfsctl_fdparm u32_getfd;
  1770. struct compat_nfsctl_fsparm u32_getfs;
  1771. } u;
  1772. #define ca32_svc u.u32_svc
  1773. #define ca32_client u.u32_client
  1774. #define ca32_export u.u32_export
  1775. #define ca32_getfd u.u32_getfd
  1776. #define ca32_getfs u.u32_getfs
  1777. };
  1778. union compat_nfsctl_res {
  1779. __u8 cr32_getfh[NFS_FHSIZE];
  1780. struct knfsd_fh cr32_getfs;
  1781. };
  1782. static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
  1783. struct compat_nfsctl_arg __user *arg)
  1784. {
  1785. if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) ||
  1786. get_user(karg->ca_version, &arg->ca32_version) ||
  1787. __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) ||
  1788. __get_user(karg->ca_svc.svc_nthreads,
  1789. &arg->ca32_svc.svc32_nthreads))
  1790. return -EFAULT;
  1791. return 0;
  1792. }
  1793. static int compat_nfs_clnt_trans(struct nfsctl_arg *karg,
  1794. struct compat_nfsctl_arg __user *arg)
  1795. {
  1796. if (!access_ok(VERIFY_READ, &arg->ca32_client,
  1797. sizeof(arg->ca32_client)) ||
  1798. get_user(karg->ca_version, &arg->ca32_version) ||
  1799. __copy_from_user(&karg->ca_client.cl_ident[0],
  1800. &arg->ca32_client.cl32_ident[0],
  1801. NFSCLNT_IDMAX) ||
  1802. __get_user(karg->ca_client.cl_naddr,
  1803. &arg->ca32_client.cl32_naddr) ||
  1804. __copy_from_user(&karg->ca_client.cl_addrlist[0],
  1805. &arg->ca32_client.cl32_addrlist[0],
  1806. (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) ||
  1807. __get_user(karg->ca_client.cl_fhkeytype,
  1808. &arg->ca32_client.cl32_fhkeytype) ||
  1809. __get_user(karg->ca_client.cl_fhkeylen,
  1810. &arg->ca32_client.cl32_fhkeylen) ||
  1811. __copy_from_user(&karg->ca_client.cl_fhkey[0],
  1812. &arg->ca32_client.cl32_fhkey[0],
  1813. NFSCLNT_KEYMAX))
  1814. return -EFAULT;
  1815. return 0;
  1816. }
  1817. static int compat_nfs_exp_trans(struct nfsctl_arg *karg,
  1818. struct compat_nfsctl_arg __user *arg)
  1819. {
  1820. if (!access_ok(VERIFY_READ, &arg->ca32_export,
  1821. sizeof(arg->ca32_export)) ||
  1822. get_user(karg->ca_version, &arg->ca32_version) ||
  1823. __copy_from_user(&karg->ca_export.ex_client[0],
  1824. &arg->ca32_export.ex32_client[0],
  1825. NFSCLNT_IDMAX) ||
  1826. __copy_from_user(&karg->ca_export.ex_path[0],
  1827. &arg->ca32_export.ex32_path[0],
  1828. NFS_MAXPATHLEN) ||
  1829. __get_user(karg->ca_export.ex_dev,
  1830. &arg->ca32_export.ex32_dev) ||
  1831. __get_user(karg->ca_export.ex_ino,
  1832. &arg->ca32_export.ex32_ino) ||
  1833. __get_user(karg->ca_export.ex_flags,
  1834. &arg->ca32_export.ex32_flags) ||
  1835. __get_user(karg->ca_export.ex_anon_uid,
  1836. &arg->ca32_export.ex32_anon_uid) ||
  1837. __get_user(karg->ca_export.ex_anon_gid,
  1838. &arg->ca32_export.ex32_anon_gid))
  1839. return -EFAULT;
  1840. SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
  1841. SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
  1842. return 0;
  1843. }
  1844. static int compat_nfs_getfd_trans(struct nfsctl_arg *karg,
  1845. struct compat_nfsctl_arg __user *arg)
  1846. {
  1847. if (!access_ok(VERIFY_READ, &arg->ca32_getfd,
  1848. sizeof(arg->ca32_getfd)) ||
  1849. get_user(karg->ca_version, &arg->ca32_version) ||
  1850. __copy_from_user(&karg->ca_getfd.gd_addr,
  1851. &arg->ca32_getfd.gd32_addr,
  1852. (sizeof(struct sockaddr))) ||
  1853. __copy_from_user(&karg->ca_getfd.gd_path,
  1854. &arg->ca32_getfd.gd32_path,
  1855. (NFS_MAXPATHLEN+1)) ||
  1856. __get_user(karg->ca_getfd.gd_version,
  1857. &arg->ca32_getfd.gd32_version))
  1858. return -EFAULT;
  1859. return 0;
  1860. }
  1861. static int compat_nfs_getfs_trans(struct nfsctl_arg *karg,
  1862. struct compat_nfsctl_arg __user *arg)
  1863. {
  1864. if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) ||
  1865. get_user(karg->ca_version, &arg->ca32_version) ||
  1866. __copy_from_user(&karg->ca_getfs.gd_addr,
  1867. &arg->ca32_getfs.gd32_addr,
  1868. (sizeof(struct sockaddr))) ||
  1869. __copy_from_user(&karg->ca_getfs.gd_path,
  1870. &arg->ca32_getfs.gd32_path,
  1871. (NFS_MAXPATHLEN+1)) ||
  1872. __get_user(karg->ca_getfs.gd_maxlen,
  1873. &arg->ca32_getfs.gd32_maxlen))
  1874. return -EFAULT;
  1875. return 0;
  1876. }
  1877. /* This really doesn't need translations, we are only passing
  1878. * back a union which contains opaque nfs file handle data.
  1879. */
  1880. static int compat_nfs_getfh_res_trans(union nfsctl_res *kres,
  1881. union compat_nfsctl_res __user *res)
  1882. {
  1883. int err;
  1884. err = copy_to_user(res, kres, sizeof(*res));
  1885. return (err) ? -EFAULT : 0;
  1886. }
  1887. asmlinkage long compat_sys_nfsservctl(int cmd,
  1888. struct compat_nfsctl_arg __user *arg,
  1889. union compat_nfsctl_res __user *res)
  1890. {
  1891. struct nfsctl_arg *karg;
  1892. union nfsctl_res *kres;
  1893. mm_segment_t oldfs;
  1894. int err;
  1895. karg = kmalloc(sizeof(*karg), GFP_USER);
  1896. kres = kmalloc(sizeof(*kres), GFP_USER);
  1897. if(!karg || !kres) {
  1898. err = -ENOMEM;
  1899. goto done;
  1900. }
  1901. switch(cmd) {
  1902. case NFSCTL_SVC:
  1903. err = compat_nfs_svc_trans(karg, arg);
  1904. break;
  1905. case NFSCTL_ADDCLIENT:
  1906. err = compat_nfs_clnt_trans(karg, arg);
  1907. break;
  1908. case NFSCTL_DELCLIENT:
  1909. err = compat_nfs_clnt_trans(karg, arg);
  1910. break;
  1911. case NFSCTL_EXPORT:
  1912. case NFSCTL_UNEXPORT:
  1913. err = compat_nfs_exp_trans(karg, arg);
  1914. break;
  1915. case NFSCTL_GETFD:
  1916. err = compat_nfs_getfd_trans(karg, arg);
  1917. break;
  1918. case NFSCTL_GETFS:
  1919. err = compat_nfs_getfs_trans(karg, arg);
  1920. break;
  1921. default:
  1922. err = -EINVAL;
  1923. break;
  1924. }
  1925. if (err)
  1926. goto done;
  1927. oldfs = get_fs();
  1928. set_fs(KERNEL_DS);
  1929. /* The __user pointer casts are valid because of the set_fs() */
  1930. err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
  1931. set_fs(oldfs);
  1932. if (err)
  1933. goto done;
  1934. if((cmd == NFSCTL_GETFD) ||
  1935. (cmd == NFSCTL_GETFS))
  1936. err = compat_nfs_getfh_res_trans(kres, res);
  1937. done:
  1938. kfree(karg);
  1939. kfree(kres);
  1940. return err;
  1941. }
  1942. #else /* !NFSD */
  1943. long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
  1944. {
  1945. return sys_ni_syscall();
  1946. }
  1947. #endif