sys.c 58 KB

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