compat.c 54 KB

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