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