sys.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/export.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/reboot.h>
  11. #include <linux/prctl.h>
  12. #include <linux/highuid.h>
  13. #include <linux/fs.h>
  14. #include <linux/kmod.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/resource.h>
  17. #include <linux/kernel.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/capability.h>
  20. #include <linux/device.h>
  21. #include <linux/key.h>
  22. #include <linux/times.h>
  23. #include <linux/posix-timers.h>
  24. #include <linux/security.h>
  25. #include <linux/dcookies.h>
  26. #include <linux/suspend.h>
  27. #include <linux/tty.h>
  28. #include <linux/signal.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/seccomp.h>
  33. #include <linux/cpu.h>
  34. #include <linux/personality.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/fs_struct.h>
  37. #include <linux/file.h>
  38. #include <linux/mount.h>
  39. #include <linux/gfp.h>
  40. #include <linux/syscore_ops.h>
  41. #include <linux/version.h>
  42. #include <linux/ctype.h>
  43. #include <linux/compat.h>
  44. #include <linux/syscalls.h>
  45. #include <linux/kprobes.h>
  46. #include <linux/user_namespace.h>
  47. #include <linux/binfmts.h>
  48. #include <linux/sched.h>
  49. #include <linux/rcupdate.h>
  50. #include <linux/uidgid.h>
  51. #include <linux/cred.h>
  52. #include <linux/kmsg_dump.h>
  53. /* Move somewhere else to avoid recompiling? */
  54. #include <generated/utsrelease.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/io.h>
  57. #include <asm/unistd.h>
  58. #ifndef SET_UNALIGN_CTL
  59. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  60. #endif
  61. #ifndef GET_UNALIGN_CTL
  62. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  63. #endif
  64. #ifndef SET_FPEMU_CTL
  65. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  66. #endif
  67. #ifndef GET_FPEMU_CTL
  68. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  69. #endif
  70. #ifndef SET_FPEXC_CTL
  71. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  72. #endif
  73. #ifndef GET_FPEXC_CTL
  74. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  75. #endif
  76. #ifndef GET_ENDIAN
  77. # define GET_ENDIAN(a,b) (-EINVAL)
  78. #endif
  79. #ifndef SET_ENDIAN
  80. # define SET_ENDIAN(a,b) (-EINVAL)
  81. #endif
  82. #ifndef GET_TSC_CTL
  83. # define GET_TSC_CTL(a) (-EINVAL)
  84. #endif
  85. #ifndef SET_TSC_CTL
  86. # define SET_TSC_CTL(a) (-EINVAL)
  87. #endif
  88. /*
  89. * this is where the system-wide overflow UID and GID are defined, for
  90. * architectures that now have 32-bit UID/GID but didn't in the past
  91. */
  92. int overflowuid = DEFAULT_OVERFLOWUID;
  93. int overflowgid = DEFAULT_OVERFLOWGID;
  94. EXPORT_SYMBOL(overflowuid);
  95. EXPORT_SYMBOL(overflowgid);
  96. /*
  97. * the same as above, but for filesystems which can only store a 16-bit
  98. * UID and GID. as such, this is needed on all architectures
  99. */
  100. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  101. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  102. EXPORT_SYMBOL(fs_overflowuid);
  103. EXPORT_SYMBOL(fs_overflowgid);
  104. /*
  105. * Returns true if current's euid is same as p's uid or euid,
  106. * or has CAP_SYS_NICE to p's user_ns.
  107. *
  108. * Called with rcu_read_lock, creds are safe
  109. */
  110. static bool set_one_prio_perm(struct task_struct *p)
  111. {
  112. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  113. if (uid_eq(pcred->uid, cred->euid) ||
  114. uid_eq(pcred->euid, cred->euid))
  115. return true;
  116. if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
  117. return true;
  118. return false;
  119. }
  120. /*
  121. * set the priority of a task
  122. * - the caller must hold the RCU read lock
  123. */
  124. static int set_one_prio(struct task_struct *p, int niceval, int error)
  125. {
  126. int no_nice;
  127. if (!set_one_prio_perm(p)) {
  128. error = -EPERM;
  129. goto out;
  130. }
  131. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  132. error = -EACCES;
  133. goto out;
  134. }
  135. no_nice = security_task_setnice(p, niceval);
  136. if (no_nice) {
  137. error = no_nice;
  138. goto out;
  139. }
  140. if (error == -ESRCH)
  141. error = 0;
  142. set_user_nice(p, niceval);
  143. out:
  144. return error;
  145. }
  146. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  147. {
  148. struct task_struct *g, *p;
  149. struct user_struct *user;
  150. const struct cred *cred = current_cred();
  151. int error = -EINVAL;
  152. struct pid *pgrp;
  153. kuid_t uid;
  154. if (which > PRIO_USER || which < PRIO_PROCESS)
  155. goto out;
  156. /* normalize: avoid signed division (rounding problems) */
  157. error = -ESRCH;
  158. if (niceval < -20)
  159. niceval = -20;
  160. if (niceval > 19)
  161. niceval = 19;
  162. rcu_read_lock();
  163. read_lock(&tasklist_lock);
  164. switch (which) {
  165. case PRIO_PROCESS:
  166. if (who)
  167. p = find_task_by_vpid(who);
  168. else
  169. p = current;
  170. if (p)
  171. error = set_one_prio(p, niceval, error);
  172. break;
  173. case PRIO_PGRP:
  174. if (who)
  175. pgrp = find_vpid(who);
  176. else
  177. pgrp = task_pgrp(current);
  178. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  179. error = set_one_prio(p, niceval, error);
  180. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  181. break;
  182. case PRIO_USER:
  183. uid = make_kuid(cred->user_ns, who);
  184. user = cred->user;
  185. if (!who)
  186. uid = cred->uid;
  187. else if (!uid_eq(uid, cred->uid) &&
  188. !(user = find_user(uid)))
  189. goto out_unlock; /* No processes for this user */
  190. do_each_thread(g, p) {
  191. if (uid_eq(task_uid(p), uid))
  192. error = set_one_prio(p, niceval, error);
  193. } while_each_thread(g, p);
  194. if (!uid_eq(uid, cred->uid))
  195. free_uid(user); /* For find_user() */
  196. break;
  197. }
  198. out_unlock:
  199. read_unlock(&tasklist_lock);
  200. rcu_read_unlock();
  201. out:
  202. return error;
  203. }
  204. /*
  205. * Ugh. To avoid negative return values, "getpriority()" will
  206. * not return the normal nice-value, but a negated value that
  207. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  208. * to stay compatible.
  209. */
  210. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  211. {
  212. struct task_struct *g, *p;
  213. struct user_struct *user;
  214. const struct cred *cred = current_cred();
  215. long niceval, retval = -ESRCH;
  216. struct pid *pgrp;
  217. kuid_t uid;
  218. if (which > PRIO_USER || which < PRIO_PROCESS)
  219. return -EINVAL;
  220. rcu_read_lock();
  221. read_lock(&tasklist_lock);
  222. switch (which) {
  223. case PRIO_PROCESS:
  224. if (who)
  225. p = find_task_by_vpid(who);
  226. else
  227. p = current;
  228. if (p) {
  229. niceval = 20 - task_nice(p);
  230. if (niceval > retval)
  231. retval = niceval;
  232. }
  233. break;
  234. case PRIO_PGRP:
  235. if (who)
  236. pgrp = find_vpid(who);
  237. else
  238. pgrp = task_pgrp(current);
  239. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  240. niceval = 20 - task_nice(p);
  241. if (niceval > retval)
  242. retval = niceval;
  243. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  244. break;
  245. case PRIO_USER:
  246. uid = make_kuid(cred->user_ns, who);
  247. user = cred->user;
  248. if (!who)
  249. uid = cred->uid;
  250. else if (!uid_eq(uid, cred->uid) &&
  251. !(user = find_user(uid)))
  252. goto out_unlock; /* No processes for this user */
  253. do_each_thread(g, p) {
  254. if (uid_eq(task_uid(p), uid)) {
  255. niceval = 20 - task_nice(p);
  256. if (niceval > retval)
  257. retval = niceval;
  258. }
  259. } while_each_thread(g, p);
  260. if (!uid_eq(uid, cred->uid))
  261. free_uid(user); /* for find_user() */
  262. break;
  263. }
  264. out_unlock:
  265. read_unlock(&tasklist_lock);
  266. rcu_read_unlock();
  267. return retval;
  268. }
  269. /*
  270. * Unprivileged users may change the real gid to the effective gid
  271. * or vice versa. (BSD-style)
  272. *
  273. * If you set the real gid at all, or set the effective gid to a value not
  274. * equal to the real gid, then the saved gid is set to the new effective gid.
  275. *
  276. * This makes it possible for a setgid program to completely drop its
  277. * privileges, which is often a useful assertion to make when you are doing
  278. * a security audit over a program.
  279. *
  280. * The general idea is that a program which uses just setregid() will be
  281. * 100% compatible with BSD. A program which uses just setgid() will be
  282. * 100% compatible with POSIX with saved IDs.
  283. *
  284. * SMP: There are not races, the GIDs are checked only by filesystem
  285. * operations (as far as semantic preservation is concerned).
  286. */
  287. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  288. {
  289. struct user_namespace *ns = current_user_ns();
  290. const struct cred *old;
  291. struct cred *new;
  292. int retval;
  293. kgid_t krgid, kegid;
  294. krgid = make_kgid(ns, rgid);
  295. kegid = make_kgid(ns, egid);
  296. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  297. return -EINVAL;
  298. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  299. return -EINVAL;
  300. new = prepare_creds();
  301. if (!new)
  302. return -ENOMEM;
  303. old = current_cred();
  304. retval = -EPERM;
  305. if (rgid != (gid_t) -1) {
  306. if (gid_eq(old->gid, krgid) ||
  307. gid_eq(old->egid, krgid) ||
  308. ns_capable(old->user_ns, CAP_SETGID))
  309. new->gid = krgid;
  310. else
  311. goto error;
  312. }
  313. if (egid != (gid_t) -1) {
  314. if (gid_eq(old->gid, kegid) ||
  315. gid_eq(old->egid, kegid) ||
  316. gid_eq(old->sgid, kegid) ||
  317. ns_capable(old->user_ns, CAP_SETGID))
  318. new->egid = kegid;
  319. else
  320. goto error;
  321. }
  322. if (rgid != (gid_t) -1 ||
  323. (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
  324. new->sgid = new->egid;
  325. new->fsgid = new->egid;
  326. return commit_creds(new);
  327. error:
  328. abort_creds(new);
  329. return retval;
  330. }
  331. /*
  332. * setgid() is implemented like SysV w/ SAVED_IDS
  333. *
  334. * SMP: Same implicit races as above.
  335. */
  336. SYSCALL_DEFINE1(setgid, gid_t, gid)
  337. {
  338. struct user_namespace *ns = current_user_ns();
  339. const struct cred *old;
  340. struct cred *new;
  341. int retval;
  342. kgid_t kgid;
  343. kgid = make_kgid(ns, gid);
  344. if (!gid_valid(kgid))
  345. return -EINVAL;
  346. new = prepare_creds();
  347. if (!new)
  348. return -ENOMEM;
  349. old = current_cred();
  350. retval = -EPERM;
  351. if (ns_capable(old->user_ns, CAP_SETGID))
  352. new->gid = new->egid = new->sgid = new->fsgid = kgid;
  353. else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
  354. new->egid = new->fsgid = kgid;
  355. else
  356. goto error;
  357. return commit_creds(new);
  358. error:
  359. abort_creds(new);
  360. return retval;
  361. }
  362. /*
  363. * change the user struct in a credentials set to match the new UID
  364. */
  365. static int set_user(struct cred *new)
  366. {
  367. struct user_struct *new_user;
  368. new_user = alloc_uid(new->uid);
  369. if (!new_user)
  370. return -EAGAIN;
  371. /*
  372. * We don't fail in case of NPROC limit excess here because too many
  373. * poorly written programs don't check set*uid() return code, assuming
  374. * it never fails if called by root. We may still enforce NPROC limit
  375. * for programs doing set*uid()+execve() by harmlessly deferring the
  376. * failure to the execve() stage.
  377. */
  378. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  379. new_user != INIT_USER)
  380. current->flags |= PF_NPROC_EXCEEDED;
  381. else
  382. current->flags &= ~PF_NPROC_EXCEEDED;
  383. free_uid(new->user);
  384. new->user = new_user;
  385. return 0;
  386. }
  387. /*
  388. * Unprivileged users may change the real uid to the effective uid
  389. * or vice versa. (BSD-style)
  390. *
  391. * If you set the real uid at all, or set the effective uid to a value not
  392. * equal to the real uid, then the saved uid is set to the new effective uid.
  393. *
  394. * This makes it possible for a setuid program to completely drop its
  395. * privileges, which is often a useful assertion to make when you are doing
  396. * a security audit over a program.
  397. *
  398. * The general idea is that a program which uses just setreuid() will be
  399. * 100% compatible with BSD. A program which uses just setuid() will be
  400. * 100% compatible with POSIX with saved IDs.
  401. */
  402. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  403. {
  404. struct user_namespace *ns = current_user_ns();
  405. const struct cred *old;
  406. struct cred *new;
  407. int retval;
  408. kuid_t kruid, keuid;
  409. kruid = make_kuid(ns, ruid);
  410. keuid = make_kuid(ns, euid);
  411. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  412. return -EINVAL;
  413. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  414. return -EINVAL;
  415. new = prepare_creds();
  416. if (!new)
  417. return -ENOMEM;
  418. old = current_cred();
  419. retval = -EPERM;
  420. if (ruid != (uid_t) -1) {
  421. new->uid = kruid;
  422. if (!uid_eq(old->uid, kruid) &&
  423. !uid_eq(old->euid, kruid) &&
  424. !ns_capable(old->user_ns, CAP_SETUID))
  425. goto error;
  426. }
  427. if (euid != (uid_t) -1) {
  428. new->euid = keuid;
  429. if (!uid_eq(old->uid, keuid) &&
  430. !uid_eq(old->euid, keuid) &&
  431. !uid_eq(old->suid, keuid) &&
  432. !ns_capable(old->user_ns, CAP_SETUID))
  433. goto error;
  434. }
  435. if (!uid_eq(new->uid, old->uid)) {
  436. retval = set_user(new);
  437. if (retval < 0)
  438. goto error;
  439. }
  440. if (ruid != (uid_t) -1 ||
  441. (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
  442. new->suid = new->euid;
  443. new->fsuid = new->euid;
  444. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  445. if (retval < 0)
  446. goto error;
  447. return commit_creds(new);
  448. error:
  449. abort_creds(new);
  450. return retval;
  451. }
  452. /*
  453. * setuid() is implemented like SysV with SAVED_IDS
  454. *
  455. * Note that SAVED_ID's is deficient in that a setuid root program
  456. * like sendmail, for example, cannot set its uid to be a normal
  457. * user and then switch back, because if you're root, setuid() sets
  458. * the saved uid too. If you don't like this, blame the bright people
  459. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  460. * will allow a root program to temporarily drop privileges and be able to
  461. * regain them by swapping the real and effective uid.
  462. */
  463. SYSCALL_DEFINE1(setuid, uid_t, uid)
  464. {
  465. struct user_namespace *ns = current_user_ns();
  466. const struct cred *old;
  467. struct cred *new;
  468. int retval;
  469. kuid_t kuid;
  470. kuid = make_kuid(ns, uid);
  471. if (!uid_valid(kuid))
  472. return -EINVAL;
  473. new = prepare_creds();
  474. if (!new)
  475. return -ENOMEM;
  476. old = current_cred();
  477. retval = -EPERM;
  478. if (ns_capable(old->user_ns, CAP_SETUID)) {
  479. new->suid = new->uid = kuid;
  480. if (!uid_eq(kuid, old->uid)) {
  481. retval = set_user(new);
  482. if (retval < 0)
  483. goto error;
  484. }
  485. } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
  486. goto error;
  487. }
  488. new->fsuid = new->euid = kuid;
  489. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  490. if (retval < 0)
  491. goto error;
  492. return commit_creds(new);
  493. error:
  494. abort_creds(new);
  495. return retval;
  496. }
  497. /*
  498. * This function implements a generic ability to update ruid, euid,
  499. * and suid. This allows you to implement the 4.4 compatible seteuid().
  500. */
  501. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  502. {
  503. struct user_namespace *ns = current_user_ns();
  504. const struct cred *old;
  505. struct cred *new;
  506. int retval;
  507. kuid_t kruid, keuid, ksuid;
  508. kruid = make_kuid(ns, ruid);
  509. keuid = make_kuid(ns, euid);
  510. ksuid = make_kuid(ns, suid);
  511. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  512. return -EINVAL;
  513. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  514. return -EINVAL;
  515. if ((suid != (uid_t) -1) && !uid_valid(ksuid))
  516. return -EINVAL;
  517. new = prepare_creds();
  518. if (!new)
  519. return -ENOMEM;
  520. old = current_cred();
  521. retval = -EPERM;
  522. if (!ns_capable(old->user_ns, CAP_SETUID)) {
  523. if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
  524. !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
  525. goto error;
  526. if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
  527. !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
  528. goto error;
  529. if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
  530. !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
  531. goto error;
  532. }
  533. if (ruid != (uid_t) -1) {
  534. new->uid = kruid;
  535. if (!uid_eq(kruid, old->uid)) {
  536. retval = set_user(new);
  537. if (retval < 0)
  538. goto error;
  539. }
  540. }
  541. if (euid != (uid_t) -1)
  542. new->euid = keuid;
  543. if (suid != (uid_t) -1)
  544. new->suid = ksuid;
  545. new->fsuid = new->euid;
  546. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  547. if (retval < 0)
  548. goto error;
  549. return commit_creds(new);
  550. error:
  551. abort_creds(new);
  552. return retval;
  553. }
  554. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
  555. {
  556. const struct cred *cred = current_cred();
  557. int retval;
  558. uid_t ruid, euid, suid;
  559. ruid = from_kuid_munged(cred->user_ns, cred->uid);
  560. euid = from_kuid_munged(cred->user_ns, cred->euid);
  561. suid = from_kuid_munged(cred->user_ns, cred->suid);
  562. if (!(retval = put_user(ruid, ruidp)) &&
  563. !(retval = put_user(euid, euidp)))
  564. retval = put_user(suid, suidp);
  565. return retval;
  566. }
  567. /*
  568. * Same as above, but for rgid, egid, sgid.
  569. */
  570. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  571. {
  572. struct user_namespace *ns = current_user_ns();
  573. const struct cred *old;
  574. struct cred *new;
  575. int retval;
  576. kgid_t krgid, kegid, ksgid;
  577. krgid = make_kgid(ns, rgid);
  578. kegid = make_kgid(ns, egid);
  579. ksgid = make_kgid(ns, sgid);
  580. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  581. return -EINVAL;
  582. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  583. return -EINVAL;
  584. if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
  585. return -EINVAL;
  586. new = prepare_creds();
  587. if (!new)
  588. return -ENOMEM;
  589. old = current_cred();
  590. retval = -EPERM;
  591. if (!ns_capable(old->user_ns, CAP_SETGID)) {
  592. if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
  593. !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
  594. goto error;
  595. if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
  596. !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
  597. goto error;
  598. if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
  599. !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
  600. goto error;
  601. }
  602. if (rgid != (gid_t) -1)
  603. new->gid = krgid;
  604. if (egid != (gid_t) -1)
  605. new->egid = kegid;
  606. if (sgid != (gid_t) -1)
  607. new->sgid = ksgid;
  608. new->fsgid = new->egid;
  609. return commit_creds(new);
  610. error:
  611. abort_creds(new);
  612. return retval;
  613. }
  614. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
  615. {
  616. const struct cred *cred = current_cred();
  617. int retval;
  618. gid_t rgid, egid, sgid;
  619. rgid = from_kgid_munged(cred->user_ns, cred->gid);
  620. egid = from_kgid_munged(cred->user_ns, cred->egid);
  621. sgid = from_kgid_munged(cred->user_ns, cred->sgid);
  622. if (!(retval = put_user(rgid, rgidp)) &&
  623. !(retval = put_user(egid, egidp)))
  624. retval = put_user(sgid, sgidp);
  625. return retval;
  626. }
  627. /*
  628. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  629. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  630. * whatever uid it wants to). It normally shadows "euid", except when
  631. * explicitly set by setfsuid() or for access..
  632. */
  633. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  634. {
  635. const struct cred *old;
  636. struct cred *new;
  637. uid_t old_fsuid;
  638. kuid_t kuid;
  639. old = current_cred();
  640. old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
  641. kuid = make_kuid(old->user_ns, uid);
  642. if (!uid_valid(kuid))
  643. return old_fsuid;
  644. new = prepare_creds();
  645. if (!new)
  646. return old_fsuid;
  647. if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
  648. uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
  649. ns_capable(old->user_ns, CAP_SETUID)) {
  650. if (!uid_eq(kuid, old->fsuid)) {
  651. new->fsuid = kuid;
  652. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  653. goto change_okay;
  654. }
  655. }
  656. abort_creds(new);
  657. return old_fsuid;
  658. change_okay:
  659. commit_creds(new);
  660. return old_fsuid;
  661. }
  662. /*
  663. * Samma på svenska..
  664. */
  665. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  666. {
  667. const struct cred *old;
  668. struct cred *new;
  669. gid_t old_fsgid;
  670. kgid_t kgid;
  671. old = current_cred();
  672. old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
  673. kgid = make_kgid(old->user_ns, gid);
  674. if (!gid_valid(kgid))
  675. return old_fsgid;
  676. new = prepare_creds();
  677. if (!new)
  678. return old_fsgid;
  679. if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
  680. gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
  681. ns_capable(old->user_ns, CAP_SETGID)) {
  682. if (!gid_eq(kgid, old->fsgid)) {
  683. new->fsgid = kgid;
  684. goto change_okay;
  685. }
  686. }
  687. abort_creds(new);
  688. return old_fsgid;
  689. change_okay:
  690. commit_creds(new);
  691. return old_fsgid;
  692. }
  693. /**
  694. * sys_getpid - return the thread group id of the current process
  695. *
  696. * Note, despite the name, this returns the tgid not the pid. The tgid and
  697. * the pid are identical unless CLONE_THREAD was specified on clone() in
  698. * which case the tgid is the same in all threads of the same group.
  699. *
  700. * This is SMP safe as current->tgid does not change.
  701. */
  702. SYSCALL_DEFINE0(getpid)
  703. {
  704. return task_tgid_vnr(current);
  705. }
  706. /* Thread ID - the internal kernel "pid" */
  707. SYSCALL_DEFINE0(gettid)
  708. {
  709. return task_pid_vnr(current);
  710. }
  711. /*
  712. * Accessing ->real_parent is not SMP-safe, it could
  713. * change from under us. However, we can use a stale
  714. * value of ->real_parent under rcu_read_lock(), see
  715. * release_task()->call_rcu(delayed_put_task_struct).
  716. */
  717. SYSCALL_DEFINE0(getppid)
  718. {
  719. int pid;
  720. rcu_read_lock();
  721. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  722. rcu_read_unlock();
  723. return pid;
  724. }
  725. SYSCALL_DEFINE0(getuid)
  726. {
  727. /* Only we change this so SMP safe */
  728. return from_kuid_munged(current_user_ns(), current_uid());
  729. }
  730. SYSCALL_DEFINE0(geteuid)
  731. {
  732. /* Only we change this so SMP safe */
  733. return from_kuid_munged(current_user_ns(), current_euid());
  734. }
  735. SYSCALL_DEFINE0(getgid)
  736. {
  737. /* Only we change this so SMP safe */
  738. return from_kgid_munged(current_user_ns(), current_gid());
  739. }
  740. SYSCALL_DEFINE0(getegid)
  741. {
  742. /* Only we change this so SMP safe */
  743. return from_kgid_munged(current_user_ns(), current_egid());
  744. }
  745. void do_sys_times(struct tms *tms)
  746. {
  747. cputime_t tgutime, tgstime, cutime, cstime;
  748. spin_lock_irq(&current->sighand->siglock);
  749. thread_group_cputime_adjusted(current, &tgutime, &tgstime);
  750. cutime = current->signal->cutime;
  751. cstime = current->signal->cstime;
  752. spin_unlock_irq(&current->sighand->siglock);
  753. tms->tms_utime = cputime_to_clock_t(tgutime);
  754. tms->tms_stime = cputime_to_clock_t(tgstime);
  755. tms->tms_cutime = cputime_to_clock_t(cutime);
  756. tms->tms_cstime = cputime_to_clock_t(cstime);
  757. }
  758. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  759. {
  760. if (tbuf) {
  761. struct tms tmp;
  762. do_sys_times(&tmp);
  763. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  764. return -EFAULT;
  765. }
  766. force_successful_syscall_return();
  767. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  768. }
  769. /*
  770. * This needs some heavy checking ...
  771. * I just haven't the stomach for it. I also don't fully
  772. * understand sessions/pgrp etc. Let somebody who does explain it.
  773. *
  774. * OK, I think I have the protection semantics right.... this is really
  775. * only important on a multi-user system anyway, to make sure one user
  776. * can't send a signal to a process owned by another. -TYT, 12/12/91
  777. *
  778. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  779. * LBT 04.03.94
  780. */
  781. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  782. {
  783. struct task_struct *p;
  784. struct task_struct *group_leader = current->group_leader;
  785. struct pid *pgrp;
  786. int err;
  787. if (!pid)
  788. pid = task_pid_vnr(group_leader);
  789. if (!pgid)
  790. pgid = pid;
  791. if (pgid < 0)
  792. return -EINVAL;
  793. rcu_read_lock();
  794. /* From this point forward we keep holding onto the tasklist lock
  795. * so that our parent does not change from under us. -DaveM
  796. */
  797. write_lock_irq(&tasklist_lock);
  798. err = -ESRCH;
  799. p = find_task_by_vpid(pid);
  800. if (!p)
  801. goto out;
  802. err = -EINVAL;
  803. if (!thread_group_leader(p))
  804. goto out;
  805. if (same_thread_group(p->real_parent, group_leader)) {
  806. err = -EPERM;
  807. if (task_session(p) != task_session(group_leader))
  808. goto out;
  809. err = -EACCES;
  810. if (p->did_exec)
  811. goto out;
  812. } else {
  813. err = -ESRCH;
  814. if (p != group_leader)
  815. goto out;
  816. }
  817. err = -EPERM;
  818. if (p->signal->leader)
  819. goto out;
  820. pgrp = task_pid(p);
  821. if (pgid != pid) {
  822. struct task_struct *g;
  823. pgrp = find_vpid(pgid);
  824. g = pid_task(pgrp, PIDTYPE_PGID);
  825. if (!g || task_session(g) != task_session(group_leader))
  826. goto out;
  827. }
  828. err = security_task_setpgid(p, pgid);
  829. if (err)
  830. goto out;
  831. if (task_pgrp(p) != pgrp)
  832. change_pid(p, PIDTYPE_PGID, pgrp);
  833. err = 0;
  834. out:
  835. /* All paths lead to here, thus we are safe. -DaveM */
  836. write_unlock_irq(&tasklist_lock);
  837. rcu_read_unlock();
  838. return err;
  839. }
  840. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  841. {
  842. struct task_struct *p;
  843. struct pid *grp;
  844. int retval;
  845. rcu_read_lock();
  846. if (!pid)
  847. grp = task_pgrp(current);
  848. else {
  849. retval = -ESRCH;
  850. p = find_task_by_vpid(pid);
  851. if (!p)
  852. goto out;
  853. grp = task_pgrp(p);
  854. if (!grp)
  855. goto out;
  856. retval = security_task_getpgid(p);
  857. if (retval)
  858. goto out;
  859. }
  860. retval = pid_vnr(grp);
  861. out:
  862. rcu_read_unlock();
  863. return retval;
  864. }
  865. #ifdef __ARCH_WANT_SYS_GETPGRP
  866. SYSCALL_DEFINE0(getpgrp)
  867. {
  868. return sys_getpgid(0);
  869. }
  870. #endif
  871. SYSCALL_DEFINE1(getsid, pid_t, pid)
  872. {
  873. struct task_struct *p;
  874. struct pid *sid;
  875. int retval;
  876. rcu_read_lock();
  877. if (!pid)
  878. sid = task_session(current);
  879. else {
  880. retval = -ESRCH;
  881. p = find_task_by_vpid(pid);
  882. if (!p)
  883. goto out;
  884. sid = task_session(p);
  885. if (!sid)
  886. goto out;
  887. retval = security_task_getsid(p);
  888. if (retval)
  889. goto out;
  890. }
  891. retval = pid_vnr(sid);
  892. out:
  893. rcu_read_unlock();
  894. return retval;
  895. }
  896. static void set_special_pids(struct pid *pid)
  897. {
  898. struct task_struct *curr = current->group_leader;
  899. if (task_session(curr) != pid)
  900. change_pid(curr, PIDTYPE_SID, pid);
  901. if (task_pgrp(curr) != pid)
  902. change_pid(curr, PIDTYPE_PGID, pid);
  903. }
  904. SYSCALL_DEFINE0(setsid)
  905. {
  906. struct task_struct *group_leader = current->group_leader;
  907. struct pid *sid = task_pid(group_leader);
  908. pid_t session = pid_vnr(sid);
  909. int err = -EPERM;
  910. write_lock_irq(&tasklist_lock);
  911. /* Fail if I am already a session leader */
  912. if (group_leader->signal->leader)
  913. goto out;
  914. /* Fail if a process group id already exists that equals the
  915. * proposed session id.
  916. */
  917. if (pid_task(sid, PIDTYPE_PGID))
  918. goto out;
  919. group_leader->signal->leader = 1;
  920. set_special_pids(sid);
  921. proc_clear_tty(group_leader);
  922. err = session;
  923. out:
  924. write_unlock_irq(&tasklist_lock);
  925. if (err > 0) {
  926. proc_sid_connector(group_leader);
  927. sched_autogroup_create_attach(group_leader);
  928. }
  929. return err;
  930. }
  931. DECLARE_RWSEM(uts_sem);
  932. #ifdef COMPAT_UTS_MACHINE
  933. #define override_architecture(name) \
  934. (personality(current->personality) == PER_LINUX32 && \
  935. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  936. sizeof(COMPAT_UTS_MACHINE)))
  937. #else
  938. #define override_architecture(name) 0
  939. #endif
  940. /*
  941. * Work around broken programs that cannot handle "Linux 3.0".
  942. * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
  943. */
  944. static int override_release(char __user *release, size_t len)
  945. {
  946. int ret = 0;
  947. if (current->personality & UNAME26) {
  948. const char *rest = UTS_RELEASE;
  949. char buf[65] = { 0 };
  950. int ndots = 0;
  951. unsigned v;
  952. size_t copy;
  953. while (*rest) {
  954. if (*rest == '.' && ++ndots >= 3)
  955. break;
  956. if (!isdigit(*rest) && *rest != '.')
  957. break;
  958. rest++;
  959. }
  960. v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
  961. copy = clamp_t(size_t, len, 1, sizeof(buf));
  962. copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
  963. ret = copy_to_user(release, buf, copy + 1);
  964. }
  965. return ret;
  966. }
  967. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  968. {
  969. int errno = 0;
  970. down_read(&uts_sem);
  971. if (copy_to_user(name, utsname(), sizeof *name))
  972. errno = -EFAULT;
  973. up_read(&uts_sem);
  974. if (!errno && override_release(name->release, sizeof(name->release)))
  975. errno = -EFAULT;
  976. if (!errno && override_architecture(name))
  977. errno = -EFAULT;
  978. return errno;
  979. }
  980. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  981. /*
  982. * Old cruft
  983. */
  984. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  985. {
  986. int error = 0;
  987. if (!name)
  988. return -EFAULT;
  989. down_read(&uts_sem);
  990. if (copy_to_user(name, utsname(), sizeof(*name)))
  991. error = -EFAULT;
  992. up_read(&uts_sem);
  993. if (!error && override_release(name->release, sizeof(name->release)))
  994. error = -EFAULT;
  995. if (!error && override_architecture(name))
  996. error = -EFAULT;
  997. return error;
  998. }
  999. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1000. {
  1001. int error;
  1002. if (!name)
  1003. return -EFAULT;
  1004. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1005. return -EFAULT;
  1006. down_read(&uts_sem);
  1007. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1008. __OLD_UTS_LEN);
  1009. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1010. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1011. __OLD_UTS_LEN);
  1012. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1013. error |= __copy_to_user(&name->release, &utsname()->release,
  1014. __OLD_UTS_LEN);
  1015. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1016. error |= __copy_to_user(&name->version, &utsname()->version,
  1017. __OLD_UTS_LEN);
  1018. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1019. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1020. __OLD_UTS_LEN);
  1021. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1022. up_read(&uts_sem);
  1023. if (!error && override_architecture(name))
  1024. error = -EFAULT;
  1025. if (!error && override_release(name->release, sizeof(name->release)))
  1026. error = -EFAULT;
  1027. return error ? -EFAULT : 0;
  1028. }
  1029. #endif
  1030. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1031. {
  1032. int errno;
  1033. char tmp[__NEW_UTS_LEN];
  1034. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1035. return -EPERM;
  1036. if (len < 0 || len > __NEW_UTS_LEN)
  1037. return -EINVAL;
  1038. down_write(&uts_sem);
  1039. errno = -EFAULT;
  1040. if (!copy_from_user(tmp, name, len)) {
  1041. struct new_utsname *u = utsname();
  1042. memcpy(u->nodename, tmp, len);
  1043. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1044. errno = 0;
  1045. uts_proc_notify(UTS_PROC_HOSTNAME);
  1046. }
  1047. up_write(&uts_sem);
  1048. return errno;
  1049. }
  1050. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1051. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1052. {
  1053. int i, errno;
  1054. struct new_utsname *u;
  1055. if (len < 0)
  1056. return -EINVAL;
  1057. down_read(&uts_sem);
  1058. u = utsname();
  1059. i = 1 + strlen(u->nodename);
  1060. if (i > len)
  1061. i = len;
  1062. errno = 0;
  1063. if (copy_to_user(name, u->nodename, i))
  1064. errno = -EFAULT;
  1065. up_read(&uts_sem);
  1066. return errno;
  1067. }
  1068. #endif
  1069. /*
  1070. * Only setdomainname; getdomainname can be implemented by calling
  1071. * uname()
  1072. */
  1073. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1074. {
  1075. int errno;
  1076. char tmp[__NEW_UTS_LEN];
  1077. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1078. return -EPERM;
  1079. if (len < 0 || len > __NEW_UTS_LEN)
  1080. return -EINVAL;
  1081. down_write(&uts_sem);
  1082. errno = -EFAULT;
  1083. if (!copy_from_user(tmp, name, len)) {
  1084. struct new_utsname *u = utsname();
  1085. memcpy(u->domainname, tmp, len);
  1086. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1087. errno = 0;
  1088. uts_proc_notify(UTS_PROC_DOMAINNAME);
  1089. }
  1090. up_write(&uts_sem);
  1091. return errno;
  1092. }
  1093. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1094. {
  1095. struct rlimit value;
  1096. int ret;
  1097. ret = do_prlimit(current, resource, NULL, &value);
  1098. if (!ret)
  1099. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1100. return ret;
  1101. }
  1102. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1103. /*
  1104. * Back compatibility for getrlimit. Needed for some apps.
  1105. */
  1106. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1107. struct rlimit __user *, rlim)
  1108. {
  1109. struct rlimit x;
  1110. if (resource >= RLIM_NLIMITS)
  1111. return -EINVAL;
  1112. task_lock(current->group_leader);
  1113. x = current->signal->rlim[resource];
  1114. task_unlock(current->group_leader);
  1115. if (x.rlim_cur > 0x7FFFFFFF)
  1116. x.rlim_cur = 0x7FFFFFFF;
  1117. if (x.rlim_max > 0x7FFFFFFF)
  1118. x.rlim_max = 0x7FFFFFFF;
  1119. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1120. }
  1121. #endif
  1122. static inline bool rlim64_is_infinity(__u64 rlim64)
  1123. {
  1124. #if BITS_PER_LONG < 64
  1125. return rlim64 >= ULONG_MAX;
  1126. #else
  1127. return rlim64 == RLIM64_INFINITY;
  1128. #endif
  1129. }
  1130. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1131. {
  1132. if (rlim->rlim_cur == RLIM_INFINITY)
  1133. rlim64->rlim_cur = RLIM64_INFINITY;
  1134. else
  1135. rlim64->rlim_cur = rlim->rlim_cur;
  1136. if (rlim->rlim_max == RLIM_INFINITY)
  1137. rlim64->rlim_max = RLIM64_INFINITY;
  1138. else
  1139. rlim64->rlim_max = rlim->rlim_max;
  1140. }
  1141. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1142. {
  1143. if (rlim64_is_infinity(rlim64->rlim_cur))
  1144. rlim->rlim_cur = RLIM_INFINITY;
  1145. else
  1146. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1147. if (rlim64_is_infinity(rlim64->rlim_max))
  1148. rlim->rlim_max = RLIM_INFINITY;
  1149. else
  1150. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1151. }
  1152. /* make sure you are allowed to change @tsk limits before calling this */
  1153. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1154. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1155. {
  1156. struct rlimit *rlim;
  1157. int retval = 0;
  1158. if (resource >= RLIM_NLIMITS)
  1159. return -EINVAL;
  1160. if (new_rlim) {
  1161. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1162. return -EINVAL;
  1163. if (resource == RLIMIT_NOFILE &&
  1164. new_rlim->rlim_max > sysctl_nr_open)
  1165. return -EPERM;
  1166. }
  1167. /* protect tsk->signal and tsk->sighand from disappearing */
  1168. read_lock(&tasklist_lock);
  1169. if (!tsk->sighand) {
  1170. retval = -ESRCH;
  1171. goto out;
  1172. }
  1173. rlim = tsk->signal->rlim + resource;
  1174. task_lock(tsk->group_leader);
  1175. if (new_rlim) {
  1176. /* Keep the capable check against init_user_ns until
  1177. cgroups can contain all limits */
  1178. if (new_rlim->rlim_max > rlim->rlim_max &&
  1179. !capable(CAP_SYS_RESOURCE))
  1180. retval = -EPERM;
  1181. if (!retval)
  1182. retval = security_task_setrlimit(tsk->group_leader,
  1183. resource, new_rlim);
  1184. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1185. /*
  1186. * The caller is asking for an immediate RLIMIT_CPU
  1187. * expiry. But we use the zero value to mean "it was
  1188. * never set". So let's cheat and make it one second
  1189. * instead
  1190. */
  1191. new_rlim->rlim_cur = 1;
  1192. }
  1193. }
  1194. if (!retval) {
  1195. if (old_rlim)
  1196. *old_rlim = *rlim;
  1197. if (new_rlim)
  1198. *rlim = *new_rlim;
  1199. }
  1200. task_unlock(tsk->group_leader);
  1201. /*
  1202. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1203. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1204. * very long-standing error, and fixing it now risks breakage of
  1205. * applications, so we live with it
  1206. */
  1207. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1208. new_rlim->rlim_cur != RLIM_INFINITY)
  1209. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1210. out:
  1211. read_unlock(&tasklist_lock);
  1212. return retval;
  1213. }
  1214. /* rcu lock must be held */
  1215. static int check_prlimit_permission(struct task_struct *task)
  1216. {
  1217. const struct cred *cred = current_cred(), *tcred;
  1218. if (current == task)
  1219. return 0;
  1220. tcred = __task_cred(task);
  1221. if (uid_eq(cred->uid, tcred->euid) &&
  1222. uid_eq(cred->uid, tcred->suid) &&
  1223. uid_eq(cred->uid, tcred->uid) &&
  1224. gid_eq(cred->gid, tcred->egid) &&
  1225. gid_eq(cred->gid, tcred->sgid) &&
  1226. gid_eq(cred->gid, tcred->gid))
  1227. return 0;
  1228. if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
  1229. return 0;
  1230. return -EPERM;
  1231. }
  1232. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1233. const struct rlimit64 __user *, new_rlim,
  1234. struct rlimit64 __user *, old_rlim)
  1235. {
  1236. struct rlimit64 old64, new64;
  1237. struct rlimit old, new;
  1238. struct task_struct *tsk;
  1239. int ret;
  1240. if (new_rlim) {
  1241. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1242. return -EFAULT;
  1243. rlim64_to_rlim(&new64, &new);
  1244. }
  1245. rcu_read_lock();
  1246. tsk = pid ? find_task_by_vpid(pid) : current;
  1247. if (!tsk) {
  1248. rcu_read_unlock();
  1249. return -ESRCH;
  1250. }
  1251. ret = check_prlimit_permission(tsk);
  1252. if (ret) {
  1253. rcu_read_unlock();
  1254. return ret;
  1255. }
  1256. get_task_struct(tsk);
  1257. rcu_read_unlock();
  1258. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1259. old_rlim ? &old : NULL);
  1260. if (!ret && old_rlim) {
  1261. rlim_to_rlim64(&old, &old64);
  1262. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1263. ret = -EFAULT;
  1264. }
  1265. put_task_struct(tsk);
  1266. return ret;
  1267. }
  1268. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1269. {
  1270. struct rlimit new_rlim;
  1271. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1272. return -EFAULT;
  1273. return do_prlimit(current, resource, &new_rlim, NULL);
  1274. }
  1275. /*
  1276. * It would make sense to put struct rusage in the task_struct,
  1277. * except that would make the task_struct be *really big*. After
  1278. * task_struct gets moved into malloc'ed memory, it would
  1279. * make sense to do this. It will make moving the rest of the information
  1280. * a lot simpler! (Which we're not doing right now because we're not
  1281. * measuring them yet).
  1282. *
  1283. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1284. * races with threads incrementing their own counters. But since word
  1285. * reads are atomic, we either get new values or old values and we don't
  1286. * care which for the sums. We always take the siglock to protect reading
  1287. * the c* fields from p->signal from races with exit.c updating those
  1288. * fields when reaping, so a sample either gets all the additions of a
  1289. * given child after it's reaped, or none so this sample is before reaping.
  1290. *
  1291. * Locking:
  1292. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1293. * for the cases current multithreaded, non-current single threaded
  1294. * non-current multithreaded. Thread traversal is now safe with
  1295. * the siglock held.
  1296. * Strictly speaking, we donot need to take the siglock if we are current and
  1297. * single threaded, as no one else can take our signal_struct away, no one
  1298. * else can reap the children to update signal->c* counters, and no one else
  1299. * can race with the signal-> fields. If we do not take any lock, the
  1300. * signal-> fields could be read out of order while another thread was just
  1301. * exiting. So we should place a read memory barrier when we avoid the lock.
  1302. * On the writer side, write memory barrier is implied in __exit_signal
  1303. * as __exit_signal releases the siglock spinlock after updating the signal->
  1304. * fields. But we don't do this yet to keep things simple.
  1305. *
  1306. */
  1307. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1308. {
  1309. r->ru_nvcsw += t->nvcsw;
  1310. r->ru_nivcsw += t->nivcsw;
  1311. r->ru_minflt += t->min_flt;
  1312. r->ru_majflt += t->maj_flt;
  1313. r->ru_inblock += task_io_get_inblock(t);
  1314. r->ru_oublock += task_io_get_oublock(t);
  1315. }
  1316. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1317. {
  1318. struct task_struct *t;
  1319. unsigned long flags;
  1320. cputime_t tgutime, tgstime, utime, stime;
  1321. unsigned long maxrss = 0;
  1322. memset((char *) r, 0, sizeof *r);
  1323. utime = stime = 0;
  1324. if (who == RUSAGE_THREAD) {
  1325. task_cputime_adjusted(current, &utime, &stime);
  1326. accumulate_thread_rusage(p, r);
  1327. maxrss = p->signal->maxrss;
  1328. goto out;
  1329. }
  1330. if (!lock_task_sighand(p, &flags))
  1331. return;
  1332. switch (who) {
  1333. case RUSAGE_BOTH:
  1334. case RUSAGE_CHILDREN:
  1335. utime = p->signal->cutime;
  1336. stime = p->signal->cstime;
  1337. r->ru_nvcsw = p->signal->cnvcsw;
  1338. r->ru_nivcsw = p->signal->cnivcsw;
  1339. r->ru_minflt = p->signal->cmin_flt;
  1340. r->ru_majflt = p->signal->cmaj_flt;
  1341. r->ru_inblock = p->signal->cinblock;
  1342. r->ru_oublock = p->signal->coublock;
  1343. maxrss = p->signal->cmaxrss;
  1344. if (who == RUSAGE_CHILDREN)
  1345. break;
  1346. case RUSAGE_SELF:
  1347. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1348. utime += tgutime;
  1349. stime += tgstime;
  1350. r->ru_nvcsw += p->signal->nvcsw;
  1351. r->ru_nivcsw += p->signal->nivcsw;
  1352. r->ru_minflt += p->signal->min_flt;
  1353. r->ru_majflt += p->signal->maj_flt;
  1354. r->ru_inblock += p->signal->inblock;
  1355. r->ru_oublock += p->signal->oublock;
  1356. if (maxrss < p->signal->maxrss)
  1357. maxrss = p->signal->maxrss;
  1358. t = p;
  1359. do {
  1360. accumulate_thread_rusage(t, r);
  1361. t = next_thread(t);
  1362. } while (t != p);
  1363. break;
  1364. default:
  1365. BUG();
  1366. }
  1367. unlock_task_sighand(p, &flags);
  1368. out:
  1369. cputime_to_timeval(utime, &r->ru_utime);
  1370. cputime_to_timeval(stime, &r->ru_stime);
  1371. if (who != RUSAGE_CHILDREN) {
  1372. struct mm_struct *mm = get_task_mm(p);
  1373. if (mm) {
  1374. setmax_mm_hiwater_rss(&maxrss, mm);
  1375. mmput(mm);
  1376. }
  1377. }
  1378. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1379. }
  1380. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1381. {
  1382. struct rusage r;
  1383. k_getrusage(p, who, &r);
  1384. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1385. }
  1386. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1387. {
  1388. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1389. who != RUSAGE_THREAD)
  1390. return -EINVAL;
  1391. return getrusage(current, who, ru);
  1392. }
  1393. #ifdef CONFIG_COMPAT
  1394. COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
  1395. {
  1396. struct rusage r;
  1397. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1398. who != RUSAGE_THREAD)
  1399. return -EINVAL;
  1400. k_getrusage(current, who, &r);
  1401. return put_compat_rusage(&r, ru);
  1402. }
  1403. #endif
  1404. SYSCALL_DEFINE1(umask, int, mask)
  1405. {
  1406. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1407. return mask;
  1408. }
  1409. static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
  1410. {
  1411. struct fd exe;
  1412. struct inode *inode;
  1413. int err;
  1414. exe = fdget(fd);
  1415. if (!exe.file)
  1416. return -EBADF;
  1417. inode = file_inode(exe.file);
  1418. /*
  1419. * Because the original mm->exe_file points to executable file, make
  1420. * sure that this one is executable as well, to avoid breaking an
  1421. * overall picture.
  1422. */
  1423. err = -EACCES;
  1424. if (!S_ISREG(inode->i_mode) ||
  1425. exe.file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  1426. goto exit;
  1427. err = inode_permission(inode, MAY_EXEC);
  1428. if (err)
  1429. goto exit;
  1430. down_write(&mm->mmap_sem);
  1431. /*
  1432. * Forbid mm->exe_file change if old file still mapped.
  1433. */
  1434. err = -EBUSY;
  1435. if (mm->exe_file) {
  1436. struct vm_area_struct *vma;
  1437. for (vma = mm->mmap; vma; vma = vma->vm_next)
  1438. if (vma->vm_file &&
  1439. path_equal(&vma->vm_file->f_path,
  1440. &mm->exe_file->f_path))
  1441. goto exit_unlock;
  1442. }
  1443. /*
  1444. * The symlink can be changed only once, just to disallow arbitrary
  1445. * transitions malicious software might bring in. This means one
  1446. * could make a snapshot over all processes running and monitor
  1447. * /proc/pid/exe changes to notice unusual activity if needed.
  1448. */
  1449. err = -EPERM;
  1450. if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
  1451. goto exit_unlock;
  1452. err = 0;
  1453. set_mm_exe_file(mm, exe.file); /* this grabs a reference to exe.file */
  1454. exit_unlock:
  1455. up_write(&mm->mmap_sem);
  1456. exit:
  1457. fdput(exe);
  1458. return err;
  1459. }
  1460. static int prctl_set_mm(int opt, unsigned long addr,
  1461. unsigned long arg4, unsigned long arg5)
  1462. {
  1463. unsigned long rlim = rlimit(RLIMIT_DATA);
  1464. struct mm_struct *mm = current->mm;
  1465. struct vm_area_struct *vma;
  1466. int error;
  1467. if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))
  1468. return -EINVAL;
  1469. if (!capable(CAP_SYS_RESOURCE))
  1470. return -EPERM;
  1471. if (opt == PR_SET_MM_EXE_FILE)
  1472. return prctl_set_mm_exe_file(mm, (unsigned int)addr);
  1473. if (addr >= TASK_SIZE || addr < mmap_min_addr)
  1474. return -EINVAL;
  1475. error = -EINVAL;
  1476. down_read(&mm->mmap_sem);
  1477. vma = find_vma(mm, addr);
  1478. switch (opt) {
  1479. case PR_SET_MM_START_CODE:
  1480. mm->start_code = addr;
  1481. break;
  1482. case PR_SET_MM_END_CODE:
  1483. mm->end_code = addr;
  1484. break;
  1485. case PR_SET_MM_START_DATA:
  1486. mm->start_data = addr;
  1487. break;
  1488. case PR_SET_MM_END_DATA:
  1489. mm->end_data = addr;
  1490. break;
  1491. case PR_SET_MM_START_BRK:
  1492. if (addr <= mm->end_data)
  1493. goto out;
  1494. if (rlim < RLIM_INFINITY &&
  1495. (mm->brk - addr) +
  1496. (mm->end_data - mm->start_data) > rlim)
  1497. goto out;
  1498. mm->start_brk = addr;
  1499. break;
  1500. case PR_SET_MM_BRK:
  1501. if (addr <= mm->end_data)
  1502. goto out;
  1503. if (rlim < RLIM_INFINITY &&
  1504. (addr - mm->start_brk) +
  1505. (mm->end_data - mm->start_data) > rlim)
  1506. goto out;
  1507. mm->brk = addr;
  1508. break;
  1509. /*
  1510. * If command line arguments and environment
  1511. * are placed somewhere else on stack, we can
  1512. * set them up here, ARG_START/END to setup
  1513. * command line argumets and ENV_START/END
  1514. * for environment.
  1515. */
  1516. case PR_SET_MM_START_STACK:
  1517. case PR_SET_MM_ARG_START:
  1518. case PR_SET_MM_ARG_END:
  1519. case PR_SET_MM_ENV_START:
  1520. case PR_SET_MM_ENV_END:
  1521. if (!vma) {
  1522. error = -EFAULT;
  1523. goto out;
  1524. }
  1525. if (opt == PR_SET_MM_START_STACK)
  1526. mm->start_stack = addr;
  1527. else if (opt == PR_SET_MM_ARG_START)
  1528. mm->arg_start = addr;
  1529. else if (opt == PR_SET_MM_ARG_END)
  1530. mm->arg_end = addr;
  1531. else if (opt == PR_SET_MM_ENV_START)
  1532. mm->env_start = addr;
  1533. else if (opt == PR_SET_MM_ENV_END)
  1534. mm->env_end = addr;
  1535. break;
  1536. /*
  1537. * This doesn't move auxiliary vector itself
  1538. * since it's pinned to mm_struct, but allow
  1539. * to fill vector with new values. It's up
  1540. * to a caller to provide sane values here
  1541. * otherwise user space tools which use this
  1542. * vector might be unhappy.
  1543. */
  1544. case PR_SET_MM_AUXV: {
  1545. unsigned long user_auxv[AT_VECTOR_SIZE];
  1546. if (arg4 > sizeof(user_auxv))
  1547. goto out;
  1548. up_read(&mm->mmap_sem);
  1549. if (copy_from_user(user_auxv, (const void __user *)addr, arg4))
  1550. return -EFAULT;
  1551. /* Make sure the last entry is always AT_NULL */
  1552. user_auxv[AT_VECTOR_SIZE - 2] = 0;
  1553. user_auxv[AT_VECTOR_SIZE - 1] = 0;
  1554. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1555. task_lock(current);
  1556. memcpy(mm->saved_auxv, user_auxv, arg4);
  1557. task_unlock(current);
  1558. return 0;
  1559. }
  1560. default:
  1561. goto out;
  1562. }
  1563. error = 0;
  1564. out:
  1565. up_read(&mm->mmap_sem);
  1566. return error;
  1567. }
  1568. #ifdef CONFIG_CHECKPOINT_RESTORE
  1569. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1570. {
  1571. return put_user(me->clear_child_tid, tid_addr);
  1572. }
  1573. #else
  1574. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1575. {
  1576. return -EINVAL;
  1577. }
  1578. #endif
  1579. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1580. unsigned long, arg4, unsigned long, arg5)
  1581. {
  1582. struct task_struct *me = current;
  1583. unsigned char comm[sizeof(me->comm)];
  1584. long error;
  1585. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1586. if (error != -ENOSYS)
  1587. return error;
  1588. error = 0;
  1589. switch (option) {
  1590. case PR_SET_PDEATHSIG:
  1591. if (!valid_signal(arg2)) {
  1592. error = -EINVAL;
  1593. break;
  1594. }
  1595. me->pdeath_signal = arg2;
  1596. break;
  1597. case PR_GET_PDEATHSIG:
  1598. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1599. break;
  1600. case PR_GET_DUMPABLE:
  1601. error = get_dumpable(me->mm);
  1602. break;
  1603. case PR_SET_DUMPABLE:
  1604. if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
  1605. error = -EINVAL;
  1606. break;
  1607. }
  1608. set_dumpable(me->mm, arg2);
  1609. break;
  1610. case PR_SET_UNALIGN:
  1611. error = SET_UNALIGN_CTL(me, arg2);
  1612. break;
  1613. case PR_GET_UNALIGN:
  1614. error = GET_UNALIGN_CTL(me, arg2);
  1615. break;
  1616. case PR_SET_FPEMU:
  1617. error = SET_FPEMU_CTL(me, arg2);
  1618. break;
  1619. case PR_GET_FPEMU:
  1620. error = GET_FPEMU_CTL(me, arg2);
  1621. break;
  1622. case PR_SET_FPEXC:
  1623. error = SET_FPEXC_CTL(me, arg2);
  1624. break;
  1625. case PR_GET_FPEXC:
  1626. error = GET_FPEXC_CTL(me, arg2);
  1627. break;
  1628. case PR_GET_TIMING:
  1629. error = PR_TIMING_STATISTICAL;
  1630. break;
  1631. case PR_SET_TIMING:
  1632. if (arg2 != PR_TIMING_STATISTICAL)
  1633. error = -EINVAL;
  1634. break;
  1635. case PR_SET_NAME:
  1636. comm[sizeof(me->comm) - 1] = 0;
  1637. if (strncpy_from_user(comm, (char __user *)arg2,
  1638. sizeof(me->comm) - 1) < 0)
  1639. return -EFAULT;
  1640. set_task_comm(me, comm);
  1641. proc_comm_connector(me);
  1642. break;
  1643. case PR_GET_NAME:
  1644. get_task_comm(comm, me);
  1645. if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
  1646. return -EFAULT;
  1647. break;
  1648. case PR_GET_ENDIAN:
  1649. error = GET_ENDIAN(me, arg2);
  1650. break;
  1651. case PR_SET_ENDIAN:
  1652. error = SET_ENDIAN(me, arg2);
  1653. break;
  1654. case PR_GET_SECCOMP:
  1655. error = prctl_get_seccomp();
  1656. break;
  1657. case PR_SET_SECCOMP:
  1658. error = prctl_set_seccomp(arg2, (char __user *)arg3);
  1659. break;
  1660. case PR_GET_TSC:
  1661. error = GET_TSC_CTL(arg2);
  1662. break;
  1663. case PR_SET_TSC:
  1664. error = SET_TSC_CTL(arg2);
  1665. break;
  1666. case PR_TASK_PERF_EVENTS_DISABLE:
  1667. error = perf_event_task_disable();
  1668. break;
  1669. case PR_TASK_PERF_EVENTS_ENABLE:
  1670. error = perf_event_task_enable();
  1671. break;
  1672. case PR_GET_TIMERSLACK:
  1673. error = current->timer_slack_ns;
  1674. break;
  1675. case PR_SET_TIMERSLACK:
  1676. if (arg2 <= 0)
  1677. current->timer_slack_ns =
  1678. current->default_timer_slack_ns;
  1679. else
  1680. current->timer_slack_ns = arg2;
  1681. break;
  1682. case PR_MCE_KILL:
  1683. if (arg4 | arg5)
  1684. return -EINVAL;
  1685. switch (arg2) {
  1686. case PR_MCE_KILL_CLEAR:
  1687. if (arg3 != 0)
  1688. return -EINVAL;
  1689. current->flags &= ~PF_MCE_PROCESS;
  1690. break;
  1691. case PR_MCE_KILL_SET:
  1692. current->flags |= PF_MCE_PROCESS;
  1693. if (arg3 == PR_MCE_KILL_EARLY)
  1694. current->flags |= PF_MCE_EARLY;
  1695. else if (arg3 == PR_MCE_KILL_LATE)
  1696. current->flags &= ~PF_MCE_EARLY;
  1697. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1698. current->flags &=
  1699. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1700. else
  1701. return -EINVAL;
  1702. break;
  1703. default:
  1704. return -EINVAL;
  1705. }
  1706. break;
  1707. case PR_MCE_KILL_GET:
  1708. if (arg2 | arg3 | arg4 | arg5)
  1709. return -EINVAL;
  1710. if (current->flags & PF_MCE_PROCESS)
  1711. error = (current->flags & PF_MCE_EARLY) ?
  1712. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1713. else
  1714. error = PR_MCE_KILL_DEFAULT;
  1715. break;
  1716. case PR_SET_MM:
  1717. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  1718. break;
  1719. case PR_GET_TID_ADDRESS:
  1720. error = prctl_get_tid_address(me, (int __user **)arg2);
  1721. break;
  1722. case PR_SET_CHILD_SUBREAPER:
  1723. me->signal->is_child_subreaper = !!arg2;
  1724. break;
  1725. case PR_GET_CHILD_SUBREAPER:
  1726. error = put_user(me->signal->is_child_subreaper,
  1727. (int __user *)arg2);
  1728. break;
  1729. case PR_SET_NO_NEW_PRIVS:
  1730. if (arg2 != 1 || arg3 || arg4 || arg5)
  1731. return -EINVAL;
  1732. current->no_new_privs = 1;
  1733. break;
  1734. case PR_GET_NO_NEW_PRIVS:
  1735. if (arg2 || arg3 || arg4 || arg5)
  1736. return -EINVAL;
  1737. return current->no_new_privs ? 1 : 0;
  1738. default:
  1739. error = -EINVAL;
  1740. break;
  1741. }
  1742. return error;
  1743. }
  1744. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1745. struct getcpu_cache __user *, unused)
  1746. {
  1747. int err = 0;
  1748. int cpu = raw_smp_processor_id();
  1749. if (cpup)
  1750. err |= put_user(cpu, cpup);
  1751. if (nodep)
  1752. err |= put_user(cpu_to_node(cpu), nodep);
  1753. return err ? -EFAULT : 0;
  1754. }
  1755. /**
  1756. * do_sysinfo - fill in sysinfo struct
  1757. * @info: pointer to buffer to fill
  1758. */
  1759. static int do_sysinfo(struct sysinfo *info)
  1760. {
  1761. unsigned long mem_total, sav_total;
  1762. unsigned int mem_unit, bitcount;
  1763. struct timespec tp;
  1764. memset(info, 0, sizeof(struct sysinfo));
  1765. get_monotonic_boottime(&tp);
  1766. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1767. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  1768. info->procs = nr_threads;
  1769. si_meminfo(info);
  1770. si_swapinfo(info);
  1771. /*
  1772. * If the sum of all the available memory (i.e. ram + swap)
  1773. * is less than can be stored in a 32 bit unsigned long then
  1774. * we can be binary compatible with 2.2.x kernels. If not,
  1775. * well, in that case 2.2.x was broken anyways...
  1776. *
  1777. * -Erik Andersen <andersee@debian.org>
  1778. */
  1779. mem_total = info->totalram + info->totalswap;
  1780. if (mem_total < info->totalram || mem_total < info->totalswap)
  1781. goto out;
  1782. bitcount = 0;
  1783. mem_unit = info->mem_unit;
  1784. while (mem_unit > 1) {
  1785. bitcount++;
  1786. mem_unit >>= 1;
  1787. sav_total = mem_total;
  1788. mem_total <<= 1;
  1789. if (mem_total < sav_total)
  1790. goto out;
  1791. }
  1792. /*
  1793. * If mem_total did not overflow, multiply all memory values by
  1794. * info->mem_unit and set it to 1. This leaves things compatible
  1795. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1796. * kernels...
  1797. */
  1798. info->mem_unit = 1;
  1799. info->totalram <<= bitcount;
  1800. info->freeram <<= bitcount;
  1801. info->sharedram <<= bitcount;
  1802. info->bufferram <<= bitcount;
  1803. info->totalswap <<= bitcount;
  1804. info->freeswap <<= bitcount;
  1805. info->totalhigh <<= bitcount;
  1806. info->freehigh <<= bitcount;
  1807. out:
  1808. return 0;
  1809. }
  1810. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  1811. {
  1812. struct sysinfo val;
  1813. do_sysinfo(&val);
  1814. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1815. return -EFAULT;
  1816. return 0;
  1817. }
  1818. #ifdef CONFIG_COMPAT
  1819. struct compat_sysinfo {
  1820. s32 uptime;
  1821. u32 loads[3];
  1822. u32 totalram;
  1823. u32 freeram;
  1824. u32 sharedram;
  1825. u32 bufferram;
  1826. u32 totalswap;
  1827. u32 freeswap;
  1828. u16 procs;
  1829. u16 pad;
  1830. u32 totalhigh;
  1831. u32 freehigh;
  1832. u32 mem_unit;
  1833. char _f[20-2*sizeof(u32)-sizeof(int)];
  1834. };
  1835. COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
  1836. {
  1837. struct sysinfo s;
  1838. do_sysinfo(&s);
  1839. /* Check to see if any memory value is too large for 32-bit and scale
  1840. * down if needed
  1841. */
  1842. if ((s.totalram >> 32) || (s.totalswap >> 32)) {
  1843. int bitcount = 0;
  1844. while (s.mem_unit < PAGE_SIZE) {
  1845. s.mem_unit <<= 1;
  1846. bitcount++;
  1847. }
  1848. s.totalram >>= bitcount;
  1849. s.freeram >>= bitcount;
  1850. s.sharedram >>= bitcount;
  1851. s.bufferram >>= bitcount;
  1852. s.totalswap >>= bitcount;
  1853. s.freeswap >>= bitcount;
  1854. s.totalhigh >>= bitcount;
  1855. s.freehigh >>= bitcount;
  1856. }
  1857. if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
  1858. __put_user(s.uptime, &info->uptime) ||
  1859. __put_user(s.loads[0], &info->loads[0]) ||
  1860. __put_user(s.loads[1], &info->loads[1]) ||
  1861. __put_user(s.loads[2], &info->loads[2]) ||
  1862. __put_user(s.totalram, &info->totalram) ||
  1863. __put_user(s.freeram, &info->freeram) ||
  1864. __put_user(s.sharedram, &info->sharedram) ||
  1865. __put_user(s.bufferram, &info->bufferram) ||
  1866. __put_user(s.totalswap, &info->totalswap) ||
  1867. __put_user(s.freeswap, &info->freeswap) ||
  1868. __put_user(s.procs, &info->procs) ||
  1869. __put_user(s.totalhigh, &info->totalhigh) ||
  1870. __put_user(s.freehigh, &info->freehigh) ||
  1871. __put_user(s.mem_unit, &info->mem_unit))
  1872. return -EFAULT;
  1873. return 0;
  1874. }
  1875. #endif /* CONFIG_COMPAT */