compat.c 45 KB

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