compat.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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/eventpoll.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/slab.h>
  49. #include <linux/pagemap.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. #include "read_write.h"
  67. /*
  68. * Not all architectures have sys_utime, so implement this in terms
  69. * of sys_utimes.
  70. */
  71. asmlinkage long compat_sys_utime(const char __user *filename,
  72. struct compat_utimbuf __user *t)
  73. {
  74. struct timespec tv[2];
  75. if (t) {
  76. if (get_user(tv[0].tv_sec, &t->actime) ||
  77. get_user(tv[1].tv_sec, &t->modtime))
  78. return -EFAULT;
  79. tv[0].tv_nsec = 0;
  80. tv[1].tv_nsec = 0;
  81. }
  82. return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  83. }
  84. asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags)
  85. {
  86. struct timespec tv[2];
  87. if (t) {
  88. if (get_compat_timespec(&tv[0], &t[0]) ||
  89. get_compat_timespec(&tv[1], &t[1]))
  90. return -EFAULT;
  91. if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
  92. return 0;
  93. }
  94. return do_utimes(dfd, filename, t ? tv : NULL, flags);
  95. }
  96. asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t)
  97. {
  98. struct timespec tv[2];
  99. if (t) {
  100. if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
  101. get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
  102. get_user(tv[1].tv_sec, &t[1].tv_sec) ||
  103. get_user(tv[1].tv_nsec, &t[1].tv_usec))
  104. return -EFAULT;
  105. if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
  106. tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
  107. return -EINVAL;
  108. tv[0].tv_nsec *= 1000;
  109. tv[1].tv_nsec *= 1000;
  110. }
  111. return do_utimes(dfd, filename, t ? tv : NULL, 0);
  112. }
  113. asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t)
  114. {
  115. return compat_sys_futimesat(AT_FDCWD, filename, t);
  116. }
  117. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  118. {
  119. struct compat_stat tmp;
  120. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  121. return -EOVERFLOW;
  122. memset(&tmp, 0, sizeof(tmp));
  123. tmp.st_dev = old_encode_dev(stat->dev);
  124. tmp.st_ino = stat->ino;
  125. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  126. return -EOVERFLOW;
  127. tmp.st_mode = stat->mode;
  128. tmp.st_nlink = stat->nlink;
  129. if (tmp.st_nlink != stat->nlink)
  130. return -EOVERFLOW;
  131. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  132. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  133. tmp.st_rdev = old_encode_dev(stat->rdev);
  134. if ((u64) stat->size > MAX_NON_LFS)
  135. return -EOVERFLOW;
  136. tmp.st_size = stat->size;
  137. tmp.st_atime = stat->atime.tv_sec;
  138. tmp.st_atime_nsec = stat->atime.tv_nsec;
  139. tmp.st_mtime = stat->mtime.tv_sec;
  140. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  141. tmp.st_ctime = stat->ctime.tv_sec;
  142. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  143. tmp.st_blocks = stat->blocks;
  144. tmp.st_blksize = stat->blksize;
  145. return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  146. }
  147. asmlinkage long compat_sys_newstat(const char __user * filename,
  148. struct compat_stat __user *statbuf)
  149. {
  150. struct kstat stat;
  151. int error;
  152. error = vfs_stat(filename, &stat);
  153. if (error)
  154. return error;
  155. return cp_compat_stat(&stat, statbuf);
  156. }
  157. asmlinkage long compat_sys_newlstat(const char __user * filename,
  158. struct compat_stat __user *statbuf)
  159. {
  160. struct kstat stat;
  161. int error;
  162. error = vfs_lstat(filename, &stat);
  163. if (error)
  164. return error;
  165. return cp_compat_stat(&stat, statbuf);
  166. }
  167. #ifndef __ARCH_WANT_STAT64
  168. asmlinkage long compat_sys_newfstatat(unsigned int dfd,
  169. const char __user *filename,
  170. struct compat_stat __user *statbuf, int flag)
  171. {
  172. struct kstat stat;
  173. int error;
  174. error = vfs_fstatat(dfd, filename, &stat, flag);
  175. if (error)
  176. return error;
  177. return cp_compat_stat(&stat, statbuf);
  178. }
  179. #endif
  180. asmlinkage long compat_sys_newfstat(unsigned int fd,
  181. struct compat_stat __user * statbuf)
  182. {
  183. struct kstat stat;
  184. int error = vfs_fstat(fd, &stat);
  185. if (!error)
  186. error = cp_compat_stat(&stat, statbuf);
  187. return error;
  188. }
  189. static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
  190. {
  191. if (sizeof ubuf->f_blocks == 4) {
  192. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  193. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  194. return -EOVERFLOW;
  195. /* f_files and f_ffree may be -1; it's okay
  196. * to stuff that into 32 bits */
  197. if (kbuf->f_files != 0xffffffffffffffffULL
  198. && (kbuf->f_files & 0xffffffff00000000ULL))
  199. return -EOVERFLOW;
  200. if (kbuf->f_ffree != 0xffffffffffffffffULL
  201. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  202. return -EOVERFLOW;
  203. }
  204. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  205. __put_user(kbuf->f_type, &ubuf->f_type) ||
  206. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  207. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  208. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  209. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  210. __put_user(kbuf->f_files, &ubuf->f_files) ||
  211. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  212. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  213. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  214. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  215. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  216. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  217. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  218. return -EFAULT;
  219. return 0;
  220. }
  221. /*
  222. * The following statfs calls are copies of code from fs/statfs.c and
  223. * should be checked against those from time to time
  224. */
  225. asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
  226. {
  227. struct kstatfs tmp;
  228. int error = user_statfs(pathname, &tmp);
  229. if (!error)
  230. error = put_compat_statfs(buf, &tmp);
  231. return error;
  232. }
  233. asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
  234. {
  235. struct kstatfs tmp;
  236. int error = fd_statfs(fd, &tmp);
  237. if (!error)
  238. error = put_compat_statfs(buf, &tmp);
  239. return error;
  240. }
  241. static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
  242. {
  243. if (sizeof ubuf->f_blocks == 4) {
  244. if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
  245. kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
  246. return -EOVERFLOW;
  247. /* f_files and f_ffree may be -1; it's okay
  248. * to stuff that into 32 bits */
  249. if (kbuf->f_files != 0xffffffffffffffffULL
  250. && (kbuf->f_files & 0xffffffff00000000ULL))
  251. return -EOVERFLOW;
  252. if (kbuf->f_ffree != 0xffffffffffffffffULL
  253. && (kbuf->f_ffree & 0xffffffff00000000ULL))
  254. return -EOVERFLOW;
  255. }
  256. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
  257. __put_user(kbuf->f_type, &ubuf->f_type) ||
  258. __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
  259. __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
  260. __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
  261. __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
  262. __put_user(kbuf->f_files, &ubuf->f_files) ||
  263. __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
  264. __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
  265. __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
  266. __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
  267. __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
  268. __put_user(kbuf->f_flags, &ubuf->f_flags) ||
  269. __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
  270. return -EFAULT;
  271. return 0;
  272. }
  273. asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
  274. {
  275. struct kstatfs tmp;
  276. int error;
  277. if (sz != sizeof(*buf))
  278. return -EINVAL;
  279. error = user_statfs(pathname, &tmp);
  280. if (!error)
  281. error = put_compat_statfs64(buf, &tmp);
  282. return error;
  283. }
  284. asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
  285. {
  286. struct kstatfs tmp;
  287. int error;
  288. if (sz != sizeof(*buf))
  289. return -EINVAL;
  290. error = fd_statfs(fd, &tmp);
  291. if (!error)
  292. error = put_compat_statfs64(buf, &tmp);
  293. return error;
  294. }
  295. /*
  296. * This is a copy of sys_ustat, just dealing with a structure layout.
  297. * Given how simple this syscall is that apporach is more maintainable
  298. * than the various conversion hacks.
  299. */
  300. asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u)
  301. {
  302. struct compat_ustat tmp;
  303. struct kstatfs sbuf;
  304. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  305. if (err)
  306. return err;
  307. memset(&tmp, 0, sizeof(struct compat_ustat));
  308. tmp.f_tfree = sbuf.f_bfree;
  309. tmp.f_tinode = sbuf.f_ffree;
  310. if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
  311. return -EFAULT;
  312. return 0;
  313. }
  314. static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  315. {
  316. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  317. __get_user(kfl->l_type, &ufl->l_type) ||
  318. __get_user(kfl->l_whence, &ufl->l_whence) ||
  319. __get_user(kfl->l_start, &ufl->l_start) ||
  320. __get_user(kfl->l_len, &ufl->l_len) ||
  321. __get_user(kfl->l_pid, &ufl->l_pid))
  322. return -EFAULT;
  323. return 0;
  324. }
  325. static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
  326. {
  327. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  328. __put_user(kfl->l_type, &ufl->l_type) ||
  329. __put_user(kfl->l_whence, &ufl->l_whence) ||
  330. __put_user(kfl->l_start, &ufl->l_start) ||
  331. __put_user(kfl->l_len, &ufl->l_len) ||
  332. __put_user(kfl->l_pid, &ufl->l_pid))
  333. return -EFAULT;
  334. return 0;
  335. }
  336. #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
  337. static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  338. {
  339. if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
  340. __get_user(kfl->l_type, &ufl->l_type) ||
  341. __get_user(kfl->l_whence, &ufl->l_whence) ||
  342. __get_user(kfl->l_start, &ufl->l_start) ||
  343. __get_user(kfl->l_len, &ufl->l_len) ||
  344. __get_user(kfl->l_pid, &ufl->l_pid))
  345. return -EFAULT;
  346. return 0;
  347. }
  348. #endif
  349. #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
  350. static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
  351. {
  352. if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
  353. __put_user(kfl->l_type, &ufl->l_type) ||
  354. __put_user(kfl->l_whence, &ufl->l_whence) ||
  355. __put_user(kfl->l_start, &ufl->l_start) ||
  356. __put_user(kfl->l_len, &ufl->l_len) ||
  357. __put_user(kfl->l_pid, &ufl->l_pid))
  358. return -EFAULT;
  359. return 0;
  360. }
  361. #endif
  362. asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
  363. unsigned long arg)
  364. {
  365. mm_segment_t old_fs;
  366. struct flock f;
  367. long ret;
  368. switch (cmd) {
  369. case F_GETLK:
  370. case F_SETLK:
  371. case F_SETLKW:
  372. ret = get_compat_flock(&f, compat_ptr(arg));
  373. if (ret != 0)
  374. break;
  375. old_fs = get_fs();
  376. set_fs(KERNEL_DS);
  377. ret = sys_fcntl(fd, cmd, (unsigned long)&f);
  378. set_fs(old_fs);
  379. if (cmd == F_GETLK && ret == 0) {
  380. /* GETLK was successful and we need to return the data...
  381. * but it needs to fit in the compat structure.
  382. * l_start shouldn't be too big, unless the original
  383. * start + end is greater than COMPAT_OFF_T_MAX, in which
  384. * case the app was asking for trouble, so we return
  385. * -EOVERFLOW in that case.
  386. * l_len could be too big, in which case we just truncate it,
  387. * and only allow the app to see that part of the conflicting
  388. * lock that might make sense to it anyway
  389. */
  390. if (f.l_start > COMPAT_OFF_T_MAX)
  391. ret = -EOVERFLOW;
  392. if (f.l_len > COMPAT_OFF_T_MAX)
  393. f.l_len = COMPAT_OFF_T_MAX;
  394. if (ret == 0)
  395. ret = put_compat_flock(&f, compat_ptr(arg));
  396. }
  397. break;
  398. case F_GETLK64:
  399. case F_SETLK64:
  400. case F_SETLKW64:
  401. ret = get_compat_flock64(&f, compat_ptr(arg));
  402. if (ret != 0)
  403. break;
  404. old_fs = get_fs();
  405. set_fs(KERNEL_DS);
  406. ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
  407. ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
  408. (unsigned long)&f);
  409. set_fs(old_fs);
  410. if (cmd == F_GETLK64 && ret == 0) {
  411. /* need to return lock information - see above for commentary */
  412. if (f.l_start > COMPAT_LOFF_T_MAX)
  413. ret = -EOVERFLOW;
  414. if (f.l_len > COMPAT_LOFF_T_MAX)
  415. f.l_len = COMPAT_LOFF_T_MAX;
  416. if (ret == 0)
  417. ret = put_compat_flock64(&f, compat_ptr(arg));
  418. }
  419. break;
  420. default:
  421. ret = sys_fcntl(fd, cmd, arg);
  422. break;
  423. }
  424. return ret;
  425. }
  426. asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
  427. unsigned long arg)
  428. {
  429. if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
  430. return -EINVAL;
  431. return compat_sys_fcntl64(fd, cmd, arg);
  432. }
  433. asmlinkage long
  434. compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
  435. {
  436. long ret;
  437. aio_context_t ctx64;
  438. mm_segment_t oldfs = get_fs();
  439. if (unlikely(get_user(ctx64, ctx32p)))
  440. return -EFAULT;
  441. set_fs(KERNEL_DS);
  442. /* The __user pointer cast is valid because of the set_fs() */
  443. ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
  444. set_fs(oldfs);
  445. /* truncating is ok because it's a user address */
  446. if (!ret)
  447. ret = put_user((u32) ctx64, ctx32p);
  448. return ret;
  449. }
  450. asmlinkage long
  451. compat_sys_io_getevents(aio_context_t ctx_id,
  452. unsigned long min_nr,
  453. unsigned long nr,
  454. struct io_event __user *events,
  455. struct compat_timespec __user *timeout)
  456. {
  457. long ret;
  458. struct timespec t;
  459. struct timespec __user *ut = NULL;
  460. ret = -EFAULT;
  461. if (unlikely(!access_ok(VERIFY_WRITE, events,
  462. nr * sizeof(struct io_event))))
  463. goto out;
  464. if (timeout) {
  465. if (get_compat_timespec(&t, timeout))
  466. goto out;
  467. ut = compat_alloc_user_space(sizeof(*ut));
  468. if (copy_to_user(ut, &t, sizeof(t)) )
  469. goto out;
  470. }
  471. ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
  472. out:
  473. return ret;
  474. }
  475. /* A write operation does a read from user space and vice versa */
  476. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  477. ssize_t compat_rw_copy_check_uvector(int type,
  478. const struct compat_iovec __user *uvector, unsigned long nr_segs,
  479. unsigned long fast_segs, struct iovec *fast_pointer,
  480. struct iovec **ret_pointer, int check_access)
  481. {
  482. compat_ssize_t tot_len;
  483. struct iovec *iov = *ret_pointer = fast_pointer;
  484. ssize_t ret = 0;
  485. int seg;
  486. /*
  487. * SuS says "The readv() function *may* fail if the iovcnt argument
  488. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  489. * traditionally returned zero for zero segments, so...
  490. */
  491. if (nr_segs == 0)
  492. goto out;
  493. ret = -EINVAL;
  494. if (nr_segs > UIO_MAXIOV || nr_segs < 0)
  495. goto out;
  496. if (nr_segs > fast_segs) {
  497. ret = -ENOMEM;
  498. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  499. if (iov == NULL)
  500. goto out;
  501. }
  502. *ret_pointer = iov;
  503. /*
  504. * Single unix specification:
  505. * We should -EINVAL if an element length is not >= 0 and fitting an
  506. * ssize_t.
  507. *
  508. * In Linux, the total length is limited to MAX_RW_COUNT, there is
  509. * no overflow possibility.
  510. */
  511. tot_len = 0;
  512. ret = -EINVAL;
  513. for (seg = 0; seg < nr_segs; seg++) {
  514. compat_uptr_t buf;
  515. compat_ssize_t len;
  516. if (__get_user(len, &uvector->iov_len) ||
  517. __get_user(buf, &uvector->iov_base)) {
  518. ret = -EFAULT;
  519. goto out;
  520. }
  521. if (len < 0) /* size_t not fitting in compat_ssize_t .. */
  522. goto out;
  523. if (check_access &&
  524. !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
  525. ret = -EFAULT;
  526. goto out;
  527. }
  528. if (len > MAX_RW_COUNT - tot_len)
  529. len = MAX_RW_COUNT - tot_len;
  530. tot_len += len;
  531. iov->iov_base = compat_ptr(buf);
  532. iov->iov_len = (compat_size_t) len;
  533. uvector++;
  534. iov++;
  535. }
  536. ret = tot_len;
  537. out:
  538. return ret;
  539. }
  540. static inline long
  541. copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
  542. {
  543. compat_uptr_t uptr;
  544. int i;
  545. for (i = 0; i < nr; ++i) {
  546. if (get_user(uptr, ptr32 + i))
  547. return -EFAULT;
  548. if (put_user(compat_ptr(uptr), ptr64 + i))
  549. return -EFAULT;
  550. }
  551. return 0;
  552. }
  553. #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
  554. asmlinkage long
  555. compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
  556. {
  557. struct iocb __user * __user *iocb64;
  558. long ret;
  559. if (unlikely(nr < 0))
  560. return -EINVAL;
  561. if (nr > MAX_AIO_SUBMITS)
  562. nr = MAX_AIO_SUBMITS;
  563. iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
  564. ret = copy_iocb(nr, iocb, iocb64);
  565. if (!ret)
  566. ret = do_io_submit(ctx_id, nr, iocb64, 1);
  567. return ret;
  568. }
  569. struct compat_ncp_mount_data {
  570. compat_int_t version;
  571. compat_uint_t ncp_fd;
  572. __compat_uid_t mounted_uid;
  573. compat_pid_t wdog_pid;
  574. unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
  575. compat_uint_t time_out;
  576. compat_uint_t retry_count;
  577. compat_uint_t flags;
  578. __compat_uid_t uid;
  579. __compat_gid_t gid;
  580. compat_mode_t file_mode;
  581. compat_mode_t dir_mode;
  582. };
  583. struct compat_ncp_mount_data_v4 {
  584. compat_int_t version;
  585. compat_ulong_t flags;
  586. compat_ulong_t mounted_uid;
  587. compat_long_t wdog_pid;
  588. compat_uint_t ncp_fd;
  589. compat_uint_t time_out;
  590. compat_uint_t retry_count;
  591. compat_ulong_t uid;
  592. compat_ulong_t gid;
  593. compat_ulong_t file_mode;
  594. compat_ulong_t dir_mode;
  595. };
  596. static void *do_ncp_super_data_conv(void *raw_data)
  597. {
  598. int version = *(unsigned int *)raw_data;
  599. if (version == 3) {
  600. struct compat_ncp_mount_data *c_n = raw_data;
  601. struct ncp_mount_data *n = raw_data;
  602. n->dir_mode = c_n->dir_mode;
  603. n->file_mode = c_n->file_mode;
  604. n->gid = c_n->gid;
  605. n->uid = c_n->uid;
  606. memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
  607. n->wdog_pid = c_n->wdog_pid;
  608. n->mounted_uid = c_n->mounted_uid;
  609. } else if (version == 4) {
  610. struct compat_ncp_mount_data_v4 *c_n = raw_data;
  611. struct ncp_mount_data_v4 *n = raw_data;
  612. n->dir_mode = c_n->dir_mode;
  613. n->file_mode = c_n->file_mode;
  614. n->gid = c_n->gid;
  615. n->uid = c_n->uid;
  616. n->retry_count = c_n->retry_count;
  617. n->time_out = c_n->time_out;
  618. n->ncp_fd = c_n->ncp_fd;
  619. n->wdog_pid = c_n->wdog_pid;
  620. n->mounted_uid = c_n->mounted_uid;
  621. n->flags = c_n->flags;
  622. } else if (version != 5) {
  623. return NULL;
  624. }
  625. return raw_data;
  626. }
  627. struct compat_nfs_string {
  628. compat_uint_t len;
  629. compat_uptr_t data;
  630. };
  631. static inline void compat_nfs_string(struct nfs_string *dst,
  632. struct compat_nfs_string *src)
  633. {
  634. dst->data = compat_ptr(src->data);
  635. dst->len = src->len;
  636. }
  637. struct compat_nfs4_mount_data_v1 {
  638. compat_int_t version;
  639. compat_int_t flags;
  640. compat_int_t rsize;
  641. compat_int_t wsize;
  642. compat_int_t timeo;
  643. compat_int_t retrans;
  644. compat_int_t acregmin;
  645. compat_int_t acregmax;
  646. compat_int_t acdirmin;
  647. compat_int_t acdirmax;
  648. struct compat_nfs_string client_addr;
  649. struct compat_nfs_string mnt_path;
  650. struct compat_nfs_string hostname;
  651. compat_uint_t host_addrlen;
  652. compat_uptr_t host_addr;
  653. compat_int_t proto;
  654. compat_int_t auth_flavourlen;
  655. compat_uptr_t auth_flavours;
  656. };
  657. static int do_nfs4_super_data_conv(void *raw_data)
  658. {
  659. int version = *(compat_uint_t *) raw_data;
  660. if (version == 1) {
  661. struct compat_nfs4_mount_data_v1 *raw = raw_data;
  662. struct nfs4_mount_data *real = raw_data;
  663. /* copy the fields backwards */
  664. real->auth_flavours = compat_ptr(raw->auth_flavours);
  665. real->auth_flavourlen = raw->auth_flavourlen;
  666. real->proto = raw->proto;
  667. real->host_addr = compat_ptr(raw->host_addr);
  668. real->host_addrlen = raw->host_addrlen;
  669. compat_nfs_string(&real->hostname, &raw->hostname);
  670. compat_nfs_string(&real->mnt_path, &raw->mnt_path);
  671. compat_nfs_string(&real->client_addr, &raw->client_addr);
  672. real->acdirmax = raw->acdirmax;
  673. real->acdirmin = raw->acdirmin;
  674. real->acregmax = raw->acregmax;
  675. real->acregmin = raw->acregmin;
  676. real->retrans = raw->retrans;
  677. real->timeo = raw->timeo;
  678. real->wsize = raw->wsize;
  679. real->rsize = raw->rsize;
  680. real->flags = raw->flags;
  681. real->version = raw->version;
  682. }
  683. return 0;
  684. }
  685. #define NCPFS_NAME "ncpfs"
  686. #define NFS4_NAME "nfs4"
  687. asmlinkage long compat_sys_mount(const char __user * dev_name,
  688. const char __user * dir_name,
  689. const char __user * type, unsigned long flags,
  690. const void __user * data)
  691. {
  692. char *kernel_type;
  693. unsigned long data_page;
  694. char *kernel_dev;
  695. char *dir_page;
  696. int retval;
  697. retval = copy_mount_string(type, &kernel_type);
  698. if (retval < 0)
  699. goto out;
  700. dir_page = getname(dir_name);
  701. retval = PTR_ERR(dir_page);
  702. if (IS_ERR(dir_page))
  703. goto out1;
  704. retval = copy_mount_string(dev_name, &kernel_dev);
  705. if (retval < 0)
  706. goto out2;
  707. retval = copy_mount_options(data, &data_page);
  708. if (retval < 0)
  709. goto out3;
  710. retval = -EINVAL;
  711. if (kernel_type && data_page) {
  712. if (!strcmp(kernel_type, NCPFS_NAME)) {
  713. do_ncp_super_data_conv((void *)data_page);
  714. } else if (!strcmp(kernel_type, NFS4_NAME)) {
  715. if (do_nfs4_super_data_conv((void *) data_page))
  716. goto out4;
  717. }
  718. }
  719. retval = do_mount(kernel_dev, dir_page, kernel_type,
  720. flags, (void*)data_page);
  721. out4:
  722. free_page(data_page);
  723. out3:
  724. kfree(kernel_dev);
  725. out2:
  726. putname(dir_page);
  727. out1:
  728. kfree(kernel_type);
  729. out:
  730. return retval;
  731. }
  732. struct compat_old_linux_dirent {
  733. compat_ulong_t d_ino;
  734. compat_ulong_t d_offset;
  735. unsigned short d_namlen;
  736. char d_name[1];
  737. };
  738. struct compat_readdir_callback {
  739. struct compat_old_linux_dirent __user *dirent;
  740. int result;
  741. };
  742. static int compat_fillonedir(void *__buf, const char *name, int namlen,
  743. loff_t offset, u64 ino, unsigned int d_type)
  744. {
  745. struct compat_readdir_callback *buf = __buf;
  746. struct compat_old_linux_dirent __user *dirent;
  747. compat_ulong_t d_ino;
  748. if (buf->result)
  749. return -EINVAL;
  750. d_ino = ino;
  751. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  752. buf->result = -EOVERFLOW;
  753. return -EOVERFLOW;
  754. }
  755. buf->result++;
  756. dirent = buf->dirent;
  757. if (!access_ok(VERIFY_WRITE, dirent,
  758. (unsigned long)(dirent->d_name + namlen + 1) -
  759. (unsigned long)dirent))
  760. goto efault;
  761. if ( __put_user(d_ino, &dirent->d_ino) ||
  762. __put_user(offset, &dirent->d_offset) ||
  763. __put_user(namlen, &dirent->d_namlen) ||
  764. __copy_to_user(dirent->d_name, name, namlen) ||
  765. __put_user(0, dirent->d_name + namlen))
  766. goto efault;
  767. return 0;
  768. efault:
  769. buf->result = -EFAULT;
  770. return -EFAULT;
  771. }
  772. asmlinkage long compat_sys_old_readdir(unsigned int fd,
  773. struct compat_old_linux_dirent __user *dirent, unsigned int count)
  774. {
  775. int error;
  776. struct file *file;
  777. int fput_needed;
  778. struct compat_readdir_callback buf;
  779. file = fget_light(fd, &fput_needed);
  780. if (!file)
  781. return -EBADF;
  782. buf.result = 0;
  783. buf.dirent = dirent;
  784. error = vfs_readdir(file, compat_fillonedir, &buf);
  785. if (buf.result)
  786. error = buf.result;
  787. fput_light(file, fput_needed);
  788. return error;
  789. }
  790. struct compat_linux_dirent {
  791. compat_ulong_t d_ino;
  792. compat_ulong_t d_off;
  793. unsigned short d_reclen;
  794. char d_name[1];
  795. };
  796. struct compat_getdents_callback {
  797. struct compat_linux_dirent __user *current_dir;
  798. struct compat_linux_dirent __user *previous;
  799. int count;
  800. int error;
  801. };
  802. static int compat_filldir(void *__buf, const char *name, int namlen,
  803. loff_t offset, u64 ino, unsigned int d_type)
  804. {
  805. struct compat_linux_dirent __user * dirent;
  806. struct compat_getdents_callback *buf = __buf;
  807. compat_ulong_t d_ino;
  808. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  809. namlen + 2, sizeof(compat_long_t));
  810. buf->error = -EINVAL; /* only used if we fail.. */
  811. if (reclen > buf->count)
  812. return -EINVAL;
  813. d_ino = ino;
  814. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  815. buf->error = -EOVERFLOW;
  816. return -EOVERFLOW;
  817. }
  818. dirent = buf->previous;
  819. if (dirent) {
  820. if (__put_user(offset, &dirent->d_off))
  821. goto efault;
  822. }
  823. dirent = buf->current_dir;
  824. if (__put_user(d_ino, &dirent->d_ino))
  825. goto efault;
  826. if (__put_user(reclen, &dirent->d_reclen))
  827. goto efault;
  828. if (copy_to_user(dirent->d_name, name, namlen))
  829. goto efault;
  830. if (__put_user(0, dirent->d_name + namlen))
  831. goto efault;
  832. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  833. goto efault;
  834. buf->previous = dirent;
  835. dirent = (void __user *)dirent + reclen;
  836. buf->current_dir = dirent;
  837. buf->count -= reclen;
  838. return 0;
  839. efault:
  840. buf->error = -EFAULT;
  841. return -EFAULT;
  842. }
  843. asmlinkage long compat_sys_getdents(unsigned int fd,
  844. struct compat_linux_dirent __user *dirent, unsigned int count)
  845. {
  846. struct file * file;
  847. struct compat_linux_dirent __user * lastdirent;
  848. struct compat_getdents_callback buf;
  849. int fput_needed;
  850. int error;
  851. if (!access_ok(VERIFY_WRITE, dirent, count))
  852. return -EFAULT;
  853. file = fget_light(fd, &fput_needed);
  854. if (!file)
  855. return -EBADF;
  856. buf.current_dir = dirent;
  857. buf.previous = NULL;
  858. buf.count = count;
  859. buf.error = 0;
  860. error = vfs_readdir(file, compat_filldir, &buf);
  861. if (error >= 0)
  862. error = buf.error;
  863. lastdirent = buf.previous;
  864. if (lastdirent) {
  865. if (put_user(file->f_pos, &lastdirent->d_off))
  866. error = -EFAULT;
  867. else
  868. error = count - buf.count;
  869. }
  870. fput_light(file, fput_needed);
  871. return error;
  872. }
  873. #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
  874. struct compat_getdents_callback64 {
  875. struct linux_dirent64 __user *current_dir;
  876. struct linux_dirent64 __user *previous;
  877. int count;
  878. int error;
  879. };
  880. static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  881. u64 ino, unsigned int d_type)
  882. {
  883. struct linux_dirent64 __user *dirent;
  884. struct compat_getdents_callback64 *buf = __buf;
  885. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  886. sizeof(u64));
  887. u64 off;
  888. buf->error = -EINVAL; /* only used if we fail.. */
  889. if (reclen > buf->count)
  890. return -EINVAL;
  891. dirent = buf->previous;
  892. if (dirent) {
  893. if (__put_user_unaligned(offset, &dirent->d_off))
  894. goto efault;
  895. }
  896. dirent = buf->current_dir;
  897. if (__put_user_unaligned(ino, &dirent->d_ino))
  898. goto efault;
  899. off = 0;
  900. if (__put_user_unaligned(off, &dirent->d_off))
  901. goto efault;
  902. if (__put_user(reclen, &dirent->d_reclen))
  903. goto efault;
  904. if (__put_user(d_type, &dirent->d_type))
  905. goto efault;
  906. if (copy_to_user(dirent->d_name, name, namlen))
  907. goto efault;
  908. if (__put_user(0, dirent->d_name + namlen))
  909. goto efault;
  910. buf->previous = dirent;
  911. dirent = (void __user *)dirent + reclen;
  912. buf->current_dir = dirent;
  913. buf->count -= reclen;
  914. return 0;
  915. efault:
  916. buf->error = -EFAULT;
  917. return -EFAULT;
  918. }
  919. asmlinkage long compat_sys_getdents64(unsigned int fd,
  920. struct linux_dirent64 __user * dirent, unsigned int count)
  921. {
  922. struct file * file;
  923. struct linux_dirent64 __user * lastdirent;
  924. struct compat_getdents_callback64 buf;
  925. int fput_needed;
  926. int error;
  927. if (!access_ok(VERIFY_WRITE, dirent, count))
  928. return -EFAULT;
  929. file = fget_light(fd, &fput_needed);
  930. if (!file)
  931. return -EBADF;
  932. buf.current_dir = dirent;
  933. buf.previous = NULL;
  934. buf.count = count;
  935. buf.error = 0;
  936. error = vfs_readdir(file, compat_filldir64, &buf);
  937. if (error >= 0)
  938. error = buf.error;
  939. lastdirent = buf.previous;
  940. if (lastdirent) {
  941. typeof(lastdirent->d_off) d_off = file->f_pos;
  942. if (__put_user_unaligned(d_off, &lastdirent->d_off))
  943. error = -EFAULT;
  944. else
  945. error = count - buf.count;
  946. }
  947. fput_light(file, fput_needed);
  948. return error;
  949. }
  950. #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
  951. static ssize_t compat_do_readv_writev(int type, struct file *file,
  952. const struct compat_iovec __user *uvector,
  953. unsigned long nr_segs, loff_t *pos)
  954. {
  955. compat_ssize_t tot_len;
  956. struct iovec iovstack[UIO_FASTIOV];
  957. struct iovec *iov = iovstack;
  958. ssize_t ret;
  959. io_fn_t fn;
  960. iov_fn_t fnv;
  961. ret = -EINVAL;
  962. if (!file->f_op)
  963. goto out;
  964. ret = -EFAULT;
  965. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  966. goto out;
  967. tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
  968. UIO_FASTIOV, iovstack, &iov, 1);
  969. if (tot_len == 0) {
  970. ret = 0;
  971. goto out;
  972. }
  973. ret = rw_verify_area(type, file, pos, tot_len);
  974. if (ret < 0)
  975. goto out;
  976. fnv = NULL;
  977. if (type == READ) {
  978. fn = file->f_op->read;
  979. fnv = file->f_op->aio_read;
  980. } else {
  981. fn = (io_fn_t)file->f_op->write;
  982. fnv = file->f_op->aio_write;
  983. }
  984. if (fnv)
  985. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  986. pos, fnv);
  987. else
  988. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  989. out:
  990. if (iov != iovstack)
  991. kfree(iov);
  992. if ((ret + (type == READ)) > 0) {
  993. if (type == READ)
  994. fsnotify_access(file);
  995. else
  996. fsnotify_modify(file);
  997. }
  998. return ret;
  999. }
  1000. static size_t compat_readv(struct file *file,
  1001. const struct compat_iovec __user *vec,
  1002. unsigned long vlen, loff_t *pos)
  1003. {
  1004. ssize_t ret = -EBADF;
  1005. if (!(file->f_mode & FMODE_READ))
  1006. goto out;
  1007. ret = -EINVAL;
  1008. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  1009. goto out;
  1010. ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
  1011. out:
  1012. if (ret > 0)
  1013. add_rchar(current, ret);
  1014. inc_syscr(current);
  1015. return ret;
  1016. }
  1017. asmlinkage ssize_t
  1018. compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec,
  1019. unsigned long vlen)
  1020. {
  1021. struct file *file;
  1022. int fput_needed;
  1023. ssize_t ret;
  1024. file = fget_light(fd, &fput_needed);
  1025. if (!file)
  1026. return -EBADF;
  1027. ret = compat_readv(file, vec, vlen, &file->f_pos);
  1028. fput_light(file, fput_needed);
  1029. return ret;
  1030. }
  1031. asmlinkage ssize_t
  1032. compat_sys_preadv64(unsigned long fd, const struct compat_iovec __user *vec,
  1033. unsigned long vlen, loff_t pos)
  1034. {
  1035. struct file *file;
  1036. int fput_needed;
  1037. ssize_t ret;
  1038. if (pos < 0)
  1039. return -EINVAL;
  1040. file = fget_light(fd, &fput_needed);
  1041. if (!file)
  1042. return -EBADF;
  1043. ret = -ESPIPE;
  1044. if (file->f_mode & FMODE_PREAD)
  1045. ret = compat_readv(file, vec, vlen, &pos);
  1046. fput_light(file, fput_needed);
  1047. return ret;
  1048. }
  1049. asmlinkage ssize_t
  1050. compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
  1051. unsigned long vlen, u32 pos_low, u32 pos_high)
  1052. {
  1053. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1054. return compat_sys_preadv64(fd, vec, vlen, pos);
  1055. }
  1056. static size_t compat_writev(struct file *file,
  1057. const struct compat_iovec __user *vec,
  1058. unsigned long vlen, loff_t *pos)
  1059. {
  1060. ssize_t ret = -EBADF;
  1061. if (!(file->f_mode & FMODE_WRITE))
  1062. goto out;
  1063. ret = -EINVAL;
  1064. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  1065. goto out;
  1066. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
  1067. out:
  1068. if (ret > 0)
  1069. add_wchar(current, ret);
  1070. inc_syscw(current);
  1071. return ret;
  1072. }
  1073. asmlinkage ssize_t
  1074. compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec,
  1075. unsigned long vlen)
  1076. {
  1077. struct file *file;
  1078. int fput_needed;
  1079. ssize_t ret;
  1080. file = fget_light(fd, &fput_needed);
  1081. if (!file)
  1082. return -EBADF;
  1083. ret = compat_writev(file, vec, vlen, &file->f_pos);
  1084. fput_light(file, fput_needed);
  1085. return ret;
  1086. }
  1087. asmlinkage ssize_t
  1088. compat_sys_pwritev64(unsigned long fd, const struct compat_iovec __user *vec,
  1089. unsigned long vlen, loff_t pos)
  1090. {
  1091. struct file *file;
  1092. int fput_needed;
  1093. ssize_t ret;
  1094. if (pos < 0)
  1095. return -EINVAL;
  1096. file = fget_light(fd, &fput_needed);
  1097. if (!file)
  1098. return -EBADF;
  1099. ret = -ESPIPE;
  1100. if (file->f_mode & FMODE_PWRITE)
  1101. ret = compat_writev(file, vec, vlen, &pos);
  1102. fput_light(file, fput_needed);
  1103. return ret;
  1104. }
  1105. asmlinkage ssize_t
  1106. compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec,
  1107. unsigned long vlen, u32 pos_low, u32 pos_high)
  1108. {
  1109. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1110. return compat_sys_pwritev64(fd, vec, vlen, pos);
  1111. }
  1112. asmlinkage long
  1113. compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
  1114. unsigned int nr_segs, unsigned int flags)
  1115. {
  1116. unsigned i;
  1117. struct iovec __user *iov;
  1118. if (nr_segs > UIO_MAXIOV)
  1119. return -EINVAL;
  1120. iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
  1121. for (i = 0; i < nr_segs; i++) {
  1122. struct compat_iovec v;
  1123. if (get_user(v.iov_base, &iov32[i].iov_base) ||
  1124. get_user(v.iov_len, &iov32[i].iov_len) ||
  1125. put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
  1126. put_user(v.iov_len, &iov[i].iov_len))
  1127. return -EFAULT;
  1128. }
  1129. return sys_vmsplice(fd, iov, nr_segs, flags);
  1130. }
  1131. /*
  1132. * Exactly like fs/open.c:sys_open(), except that it doesn't set the
  1133. * O_LARGEFILE flag.
  1134. */
  1135. asmlinkage long
  1136. compat_sys_open(const char __user *filename, int flags, umode_t mode)
  1137. {
  1138. return do_sys_open(AT_FDCWD, filename, flags, mode);
  1139. }
  1140. /*
  1141. * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
  1142. * O_LARGEFILE flag.
  1143. */
  1144. asmlinkage long
  1145. compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, umode_t mode)
  1146. {
  1147. return do_sys_open(dfd, filename, flags, mode);
  1148. }
  1149. #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
  1150. static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
  1151. int timeval, int ret)
  1152. {
  1153. struct timespec ts;
  1154. if (!p)
  1155. return ret;
  1156. if (current->personality & STICKY_TIMEOUTS)
  1157. goto sticky;
  1158. /* No update for zero timeout */
  1159. if (!end_time->tv_sec && !end_time->tv_nsec)
  1160. return ret;
  1161. ktime_get_ts(&ts);
  1162. ts = timespec_sub(*end_time, ts);
  1163. if (ts.tv_sec < 0)
  1164. ts.tv_sec = ts.tv_nsec = 0;
  1165. if (timeval) {
  1166. struct compat_timeval rtv;
  1167. rtv.tv_sec = ts.tv_sec;
  1168. rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  1169. if (!copy_to_user(p, &rtv, sizeof(rtv)))
  1170. return ret;
  1171. } else {
  1172. struct compat_timespec rts;
  1173. rts.tv_sec = ts.tv_sec;
  1174. rts.tv_nsec = ts.tv_nsec;
  1175. if (!copy_to_user(p, &rts, sizeof(rts)))
  1176. return ret;
  1177. }
  1178. /*
  1179. * If an application puts its timeval in read-only memory, we
  1180. * don't want the Linux-specific update to the timeval to
  1181. * cause a fault after the select has completed
  1182. * successfully. However, because we're not updating the
  1183. * timeval, we can't restart the system call.
  1184. */
  1185. sticky:
  1186. if (ret == -ERESTARTNOHAND)
  1187. ret = -EINTR;
  1188. return ret;
  1189. }
  1190. /*
  1191. * Ooo, nasty. We need here to frob 32-bit unsigned longs to
  1192. * 64-bit unsigned longs.
  1193. */
  1194. static
  1195. int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1196. unsigned long *fdset)
  1197. {
  1198. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1199. if (ufdset) {
  1200. unsigned long odd;
  1201. if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
  1202. return -EFAULT;
  1203. odd = nr & 1UL;
  1204. nr &= ~1UL;
  1205. while (nr) {
  1206. unsigned long h, l;
  1207. if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
  1208. return -EFAULT;
  1209. ufdset += 2;
  1210. *fdset++ = h << 32 | l;
  1211. nr -= 2;
  1212. }
  1213. if (odd && __get_user(*fdset, ufdset))
  1214. return -EFAULT;
  1215. } else {
  1216. /* Tricky, must clear full unsigned long in the
  1217. * kernel fdset at the end, this makes sure that
  1218. * actually happens.
  1219. */
  1220. memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
  1221. }
  1222. return 0;
  1223. }
  1224. static
  1225. int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
  1226. unsigned long *fdset)
  1227. {
  1228. unsigned long odd;
  1229. nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
  1230. if (!ufdset)
  1231. return 0;
  1232. odd = nr & 1UL;
  1233. nr &= ~1UL;
  1234. while (nr) {
  1235. unsigned long h, l;
  1236. l = *fdset++;
  1237. h = l >> 32;
  1238. if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
  1239. return -EFAULT;
  1240. ufdset += 2;
  1241. nr -= 2;
  1242. }
  1243. if (odd && __put_user(*fdset, ufdset))
  1244. return -EFAULT;
  1245. return 0;
  1246. }
  1247. /*
  1248. * This is a virtual copy of sys_select from fs/select.c and probably
  1249. * should be compared to it from time to time
  1250. */
  1251. /*
  1252. * We can actually return ERESTARTSYS instead of EINTR, but I'd
  1253. * like to be certain this leads to no problems. So I return
  1254. * EINTR just for safety.
  1255. *
  1256. * Update: ERESTARTSYS breaks at least the xview clock binary, so
  1257. * I'm trying ERESTARTNOHAND which restart only when you want to.
  1258. */
  1259. int compat_core_sys_select(int n, compat_ulong_t __user *inp,
  1260. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1261. struct timespec *end_time)
  1262. {
  1263. fd_set_bits fds;
  1264. void *bits;
  1265. int size, max_fds, ret = -EINVAL;
  1266. struct fdtable *fdt;
  1267. long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
  1268. if (n < 0)
  1269. goto out_nofds;
  1270. /* max_fds can increase, so grab it once to avoid race */
  1271. rcu_read_lock();
  1272. fdt = files_fdtable(current->files);
  1273. max_fds = fdt->max_fds;
  1274. rcu_read_unlock();
  1275. if (n > max_fds)
  1276. n = max_fds;
  1277. /*
  1278. * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
  1279. * since we used fdset we need to allocate memory in units of
  1280. * long-words.
  1281. */
  1282. size = FDS_BYTES(n);
  1283. bits = stack_fds;
  1284. if (size > sizeof(stack_fds) / 6) {
  1285. bits = kmalloc(6 * size, GFP_KERNEL);
  1286. ret = -ENOMEM;
  1287. if (!bits)
  1288. goto out_nofds;
  1289. }
  1290. fds.in = (unsigned long *) bits;
  1291. fds.out = (unsigned long *) (bits + size);
  1292. fds.ex = (unsigned long *) (bits + 2*size);
  1293. fds.res_in = (unsigned long *) (bits + 3*size);
  1294. fds.res_out = (unsigned long *) (bits + 4*size);
  1295. fds.res_ex = (unsigned long *) (bits + 5*size);
  1296. if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
  1297. (ret = compat_get_fd_set(n, outp, fds.out)) ||
  1298. (ret = compat_get_fd_set(n, exp, fds.ex)))
  1299. goto out;
  1300. zero_fd_set(n, fds.res_in);
  1301. zero_fd_set(n, fds.res_out);
  1302. zero_fd_set(n, fds.res_ex);
  1303. ret = do_select(n, &fds, end_time);
  1304. if (ret < 0)
  1305. goto out;
  1306. if (!ret) {
  1307. ret = -ERESTARTNOHAND;
  1308. if (signal_pending(current))
  1309. goto out;
  1310. ret = 0;
  1311. }
  1312. if (compat_set_fd_set(n, inp, fds.res_in) ||
  1313. compat_set_fd_set(n, outp, fds.res_out) ||
  1314. compat_set_fd_set(n, exp, fds.res_ex))
  1315. ret = -EFAULT;
  1316. out:
  1317. if (bits != stack_fds)
  1318. kfree(bits);
  1319. out_nofds:
  1320. return ret;
  1321. }
  1322. asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
  1323. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1324. struct compat_timeval __user *tvp)
  1325. {
  1326. struct timespec end_time, *to = NULL;
  1327. struct compat_timeval tv;
  1328. int ret;
  1329. if (tvp) {
  1330. if (copy_from_user(&tv, tvp, sizeof(tv)))
  1331. return -EFAULT;
  1332. to = &end_time;
  1333. if (poll_select_set_timeout(to,
  1334. tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
  1335. (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
  1336. return -EINVAL;
  1337. }
  1338. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1339. ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
  1340. return ret;
  1341. }
  1342. struct compat_sel_arg_struct {
  1343. compat_ulong_t n;
  1344. compat_uptr_t inp;
  1345. compat_uptr_t outp;
  1346. compat_uptr_t exp;
  1347. compat_uptr_t tvp;
  1348. };
  1349. asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
  1350. {
  1351. struct compat_sel_arg_struct a;
  1352. if (copy_from_user(&a, arg, sizeof(a)))
  1353. return -EFAULT;
  1354. return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
  1355. compat_ptr(a.exp), compat_ptr(a.tvp));
  1356. }
  1357. #ifdef HAVE_SET_RESTORE_SIGMASK
  1358. static long do_compat_pselect(int n, compat_ulong_t __user *inp,
  1359. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1360. struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
  1361. compat_size_t sigsetsize)
  1362. {
  1363. compat_sigset_t ss32;
  1364. sigset_t ksigmask, sigsaved;
  1365. struct compat_timespec ts;
  1366. struct timespec end_time, *to = NULL;
  1367. int ret;
  1368. if (tsp) {
  1369. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1370. return -EFAULT;
  1371. to = &end_time;
  1372. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1373. return -EINVAL;
  1374. }
  1375. if (sigmask) {
  1376. if (sigsetsize != sizeof(compat_sigset_t))
  1377. return -EINVAL;
  1378. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1379. return -EFAULT;
  1380. sigset_from_compat(&ksigmask, &ss32);
  1381. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1382. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1383. }
  1384. ret = compat_core_sys_select(n, inp, outp, exp, to);
  1385. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1386. if (ret == -ERESTARTNOHAND) {
  1387. /*
  1388. * Don't restore the signal mask yet. Let do_signal() deliver
  1389. * the signal on the way back to userspace, before the signal
  1390. * mask is restored.
  1391. */
  1392. if (sigmask) {
  1393. memcpy(&current->saved_sigmask, &sigsaved,
  1394. sizeof(sigsaved));
  1395. set_restore_sigmask();
  1396. }
  1397. } else if (sigmask)
  1398. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1399. return ret;
  1400. }
  1401. asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
  1402. compat_ulong_t __user *outp, compat_ulong_t __user *exp,
  1403. struct compat_timespec __user *tsp, void __user *sig)
  1404. {
  1405. compat_size_t sigsetsize = 0;
  1406. compat_uptr_t up = 0;
  1407. if (sig) {
  1408. if (!access_ok(VERIFY_READ, sig,
  1409. sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
  1410. __get_user(up, (compat_uptr_t __user *)sig) ||
  1411. __get_user(sigsetsize,
  1412. (compat_size_t __user *)(sig+sizeof(up))))
  1413. return -EFAULT;
  1414. }
  1415. return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
  1416. sigsetsize);
  1417. }
  1418. asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
  1419. unsigned int nfds, struct compat_timespec __user *tsp,
  1420. const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
  1421. {
  1422. compat_sigset_t ss32;
  1423. sigset_t ksigmask, sigsaved;
  1424. struct compat_timespec ts;
  1425. struct timespec end_time, *to = NULL;
  1426. int ret;
  1427. if (tsp) {
  1428. if (copy_from_user(&ts, tsp, sizeof(ts)))
  1429. return -EFAULT;
  1430. to = &end_time;
  1431. if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
  1432. return -EINVAL;
  1433. }
  1434. if (sigmask) {
  1435. if (sigsetsize != sizeof(compat_sigset_t))
  1436. return -EINVAL;
  1437. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1438. return -EFAULT;
  1439. sigset_from_compat(&ksigmask, &ss32);
  1440. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1441. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1442. }
  1443. ret = do_sys_poll(ufds, nfds, to);
  1444. /* We can restart this syscall, usually */
  1445. if (ret == -EINTR) {
  1446. /*
  1447. * Don't restore the signal mask yet. Let do_signal() deliver
  1448. * the signal on the way back to userspace, before the signal
  1449. * mask is restored.
  1450. */
  1451. if (sigmask) {
  1452. memcpy(&current->saved_sigmask, &sigsaved,
  1453. sizeof(sigsaved));
  1454. set_restore_sigmask();
  1455. }
  1456. ret = -ERESTARTNOHAND;
  1457. } else if (sigmask)
  1458. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1459. ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
  1460. return ret;
  1461. }
  1462. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1463. #ifdef CONFIG_EPOLL
  1464. #ifdef HAVE_SET_RESTORE_SIGMASK
  1465. asmlinkage long compat_sys_epoll_pwait(int epfd,
  1466. struct compat_epoll_event __user *events,
  1467. int maxevents, int timeout,
  1468. const compat_sigset_t __user *sigmask,
  1469. compat_size_t sigsetsize)
  1470. {
  1471. long err;
  1472. compat_sigset_t csigmask;
  1473. sigset_t ksigmask, sigsaved;
  1474. /*
  1475. * If the caller wants a certain signal mask to be set during the wait,
  1476. * we apply it here.
  1477. */
  1478. if (sigmask) {
  1479. if (sigsetsize != sizeof(compat_sigset_t))
  1480. return -EINVAL;
  1481. if (copy_from_user(&csigmask, sigmask, sizeof(csigmask)))
  1482. return -EFAULT;
  1483. sigset_from_compat(&ksigmask, &csigmask);
  1484. sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  1485. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1486. }
  1487. err = sys_epoll_wait(epfd, events, maxevents, timeout);
  1488. /*
  1489. * If we changed the signal mask, we need to restore the original one.
  1490. * In case we've got a signal while waiting, we do not restore the
  1491. * signal mask yet, and we allow do_signal() to deliver the signal on
  1492. * the way back to userspace, before the signal mask is restored.
  1493. */
  1494. if (sigmask) {
  1495. if (err == -EINTR) {
  1496. memcpy(&current->saved_sigmask, &sigsaved,
  1497. sizeof(sigsaved));
  1498. set_restore_sigmask();
  1499. } else
  1500. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1501. }
  1502. return err;
  1503. }
  1504. #endif /* HAVE_SET_RESTORE_SIGMASK */
  1505. #endif /* CONFIG_EPOLL */
  1506. #ifdef CONFIG_SIGNALFD
  1507. asmlinkage long compat_sys_signalfd4(int ufd,
  1508. const compat_sigset_t __user *sigmask,
  1509. compat_size_t sigsetsize, int flags)
  1510. {
  1511. compat_sigset_t ss32;
  1512. sigset_t tmp;
  1513. sigset_t __user *ksigmask;
  1514. if (sigsetsize != sizeof(compat_sigset_t))
  1515. return -EINVAL;
  1516. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  1517. return -EFAULT;
  1518. sigset_from_compat(&tmp, &ss32);
  1519. ksigmask = compat_alloc_user_space(sizeof(sigset_t));
  1520. if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
  1521. return -EFAULT;
  1522. return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
  1523. }
  1524. asmlinkage long compat_sys_signalfd(int ufd,
  1525. const compat_sigset_t __user *sigmask,
  1526. compat_size_t sigsetsize)
  1527. {
  1528. return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
  1529. }
  1530. #endif /* CONFIG_SIGNALFD */
  1531. #ifdef CONFIG_TIMERFD
  1532. asmlinkage long compat_sys_timerfd_settime(int ufd, int flags,
  1533. const struct compat_itimerspec __user *utmr,
  1534. struct compat_itimerspec __user *otmr)
  1535. {
  1536. int error;
  1537. struct itimerspec t;
  1538. struct itimerspec __user *ut;
  1539. if (get_compat_itimerspec(&t, utmr))
  1540. return -EFAULT;
  1541. ut = compat_alloc_user_space(2 * sizeof(struct itimerspec));
  1542. if (copy_to_user(&ut[0], &t, sizeof(t)))
  1543. return -EFAULT;
  1544. error = sys_timerfd_settime(ufd, flags, &ut[0], &ut[1]);
  1545. if (!error && otmr)
  1546. error = (copy_from_user(&t, &ut[1], sizeof(struct itimerspec)) ||
  1547. put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
  1548. return error;
  1549. }
  1550. asmlinkage long compat_sys_timerfd_gettime(int ufd,
  1551. struct compat_itimerspec __user *otmr)
  1552. {
  1553. int error;
  1554. struct itimerspec t;
  1555. struct itimerspec __user *ut;
  1556. ut = compat_alloc_user_space(sizeof(struct itimerspec));
  1557. error = sys_timerfd_gettime(ufd, ut);
  1558. if (!error)
  1559. error = (copy_from_user(&t, ut, sizeof(struct itimerspec)) ||
  1560. put_compat_itimerspec(otmr, &t)) ? -EFAULT: 0;
  1561. return error;
  1562. }
  1563. #endif /* CONFIG_TIMERFD */
  1564. #ifdef CONFIG_FHANDLE
  1565. /*
  1566. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  1567. * doesn't set the O_LARGEFILE flag.
  1568. */
  1569. asmlinkage long
  1570. compat_sys_open_by_handle_at(int mountdirfd,
  1571. struct file_handle __user *handle, int flags)
  1572. {
  1573. return do_handle_open(mountdirfd, handle, flags);
  1574. }
  1575. #endif