sys.c 69 KB

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