compat.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. * linux/fs/compat.c
  3. *
  4. * Kernel compatibililty routines for e.g. 32 bit syscall support
  5. * on 64 bit kernels.
  6. *
  7. * Copyright (C) 2002 Stephen Rothwell, IBM Corporation
  8. * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
  9. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  10. * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
  11. * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/stddef.h>
  18. #include <linux/kernel.h>
  19. #include <linux/linkage.h>
  20. #include <linux/compat.h>
  21. #include <linux/errno.h>
  22. #include <linux/time.h>
  23. #include <linux/fs.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/namei.h>
  26. #include <linux/file.h>
  27. #include <linux/fdtable.h>
  28. #include <linux/vfs.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/init.h>
  31. #include <linux/ncp_mount.h>
  32. #include <linux/nfs4_mount.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/ctype.h>
  35. #include <linux/dirent.h>
  36. #include <linux/fsnotify.h>
  37. #include <linux/highuid.h>
  38. #include <linux/personality.h>
  39. #include <linux/rwsem.h>
  40. #include <linux/tsacct_kern.h>
  41. #include <linux/security.h>
  42. #include <linux/highmem.h>
  43. #include <linux/signal.h>
  44. #include <linux/poll.h>
  45. #include <linux/mm.h>
  46. #include <linux/fs_struct.h>
  47. #include <linux/slab.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/aio.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/mmu_context.h>
  52. #include <asm/ioctls.h>
  53. #include "internal.h"
  54. int compat_log = 1;
  55. int compat_printk(const char *fmt, ...)
  56. {
  57. va_list ap;
  58. int ret;
  59. if (!compat_log)
  60. return 0;
  61. va_start(ap, fmt);
  62. ret = vprintk(fmt, ap);
  63. va_end(ap);
  64. return ret;
  65. }
  66. /*
  67. * Not all architectures have sys_utime, so implement this in terms
  68. * of sys_utimes.
  69. */
  70. COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
  71. struct compat_utimbuf __user *, t)
  72. {
  73. struct timespec tv[2];
  74. if (t) {
  75. if (get_user(tv[0].tv_sec, &t->actime) ||
  76. get_user(tv[1].tv_sec, &t->modtime))
  77. return -EFAULT;
  78. tv[0].tv_nsec = 0;
  79. tv[1].tv_nsec = 0;
  80. }
  81. return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  82. }
  83. COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
  84. {
  85. struct timespec tv[2];
  86. if (t) {
  87. if (compat_get_timespec(&tv[0], &t[0]) ||
  88. compat_get_timespec(&tv[1], &t[1]))
  89. return -EFAULT;
  90. if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
  91. return 0;
  92. }
  93. return do_utimes(dfd, filename, t ? tv : NULL, flags);
  94. }
  95. COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
  96. {
  97. struct timespec tv[2];
  98. if (t) {
  99. if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
  100. get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
  101. get_user(tv[1].tv_sec, &t[1].tv_sec) ||
  102. get_user(tv[1].tv_nsec, &t[1].tv_usec))
  103. return -EFAULT;
  104. if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
  105. tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
  106. return -EINVAL;
  107. tv[0].tv_nsec *= 1000;
  108. tv[1].tv_nsec *= 1000;
  109. }
  110. return do_utimes(dfd, filename, t ? tv : NULL, 0);
  111. }
  112. COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t)
  113. {
  114. return compat_sys_futimesat(AT_FDCWD, filename, t);
  115. }
  116. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  117. {
  118. struct compat_stat tmp;
  119. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  120. return -EOVERFLOW;
  121. memset(&tmp, 0, sizeof(tmp));
  122. tmp.st_dev = old_encode_dev(stat->dev);
  123. tmp.st_ino = stat->ino;
  124. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  125. return -EOVERFLOW;
  126. tmp.st_mode = stat->mode;
  127. tmp.st_nlink = stat->nlink;
  128. if (tmp.st_nlink != stat->nlink)
  129. return -EOVERFLOW;
  130. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  131. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  132. tmp.st_rdev = old_encode_dev(stat->rdev);
  133. if ((u64) stat->size > MAX_NON_LFS)
  134. return -EOVERFLOW;
  135. tmp.st_size = stat->size;
  136. tmp.st_atime = stat->atime.tv_sec;
  137. tmp.st_atime_nsec = stat->atime.tv_nsec;
  138. tmp.st_mtime = stat->mtime.tv_sec;
  139. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  140. tmp.st_ctime = stat->ctime.tv_sec;
  141. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  142. tmp.st_blocks = stat->blocks;
  143. tmp.st_blksize = stat->blksize;
  144. return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  145. }
  146. COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
  147. struct compat_stat __user *, statbuf)
  148. {
  149. struct kstat stat;
  150. int error;
  151. error = vfs_stat(filename, &stat);
  152. if (error)
  153. return error;
  154. return cp_compat_stat(&stat, statbuf);
  155. }
  156. COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  157. struct compat_stat __user *, statbuf)
  158. {
  159. struct kstat stat;
  160. int error;
  161. error = vfs_lstat(filename, &stat);
  162. if (error)
  163. return error;
  164. return cp_compat_stat(&stat, statbuf);
  165. }
  166. #ifndef __ARCH_WANT_STAT64
  167. COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
  168. const char __user *, filename,
  169. struct compat_stat __user *, statbuf, int, flag)
  170. {
  171. struct kstat stat;
  172. int error;
  173. error = vfs_fstatat(dfd, filename, &stat, flag);
  174. if (error)
  175. return error;
  176. return cp_compat_stat(&stat, statbuf);
  177. }
  178. #endif
  179. COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
  180. struct compat_stat __user *, statbuf)
  181. {
  182. struct kstat stat;
  183. int error = vfs_fstat(fd, &stat);
  184. if (!error)
  185. error = cp_compat_stat(&stat, statbuf);
  186. return error;
  187. }
  188. static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
  189. {
  190. if (sizeof ubuf->f_blocks == 4) {
  191. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  192. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  193. return -EOVERFLOW;
  194. /* f_files and f_ffree may be -1; it's okay
  195. * to stuff that into 32 bits */
  196. if (kbuf->f_files != 0xffffffffffffffffULL
  197. && (kbuf->f_files & 0xffffffff00000000ULL))
  198. return -EOVERFLOW;
  199. if (kbuf->f_ffree != 0xffffffffffffffffULL
  200. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  201. return -EOVERFLOW;
  202. }
  203. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  204. __put_user(kbuf->f_type, &ubuf->f_type) ||
  205. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  206. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  207. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  208. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  209. __put_user(kbuf->f_files, &ubuf->f_files) ||
  210. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  211. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  212. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  213. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  214. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  215. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  216. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  217. return -EFAULT;
  218. return 0;
  219. }
  220. /*
  221. * The following statfs calls are copies of code from fs/statfs.c and
  222. * should be checked against those from time to time
  223. */
  224. COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
  225. {
  226. struct kstatfs tmp;
  227. int error = user_statfs(pathname, &tmp);
  228. if (!error)
  229. error = put_compat_statfs(buf, &tmp);
  230. return error;
  231. }
  232. COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
  233. {
  234. struct kstatfs tmp;
  235. int error = fd_statfs(fd, &tmp);
  236. if (!error)
  237. error = put_compat_statfs(buf, &tmp);
  238. return error;
  239. }
  240. static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
  241. {
  242. if (sizeof ubuf->f_blocks == 4) {
  243. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  244. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  245. return -EOVERFLOW;
  246. /* f_files and f_ffree may be -1; it's okay
  247. * to stuff that into 32 bits */
  248. if (kbuf->f_files != 0xffffffffffffffffULL
  249. && (kbuf->f_files & 0xffffffff00000000ULL))
  250. return -EOVERFLOW;
  251. if (kbuf->f_ffree != 0xffffffffffffffffULL
  252. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  253. return -EOVERFLOW;
  254. }
  255. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  256. __put_user(kbuf->f_type, &ubuf->f_type) ||
  257. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  258. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  259. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  260. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  261. __put_user(kbuf->f_files, &ubuf->f_files) ||
  262. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  263. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  264. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  265. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  266. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  267. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  268. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  269. return -EFAULT;
  270. return 0;
  271. }
  272. COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  273. {
  274. struct kstatfs tmp;
  275. int error;
  276. if (sz != sizeof(*buf))
  277. return -EINVAL;
  278. error = user_statfs(pathname, &tmp);
  279. if (!error)
  280. error = put_compat_statfs64(buf, &tmp);
  281. return error;
  282. }
  283. COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
  284. {
  285. struct kstatfs tmp;
  286. int error;
  287. if (sz != sizeof(*buf))
  288. return -EINVAL;
  289. error = fd_statfs(fd, &tmp);
  290. if (!error)
  291. error = put_compat_statfs64(buf, &tmp);
  292. return error;
  293. }
  294. /*
  295. * This is a copy of sys_ustat, just dealing with a structure layout.
  296. * Given how simple this syscall is that apporach is more maintainable
  297. * than the various conversion hacks.
  298. */
  299. COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
  300. {
  301. struct compat_ustat tmp;
  302. struct kstatfs sbuf;
  303. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  304. if (err)
  305. return err;
  306. memset(&tmp, 0, sizeof(struct compat_ustat));
  307. tmp.f_tfree = sbuf.f_bfree;
  308. tmp.f_tinode = sbuf.f_ffree;
  309. if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
  310. return -EFAULT;
  311. return 0;
  312. }
  313. static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  314. {
  315. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  316. __get_user(kfl->l_type, &ufl->l_type) ||
  317. __get_user(kfl->l_whence, &ufl->l_whence) ||
  318. __get_user(kfl->l_start, &ufl->l_start) ||
  319. __get_user(kfl->l_len, &ufl->l_len) ||
  320. __get_user(kfl->l_pid, &ufl->l_pid))
  321. return -EFAULT;
  322. return 0;
  323. }
  324. static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  325. {
  326. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  327. __put_user(kfl->l_type, &ufl->l_type) ||
  328. __put_user(kfl->l_whence, &ufl->l_whence) ||
  329. __put_user(kfl->l_start, &ufl->l_start) ||
  330. __put_user(kfl->l_len, &ufl->l_len) ||
  331. __put_user(kfl->l_pid, &ufl->l_pid))
  332. return -EFAULT;
  333. return 0;
  334. }
  335. #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
  336. static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  337. {
  338. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  339. __get_user(kfl->l_type, &ufl->l_type) ||
  340. __get_user(kfl->l_whence, &ufl->l_whence) ||
  341. __get_user(kfl->l_start, &ufl->l_start) ||
  342. __get_user(kfl->l_len, &ufl->l_len) ||
  343. __get_user(kfl->l_pid, &ufl->l_pid))
  344. return -EFAULT;
  345. return 0;
  346. }
  347. #endif
  348. #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
  349. static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  350. {
  351. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  352. __put_user(kfl->l_type, &ufl->l_type) ||
  353. __put_user(kfl->l_whence, &ufl->l_whence) ||
  354. __put_user(kfl->l_start, &ufl->l_start) ||
  355. __put_user(kfl->l_len, &ufl->l_len) ||
  356. __put_user(kfl->l_pid, &ufl->l_pid))
  357. return -EFAULT;
  358. return 0;
  359. }
  360. #endif
  361. static unsigned int
  362. convert_fcntl_cmd(unsigned int cmd)
  363. {
  364. switch (cmd) {
  365. case F_GETLK64:
  366. return F_GETLK;
  367. case F_SETLK64:
  368. return F_SETLK;
  369. case F_SETLKW64:
  370. return F_SETLKW;
  371. }
  372. return cmd;
  373. }
  374. COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
  375. compat_ulong_t, arg)
  376. {
  377. mm_segment_t old_fs;
  378. struct flock f;
  379. long ret;
  380. unsigned int conv_cmd;
  381. switch (cmd) {
  382. case F_GETLK:
  383. case F_SETLK:
  384. case F_SETLKW:
  385. ret = get_compat_flock(&f, compat_ptr(arg));
  386. if (ret != 0)
  387. break;
  388. old_fs = get_fs();
  389. set_fs(KERNEL_DS);
  390. ret = sys_fcntl(fd, cmd, (unsigned long)&f);
  391. set_fs(old_fs);
  392. if (cmd == F_GETLK && ret == 0) {
  393. /* GETLK was successful and we need to return the data...
  394. * but it needs to fit in the compat structure.
  395. * l_start shouldn't be too big, unless the original
  396. * start + end is greater than COMPAT_OFF_T_MAX, in which
  397. * case the app was asking for trouble, so we return
  398. * -EOVERFLOW in that case.
  399. * l_len could be too big, in which case we just truncate it,
  400. * and only allow the app to see that part of the conflicting
  401. * lock that might make sense to it anyway
  402. */
  403. if (f.l_start > COMPAT_OFF_T_MAX)
  404. ret = -EOVERFLOW;
  405. if (f.l_len > COMPAT_OFF_T_MAX)
  406. f.l_len = COMPAT_OFF_T_MAX;
  407. if (ret == 0)
  408. ret = put_compat_flock(&f, compat_ptr(arg));
  409. }
  410. break;
  411. case F_GETLK64:
  412. case F_SETLK64:
  413. case F_SETLKW64:
  414. case F_OFD_GETLK:
  415. case F_OFD_SETLK:
  416. case F_OFD_SETLKW:
  417. ret = get_compat_flock64(&f, compat_ptr(arg));
  418. if (ret != 0)
  419. break;
  420. old_fs = get_fs();
  421. set_fs(KERNEL_DS);
  422. conv_cmd = convert_fcntl_cmd(cmd);
  423. ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
  424. set_fs(old_fs);
  425. if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
  426. /* need to return lock information - see above for commentary */
  427. if (f.l_start > COMPAT_LOFF_T_MAX)
  428. ret = -EOVERFLOW;
  429. if (f.l_len > COMPAT_LOFF_T_MAX)
  430. f.l_len = COMPAT_LOFF_T_MAX;
  431. if (ret == 0)
  432. ret = put_compat_flock64(&f, compat_ptr(arg));
  433. }
  434. break;
  435. default:
  436. ret = sys_fcntl(fd, cmd, arg);
  437. break;
  438. }
  439. return ret;
  440. }
  441. COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
  442. compat_ulong_t, arg)
  443. {
  444. switch (cmd) {
  445. case F_GETLK64:
  446. case F_SETLK64:
  447. case F_SETLKW64:
  448. case F_OFD_GETLK:
  449. case F_OFD_SETLK:
  450. case F_OFD_SETLKW:
  451. return -EINVAL;
  452. }
  453. return compat_sys_fcntl64(fd, cmd, arg);
  454. }
  455. COMPAT_SYSCALL_DEFINE2(io_setup, unsigned, nr_reqs, u32 __user *, ctx32p)
  456. {
  457. long ret;
  458. aio_context_t ctx64;
  459. mm_segment_t oldfs = get_fs();
  460. if (unlikely(get_user(ctx64, ctx32p)))
  461. return -EFAULT;
  462. set_fs(KERNEL_DS);
  463. /* The __user pointer cast is valid because of the set_fs() */
  464. ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
  465. set_fs(oldfs);
  466. /* truncating is ok because it's a user address */
  467. if (!ret)
  468. ret = put_user((u32) ctx64, ctx32p);
  469. return ret;
  470. }
  471. COMPAT_SYSCALL_DEFINE5(io_getevents, compat_aio_context_t, ctx_id,
  472. compat_long_t, min_nr,
  473. compat_long_t, nr,
  474. struct io_event __user *, events,
  475. struct compat_timespec __user *, timeout)
  476. {
  477. struct timespec t;
  478. struct timespec __user *ut = NULL;
  479. if (timeout) {
  480. if (compat_get_timespec(&t, timeout))
  481. return -EFAULT;
  482. ut = compat_alloc_user_space(sizeof(*ut));
  483. if (copy_to_user(ut, &t, sizeof(t)) )
  484. return -EFAULT;
  485. }
  486. return sys_io_getevents(ctx_id, min_nr, nr, events, ut);
  487. }
  488. /* A write operation does a read from user space and vice versa */
  489. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  490. ssize_t compat_rw_copy_check_uvector(int type,
  491. const struct compat_iovec __user *uvector, unsigned long nr_segs,
  492. unsigned long fast_segs, struct iovec *fast_pointer,
  493. struct iovec **ret_pointer)
  494. {
  495. compat_ssize_t tot_len;
  496. struct iovec *iov = *ret_pointer = fast_pointer;
  497. ssize_t ret = 0;
  498. int seg;
  499. /*
  500. * SuS says "The readv() function *may* fail if the iovcnt argument
  501. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  502. * traditionally returned zero for zero segments, so...
  503. */
  504. if (nr_segs == 0)
  505. goto out;
  506. ret = -EINVAL;
  507. if (nr_segs > UIO_MAXIOV || nr_segs < 0)
  508. goto out;
  509. if (nr_segs > fast_segs) {
  510. ret = -ENOMEM;
  511. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  512. if (iov == NULL)
  513. goto out;
  514. }
  515. *ret_pointer = iov;
  516. ret = -EFAULT;
  517. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  518. goto out;
  519. /*
  520. * Single unix specification:
  521. * We should -EINVAL if an element length is not >= 0 and fitting an
  522. * ssize_t.
  523. *
  524. * In Linux, the total length is limited to MAX_RW_COUNT, there is
  525. * no overflow possibility.
  526. */
  527. tot_len = 0;
  528. ret = -EINVAL;
  529. for (seg = 0; seg < nr_segs; seg++) {
  530. compat_uptr_t buf;
  531. compat_ssize_t len;
  532. if (__get_user(len, &uvector->iov_len) ||
  533. __get_user(buf, &uvector->iov_base)) {
  534. ret = -EFAULT;
  535. goto out;
  536. }
  537. if (len < 0) /* size_t not fitting in compat_ssize_t .. */
  538. goto out;
  539. if (type >= 0 &&
  540. !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
  541. ret = -EFAULT;
  542. goto out;
  543. }
  544. if (len > MAX_RW_COUNT - tot_len)
  545. len = MAX_RW_COUNT - tot_len;
  546. tot_len += len;
  547. iov->iov_base = compat_ptr(buf);
  548. iov->iov_len = (compat_size_t) len;
  549. uvector++;
  550. iov++;
  551. }
  552. ret = tot_len;
  553. out:
  554. return ret;
  555. }
  556. static inline long
  557. copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
  558. {
  559. compat_uptr_t uptr;
  560. int i;
  561. for (i = 0; i < nr; ++i) {
  562. if (get_user(uptr, ptr32 + i))
  563. return -EFAULT;
  564. if (put_user(compat_ptr(uptr), ptr64 + i))
  565. return -EFAULT;
  566. }
  567. return 0;
  568. }
  569. #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
  570. COMPAT_SYSCALL_DEFINE3(io_submit, compat_aio_context_t, ctx_id,
  571. int, nr, u32 __user *, iocb)
  572. {
  573. struct iocb __user * __user *iocb64;
  574. long ret;
  575. if (unlikely(nr < 0))
  576. return -EINVAL;
  577. if (nr > MAX_AIO_SUBMITS)
  578. nr = MAX_AIO_SUBMITS;
  579. iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
  580. ret = copy_iocb(nr, iocb, iocb64);
  581. if (!ret)
  582. ret = do_io_submit(ctx_id, nr, iocb64, 1);
  583. return ret;
  584. }
  585. struct compat_ncp_mount_data {
  586. compat_int_t version;
  587. compat_uint_t ncp_fd;
  588. __compat_uid_t mounted_uid;
  589. compat_pid_t wdog_pid;
  590. unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
  591. compat_uint_t time_out;
  592. compat_uint_t retry_count;
  593. compat_uint_t flags;
  594. __compat_uid_t uid;
  595. __compat_gid_t gid;
  596. compat_mode_t file_mode;
  597. compat_mode_t dir_mode;
  598. };
  599. struct compat_ncp_mount_data_v4 {
  600. compat_int_t version;
  601. compat_ulong_t flags;
  602. compat_ulong_t mounted_uid;
  603. compat_long_t wdog_pid;
  604. compat_uint_t ncp_fd;
  605. compat_uint_t time_out;
  606. compat_uint_t retry_count;
  607. compat_ulong_t uid;
  608. compat_ulong_t gid;
  609. compat_ulong_t file_mode;
  610. compat_ulong_t dir_mode;
  611. };
  612. static void *do_ncp_super_data_conv(void *raw_data)
  613. {
  614. int version = *(unsigned int *)raw_data;
  615. if (version == 3) {
  616. struct compat_ncp_mount_data *c_n = raw_data;
  617. struct ncp_mount_data *n = raw_data;
  618. n->dir_mode = c_n->dir_mode;
  619. n->file_mode = c_n->file_mode;
  620. n->gid = c_n->gid;
  621. n->uid = c_n->uid;
  622. memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
  623. n->wdog_pid = c_n->wdog_pid;
  624. n->mounted_uid = c_n->mounted_uid;
  625. } else if (version == 4) {
  626. struct compat_ncp_mount_data_v4 *c_n = raw_data;
  627. struct ncp_mount_data_v4 *n = raw_data;
  628. n->dir_mode = c_n->dir_mode;
  629. n->file_mode = c_n->file_mode;
  630. n->gid = c_n->gid;
  631. n->uid = c_n->uid;
  632. n->retry_count = c_n->retry_count;
  633. n->time_out = c_n->time_out;
  634. n->ncp_fd = c_n->ncp_fd;
  635. n->wdog_pid = c_n->wdog_pid;
  636. n->mounted_uid = c_n->mounted_uid;
  637. n->flags = c_n->flags;
  638. } else if (version != 5) {
  639. return NULL;
  640. }
  641. return raw_data;
  642. }
  643. struct compat_nfs_string {
  644. compat_uint_t len;
  645. compat_uptr_t data;
  646. };
  647. static inline void compat_nfs_string(struct nfs_string *dst,
  648. struct compat_nfs_string *src)
  649. {
  650. dst->data = compat_ptr(src->data);
  651. dst->len = src->len;
  652. }
  653. struct compat_nfs4_mount_data_v1 {
  654. compat_int_t version;
  655. compat_int_t flags;
  656. compat_int_t rsize;
  657. compat_int_t wsize;
  658. compat_int_t timeo;
  659. compat_int_t retrans;
  660. compat_int_t acregmin;
  661. compat_int_t acregmax;
  662. compat_int_t acdirmin;
  663. compat_int_t acdirmax;
  664. struct compat_nfs_string client_addr;
  665. struct compat_nfs_string mnt_path;
  666. struct compat_nfs_string hostname;
  667. compat_uint_t host_addrlen;
  668. compat_uptr_t host_addr;
  669. compat_int_t proto;
  670. compat_int_t auth_flavourlen;
  671. compat_uptr_t auth_flavours;
  672. };
  673. static int do_nfs4_super_data_conv(void *raw_data)
  674. {
  675. int version = *(compat_uint_t *) raw_data;
  676. if (version == 1) {
  677. struct compat_nfs4_mount_data_v1 *raw = raw_data;
  678. struct nfs4_mount_data *real = raw_data;
  679. /* copy the fields backwards */
  680. real->auth_flavours = compat_ptr(raw->auth_flavours);
  681. real->auth_flavourlen = raw->auth_flavourlen;
  682. real->proto = raw->proto;
  683. real->host_addr = compat_ptr(raw->host_addr);
  684. real->host_addrlen = raw->host_addrlen;
  685. compat_nfs_string(&real->hostname, &raw->hostname);
  686. compat_nfs_string(&real->mnt_path, &raw->mnt_path);
  687. compat_nfs_string(&real->client_addr, &raw->client_addr);
  688. real->acdirmax = raw->acdirmax;
  689. real->acdirmin = raw->acdirmin;
  690. real->acregmax = raw->acregmax;
  691. real->acregmin = raw->acregmin;
  692. real->retrans = raw->retrans;
  693. real->timeo = raw->timeo;
  694. real->wsize = raw->wsize;
  695. real->rsize = raw->rsize;
  696. real->flags = raw->flags;
  697. real->version = raw->version;
  698. }
  699. return 0;
  700. }
  701. #define NCPFS_NAME "ncpfs"
  702. #define NFS4_NAME "nfs4"
  703. COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
  704. const char __user *, dir_name,
  705. const char __user *, type, compat_ulong_t, flags,
  706. const void __user *, data)
  707. {
  708. char *kernel_type;
  709. void *options;
  710. char *kernel_dev;
  711. int retval;
  712. kernel_type = copy_mount_string(type);
  713. retval = PTR_ERR(kernel_type);
  714. if (IS_ERR(kernel_type))
  715. goto out;
  716. kernel_dev = copy_mount_string(dev_name);
  717. retval = PTR_ERR(kernel_dev);
  718. if (IS_ERR(kernel_dev))
  719. goto out1;
  720. options = copy_mount_options(data);
  721. retval = PTR_ERR(options);
  722. if (IS_ERR(options))
  723. goto out2;
  724. if (kernel_type && options) {
  725. if (!strcmp(kernel_type, NCPFS_NAME)) {
  726. do_ncp_super_data_conv(options);
  727. } else if (!strcmp(kernel_type, NFS4_NAME)) {
  728. retval = -EINVAL;
  729. if (do_nfs4_super_data_conv(options))
  730. goto out3;
  731. }
  732. }
  733. retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  734. out3:
  735. kfree(options);
  736. out2:
  737. kfree(kernel_dev);
  738. out1:
  739. kfree(kernel_type);
  740. out:
  741. return retval;
  742. }
  743. struct compat_old_linux_dirent {
  744. compat_ulong_t d_ino;
  745. compat_ulong_t d_offset;
  746. unsigned short d_namlen;
  747. char d_name[1];
  748. };
  749. struct compat_readdir_callback {
  750. struct dir_context ctx;
  751. struct compat_old_linux_dirent __user *dirent;
  752. int result;
  753. };
  754. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  755. int namlen, loff_t offset, u64 ino,
  756. unsigned int d_type)
  757. {
  758. struct compat_readdir_callback *buf =
  759. container_of(ctx, struct compat_readdir_callback, ctx);
  760. struct compat_old_linux_dirent __user *dirent;
  761. compat_ulong_t d_ino;
  762. if (buf->result)
  763. return -EINVAL;
  764. d_ino = ino;
  765. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  766. buf->result = -EOVERFLOW;
  767. return -EOVERFLOW;
  768. }
  769. buf->result++;
  770. dirent = buf->dirent;
  771. if (!access_ok(VERIFY_WRITE, dirent,
  772. (unsigned long)(dirent->d_name + namlen + 1) -
  773. (unsigned long)dirent))
  774. goto efault;
  775. if ( __put_user(d_ino, &dirent->d_ino) ||
  776. __put_user(offset, &dirent->d_offset) ||
  777. __put_user(namlen, &dirent->d_namlen) ||
  778. __copy_to_user(dirent->d_name, name, namlen) ||
  779. __put_user(0, dirent->d_name + namlen))
  780. goto efault;
  781. return 0;
  782. efault:
  783. buf->result = -EFAULT;
  784. return -EFAULT;
  785. }
  786. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  787. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  788. {
  789. int error;
  790. struct fd f = fdget_pos(fd);
  791. struct compat_readdir_callback buf = {
  792. .ctx.actor = compat_fillonedir,
  793. .dirent = dirent
  794. };
  795. if (!f.file)
  796. return -EBADF;
  797. error = iterate_dir(f.file, &buf.ctx);
  798. if (buf.result)
  799. error = buf.result;
  800. fdput_pos(f);
  801. return error;
  802. }
  803. struct compat_linux_dirent {
  804. compat_ulong_t d_ino;
  805. compat_ulong_t d_off;
  806. unsigned short d_reclen;
  807. char d_name[1];
  808. };
  809. struct compat_getdents_callback {
  810. struct dir_context ctx;
  811. struct compat_linux_dirent __user *current_dir;
  812. struct compat_linux_dirent __user *previous;
  813. int count;
  814. int error;
  815. };
  816. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  817. loff_t offset, u64 ino, unsigned int d_type)
  818. {
  819. struct compat_linux_dirent __user * dirent;
  820. struct compat_getdents_callback *buf =
  821. container_of(ctx, struct compat_getdents_callback, ctx);
  822. compat_ulong_t d_ino;
  823. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  824. namlen + 2, sizeof(compat_long_t));
  825. buf->error = -EINVAL; /* only used if we fail.. */
  826. if (reclen > buf->count)
  827. return -EINVAL;
  828. d_ino = ino;
  829. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  830. buf->error = -EOVERFLOW;
  831. return -EOVERFLOW;
  832. }
  833. dirent = buf->previous;
  834. if (dirent) {
  835. if (signal_pending(current))
  836. return -EINTR;
  837. if (__put_user(offset, &dirent->d_off))
  838. goto efault;
  839. }
  840. dirent = buf->current_dir;
  841. if (__put_user(d_ino, &dirent->d_ino))
  842. goto efault;
  843. if (__put_user(reclen, &dirent->d_reclen))
  844. goto efault;
  845. if (copy_to_user(dirent->d_name, name, namlen))
  846. goto efault;
  847. if (__put_user(0, dirent->d_name + namlen))
  848. goto efault;
  849. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  850. goto efault;
  851. buf->previous = dirent;
  852. dirent = (void __user *)dirent + reclen;
  853. buf->current_dir = dirent;
  854. buf->count -= reclen;
  855. return 0;
  856. efault:
  857. buf->error = -EFAULT;
  858. return -EFAULT;
  859. }
  860. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  861. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  862. {
  863. struct fd f;
  864. struct compat_linux_dirent __user * lastdirent;
  865. struct compat_getdents_callback buf = {
  866. .ctx.actor = compat_filldir,
  867. .current_dir = dirent,
  868. .count = count
  869. };
  870. int error;
  871. if (!access_ok(VERIFY_WRITE, dirent, count))
  872. return -EFAULT;
  873. f = fdget_pos(fd);
  874. if (!f.file)
  875. return -EBADF;
  876. error = iterate_dir(f.file, &buf.ctx);
  877. if (error >= 0)
  878. error = buf.error;
  879. lastdirent = buf.previous;
  880. if (lastdirent) {
  881. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  882. error = -EFAULT;
  883. else
  884. error = count - buf.count;
  885. }
  886. fdput_pos(f);
  887. return error;
  888. }
  889. #ifdef __ARCH_WANT_COMPAT_SYS_GETDENTS64
  890. struct compat_getdents_callback64 {
  891. struct dir_context ctx;
  892. struct linux_dirent64 __user *current_dir;
  893. struct linux_dirent64 __user *previous;
  894. int count;
  895. int error;
  896. };
  897. static int compat_filldir64(struct dir_context *ctx, const char *name,
  898. int namlen, loff_t offset, u64 ino,
  899. unsigned int d_type)
  900. {
  901. struct linux_dirent64 __user *dirent;
  902. struct compat_getdents_callback64 *buf =
  903. container_of(ctx, struct compat_getdents_callback64, ctx);
  904. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  905. sizeof(u64));
  906. u64 off;
  907. buf->error = -EINVAL; /* only used if we fail.. */
  908. if (reclen > buf->count)
  909. return -EINVAL;
  910. dirent = buf->previous;
  911. if (dirent) {
  912. if (signal_pending(current))
  913. return -EINTR;
  914. if (__put_user_unaligned(offset, &dirent->d_off))
  915. goto efault;
  916. }
  917. dirent = buf->current_dir;
  918. if (__put_user_unaligned(ino, &dirent->d_ino))
  919. goto efault;
  920. off = 0;
  921. if (__put_user_unaligned(off, &dirent->d_off))
  922. goto efault;
  923. if (__put_user(reclen, &dirent->d_reclen))
  924. goto efault;
  925. if (__put_user(d_type, &dirent->d_type))
  926. goto efault;
  927. if (copy_to_user(dirent->d_name, name, namlen))
  928. goto efault;
  929. if (__put_user(0, dirent->d_name + namlen))
  930. goto efault;
  931. buf->previous = dirent;
  932. dirent = (void __user *)dirent + reclen;
  933. buf->current_dir = dirent;
  934. buf->count -= reclen;
  935. return 0;
  936. efault:
  937. buf->error = -EFAULT;
  938. return -EFAULT;
  939. }
  940. COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  941. struct linux_dirent64 __user *, dirent, unsigned int, count)
  942. {
  943. struct fd f;
  944. struct linux_dirent64 __user * lastdirent;
  945. struct compat_getdents_callback64 buf = {
  946. .ctx.actor = compat_filldir64,
  947. .current_dir = dirent,
  948. .count = count
  949. };
  950. int error;
  951. if (!access_ok(VERIFY_WRITE, dirent, count))
  952. return -EFAULT;
  953. f = fdget_pos(fd);
  954. if (!f.file)
  955. return -EBADF;
  956. error = iterate_dir(f.file, &buf.ctx);
  957. if (error >= 0)
  958. error = buf.error;
  959. lastdirent = buf.previous;
  960. if (lastdirent) {
  961. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  962. if (__put_user_unaligned(d_off, &lastdirent->d_off))
  963. error = -EFAULT;
  964. else
  965. error = count - buf.count;
  966. }
  967. fdput_pos(f);
  968. return error;
  969. }
  970. #endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */
  971. /*
  972. * Exactly like fs/open.c:sys_open(), except that it doesn't set the
  973. * O_LARGEFILE flag.
  974. */
  975. COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  976. {
  977. return do_sys_open(AT_FDCWD, filename, flags, mode);
  978. }
  979. /*
  980. * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
  981. * O_LARGEFILE flag.
  982. */
  983. COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
  984. {
  985. return do_sys_open(dfd, filename, flags, mode);
  986. }
  987. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  988. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  989. int timeval, int ret)
  990. {
  991. struct timespec ts;
  992. if (!p)
  993. return ret;
  994. if (current->personality & STICKY_TIMEOUTS)
  995. goto sticky;
  996. /* No update for zero timeout */
  997. if (!end_time->tv_sec && !end_time->tv_nsec)
  998. return ret;
  999. ktime_get_ts(&ts);
  1000. ts = timespec_sub(*end_time, ts);
  1001. if (ts.tv_sec < 0)
  1002. ts.tv_sec = ts.tv_nsec = 0;
  1003. if (timeval) {
  1004. struct compat_timeval rtv;
  1005. rtv.tv_sec = ts.tv_sec;
  1006. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  1007. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  1008. return ret;
  1009. } else {
  1010. struct compat_timespec rts;
  1011. rts.tv_sec = ts.tv_sec;
  1012. rts.tv_nsec = ts.tv_nsec;
  1013. if (!copy_to_user(p, &rts, sizeof(rts)))
  1014. return ret;
  1015. }
  1016. /*
  1017. * If an application puts its timeval in read-only memory, we
  1018. * don't want the Linux-specific update to the timeval to
  1019. * cause a fault after the select has completed
  1020. * successfully. However, because we're not updating the
  1021. * timeval, we can't restart the system call.
  1022. */
  1023. sticky:
  1024. if (ret == -ERESTARTNOHAND)
  1025. ret = -EINTR;
  1026. return ret;
  1027. }
  1028. /*
  1029. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  1030. * 64-bit unsigned longs.
  1031. */
  1032. static
  1033. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1034. unsigned long *fdset)
  1035. {
  1036. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1037. if (ufdset) {
  1038. unsigned long odd;
  1039. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  1040. return -EFAULT;
  1041. odd = nr & 1UL;
  1042. nr &= ~1UL;
  1043. while (nr) {
  1044. unsigned long h, l;
  1045. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  1046. return -EFAULT;
  1047. ufdset += 2;
  1048. *fdset++ = h << 32 | l;
  1049. nr -= 2;
  1050. }
  1051. if (odd && __get_user(*fdset, ufdset))
  1052. return -EFAULT;
  1053. } else {
  1054. /* Tricky, must clear full unsigned long in the
  1055. * kernel fdset at the end, this makes sure that
  1056. * actually happens.
  1057. */
  1058. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  1059. }
  1060. return 0;
  1061. }
  1062. static
  1063. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1064. unsigned long *fdset)
  1065. {
  1066. unsigned long odd;
  1067. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1068. if (!ufdset)
  1069. return 0;
  1070. odd = nr & 1UL;
  1071. nr &= ~1UL;
  1072. while (nr) {
  1073. unsigned long h, l;
  1074. l = *fdset++;
  1075. h = l >> 32;
  1076. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1077. return -EFAULT;
  1078. ufdset += 2;
  1079. nr -= 2;
  1080. }
  1081. if (odd && __put_user(*fdset, ufdset))
  1082. return -EFAULT;
  1083. return 0;
  1084. }
  1085. /*
  1086. * This is a virtual copy of sys_select from fs/select.c and probably
  1087. * should be compared to it from time to time
  1088. */
  1089. /*
  1090. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1091. * like to be certain this leads to no problems. So I return
  1092. * EINTR just for safety.
  1093. *
  1094. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1095. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1096. */
  1097. int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1098. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1099. struct timespec *end_time)
  1100. {
  1101. fd_set_bits fds;
  1102. void *bits;
  1103. int size, max_fds, ret = -EINVAL;
  1104. struct fdtable *fdt;
  1105. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1106. if (n < 0)
  1107. goto out_nofds;
  1108. /* max_fds can increase, so grab it once to avoid race */
  1109. rcu_read_lock();
  1110. fdt = files_fdtable(current->files);
  1111. max_fds = fdt->max_fds;
  1112. rcu_read_unlock();
  1113. if (n > max_fds)
  1114. n = max_fds;
  1115. /*
  1116. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1117. * since we used fdset we need to allocate memory in units of
  1118. * long-words.
  1119. */
  1120. size = FDS_BYTES(n);
  1121. bits = stack_fds;
  1122. if (size > sizeof(stack_fds) / 6) {
  1123. bits = kmalloc(6 * size, GFP_KERNEL);
  1124. ret = -ENOMEM;
  1125. if (!bits)
  1126. goto out_nofds;
  1127. }
  1128. fds.in = (unsigned long *) bits;
  1129. fds.out = (unsigned long *) (bits + size);
  1130. fds.ex = (unsigned long *) (bits + 2*size);
  1131. fds.res_in = (unsigned long *) (bits + 3*size);
  1132. fds.res_out = (unsigned long *) (bits + 4*size);
  1133. fds.res_ex = (unsigned long *) (bits + 5*size);
  1134. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1135. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1136. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1137. goto out;
  1138. zero_fd_set(n, fds.res_in);
  1139. zero_fd_set(n, fds.res_out);
  1140. zero_fd_set(n, fds.res_ex);
  1141. ret = do_select(n, &fds, end_time);
  1142. if (ret < 0)
  1143. goto out;
  1144. if (!ret) {
  1145. ret = -ERESTARTNOHAND;
  1146. if (signal_pending(current))
  1147. goto out;
  1148. ret = 0;
  1149. }
  1150. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1151. compat_set_fd_set(n, outp, fds.res_out) ||
  1152. compat_set_fd_set(n, exp, fds.res_ex))
  1153. ret = -EFAULT;
  1154. out:
  1155. if (bits != stack_fds)
  1156. kfree(bits);
  1157. out_nofds:
  1158. return ret;
  1159. }
  1160. COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
  1161. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1162. struct compat_timeval __user *, tvp)
  1163. {
  1164. struct timespec end_time, *to = NULL;
  1165. struct compat_timeval tv;
  1166. int ret;
  1167. if (tvp) {
  1168. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1169. return -EFAULT;
  1170. to = &end_time;
  1171. if (poll_select_set_timeout(to,
  1172. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1173. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1174. return -EINVAL;
  1175. }
  1176. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1177. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1178. return ret;
  1179. }
  1180. struct compat_sel_arg_struct {
  1181. compat_ulong_t n;
  1182. compat_uptr_t inp;
  1183. compat_uptr_t outp;
  1184. compat_uptr_t exp;
  1185. compat_uptr_t tvp;
  1186. };
  1187. COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
  1188. {
  1189. struct compat_sel_arg_struct a;
  1190. if (copy_from_user(&a, arg, sizeof(a)))
  1191. return -EFAULT;
  1192. return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1193. compat_ptr(a.exp), compat_ptr(a.tvp));
  1194. }
  1195. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1196. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1197. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1198. compat_size_t sigsetsize)
  1199. {
  1200. compat_sigset_t ss32;
  1201. sigset_t ksigmask, sigsaved;
  1202. struct compat_timespec ts;
  1203. struct timespec end_time, *to = NULL;
  1204. int ret;
  1205. if (tsp) {
  1206. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1207. return -EFAULT;
  1208. to = &end_time;
  1209. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1210. return -EINVAL;
  1211. }
  1212. if (sigmask) {
  1213. if (sigsetsize != sizeof(compat_sigset_t))
  1214. return -EINVAL;
  1215. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1216. return -EFAULT;
  1217. sigset_from_compat(&ksigmask, &ss32);
  1218. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1219. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1220. }
  1221. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1222. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1223. if (ret == -ERESTARTNOHAND) {
  1224. /*
  1225. * Don't restore the signal mask yet. Let do_signal() deliver
  1226. * the signal on the way back to userspace, before the signal
  1227. * mask is restored.
  1228. */
  1229. if (sigmask) {
  1230. memcpy(&current->saved_sigmask, &sigsaved,
  1231. sizeof(sigsaved));
  1232. set_restore_sigmask();
  1233. }
  1234. } else if (sigmask)
  1235. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1236. return ret;
  1237. }
  1238. COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
  1239. compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
  1240. struct compat_timespec __user *, tsp, void __user *, sig)
  1241. {
  1242. compat_size_t sigsetsize = 0;
  1243. compat_uptr_t up = 0;
  1244. if (sig) {
  1245. if (!access_ok(VERIFY_READ, sig,
  1246. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1247. __get_user(up, (compat_uptr_t __user *)sig) ||
  1248. __get_user(sigsetsize,
  1249. (compat_size_t __user *)(sig+sizeof(up))))
  1250. return -EFAULT;
  1251. }
  1252. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1253. sigsetsize);
  1254. }
  1255. COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
  1256. unsigned int, nfds, struct compat_timespec __user *, tsp,
  1257. const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
  1258. {
  1259. compat_sigset_t ss32;
  1260. sigset_t ksigmask, sigsaved;
  1261. struct compat_timespec ts;
  1262. struct timespec end_time, *to = NULL;
  1263. int ret;
  1264. if (tsp) {
  1265. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1266. return -EFAULT;
  1267. to = &end_time;
  1268. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1269. return -EINVAL;
  1270. }
  1271. if (sigmask) {
  1272. if (sigsetsize != sizeof(compat_sigset_t))
  1273. return -EINVAL;
  1274. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1275. return -EFAULT;
  1276. sigset_from_compat(&ksigmask, &ss32);
  1277. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1278. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1279. }
  1280. ret = do_sys_poll(ufds, nfds, to);
  1281. /* We can restart this syscall, usually */
  1282. if (ret == -EINTR) {
  1283. /*
  1284. * Don't restore the signal mask yet. Let do_signal() deliver
  1285. * the signal on the way back to userspace, before the signal
  1286. * mask is restored.
  1287. */
  1288. if (sigmask) {
  1289. memcpy(&current->saved_sigmask, &sigsaved,
  1290. sizeof(sigsaved));
  1291. set_restore_sigmask();
  1292. }
  1293. ret = -ERESTARTNOHAND;
  1294. } else if (sigmask)
  1295. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1296. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1297. return ret;
  1298. }
  1299. #ifdef CONFIG_FHANDLE
  1300. /*
  1301. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  1302. * doesn't set the O_LARGEFILE flag.
  1303. */
  1304. COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  1305. struct file_handle __user *, handle, int, flags)
  1306. {
  1307. return do_handle_open(mountdirfd, handle, flags);
  1308. }
  1309. #endif