sys.c 66 KB

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