sys.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  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. bool need_tasklist;
  1469. int ret;
  1470. if (old_rlim)
  1471. checkflags |= LSM_PRLIMIT_READ;
  1472. if (new_rlim) {
  1473. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1474. return -EFAULT;
  1475. rlim64_to_rlim(&new64, &new);
  1476. checkflags |= LSM_PRLIMIT_WRITE;
  1477. }
  1478. rcu_read_lock();
  1479. tsk = pid ? find_task_by_vpid(pid) : current;
  1480. if (!tsk) {
  1481. rcu_read_unlock();
  1482. return -ESRCH;
  1483. }
  1484. ret = check_prlimit_permission(tsk, checkflags);
  1485. if (ret) {
  1486. rcu_read_unlock();
  1487. return ret;
  1488. }
  1489. get_task_struct(tsk);
  1490. rcu_read_unlock();
  1491. need_tasklist = !same_thread_group(tsk, current);
  1492. if (need_tasklist) {
  1493. /*
  1494. * Ensure we can't race with group exit or de_thread(),
  1495. * so tsk->group_leader can't be freed or changed until
  1496. * read_unlock(tasklist_lock) below.
  1497. */
  1498. read_lock(&tasklist_lock);
  1499. if (!pid_alive(tsk))
  1500. ret = -ESRCH;
  1501. }
  1502. if (!ret) {
  1503. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1504. old_rlim ? &old : NULL);
  1505. }
  1506. if (need_tasklist)
  1507. read_unlock(&tasklist_lock);
  1508. if (!ret && old_rlim) {
  1509. rlim_to_rlim64(&old, &old64);
  1510. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1511. ret = -EFAULT;
  1512. }
  1513. put_task_struct(tsk);
  1514. return ret;
  1515. }
  1516. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1517. {
  1518. struct rlimit new_rlim;
  1519. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1520. return -EFAULT;
  1521. return do_prlimit(current, resource, &new_rlim, NULL);
  1522. }
  1523. /*
  1524. * It would make sense to put struct rusage in the task_struct,
  1525. * except that would make the task_struct be *really big*. After
  1526. * task_struct gets moved into malloc'ed memory, it would
  1527. * make sense to do this. It will make moving the rest of the information
  1528. * a lot simpler! (Which we're not doing right now because we're not
  1529. * measuring them yet).
  1530. *
  1531. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1532. * races with threads incrementing their own counters. But since word
  1533. * reads are atomic, we either get new values or old values and we don't
  1534. * care which for the sums. We always take the siglock to protect reading
  1535. * the c* fields from p->signal from races with exit.c updating those
  1536. * fields when reaping, so a sample either gets all the additions of a
  1537. * given child after it's reaped, or none so this sample is before reaping.
  1538. *
  1539. * Locking:
  1540. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1541. * for the cases current multithreaded, non-current single threaded
  1542. * non-current multithreaded. Thread traversal is now safe with
  1543. * the siglock held.
  1544. * Strictly speaking, we donot need to take the siglock if we are current and
  1545. * single threaded, as no one else can take our signal_struct away, no one
  1546. * else can reap the children to update signal->c* counters, and no one else
  1547. * can race with the signal-> fields. If we do not take any lock, the
  1548. * signal-> fields could be read out of order while another thread was just
  1549. * exiting. So we should place a read memory barrier when we avoid the lock.
  1550. * On the writer side, write memory barrier is implied in __exit_signal
  1551. * as __exit_signal releases the siglock spinlock after updating the signal->
  1552. * fields. But we don't do this yet to keep things simple.
  1553. *
  1554. */
  1555. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1556. {
  1557. r->ru_nvcsw += t->nvcsw;
  1558. r->ru_nivcsw += t->nivcsw;
  1559. r->ru_minflt += t->min_flt;
  1560. r->ru_majflt += t->maj_flt;
  1561. r->ru_inblock += task_io_get_inblock(t);
  1562. r->ru_oublock += task_io_get_oublock(t);
  1563. }
  1564. void getrusage(struct task_struct *p, int who, struct rusage *r)
  1565. {
  1566. struct task_struct *t;
  1567. unsigned long flags;
  1568. u64 tgutime, tgstime, utime, stime;
  1569. unsigned long maxrss;
  1570. struct mm_struct *mm;
  1571. struct signal_struct *sig = p->signal;
  1572. unsigned int seq = 0;
  1573. retry:
  1574. memset(r, 0, sizeof(*r));
  1575. utime = stime = 0;
  1576. maxrss = 0;
  1577. if (who == RUSAGE_THREAD) {
  1578. task_cputime_adjusted(current, &utime, &stime);
  1579. accumulate_thread_rusage(p, r);
  1580. maxrss = sig->maxrss;
  1581. goto out_thread;
  1582. }
  1583. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  1584. switch (who) {
  1585. case RUSAGE_BOTH:
  1586. case RUSAGE_CHILDREN:
  1587. utime = sig->cutime;
  1588. stime = sig->cstime;
  1589. r->ru_nvcsw = sig->cnvcsw;
  1590. r->ru_nivcsw = sig->cnivcsw;
  1591. r->ru_minflt = sig->cmin_flt;
  1592. r->ru_majflt = sig->cmaj_flt;
  1593. r->ru_inblock = sig->cinblock;
  1594. r->ru_oublock = sig->coublock;
  1595. maxrss = sig->cmaxrss;
  1596. if (who == RUSAGE_CHILDREN)
  1597. break;
  1598. fallthrough;
  1599. case RUSAGE_SELF:
  1600. r->ru_nvcsw += sig->nvcsw;
  1601. r->ru_nivcsw += sig->nivcsw;
  1602. r->ru_minflt += sig->min_flt;
  1603. r->ru_majflt += sig->maj_flt;
  1604. r->ru_inblock += sig->inblock;
  1605. r->ru_oublock += sig->oublock;
  1606. if (maxrss < sig->maxrss)
  1607. maxrss = sig->maxrss;
  1608. rcu_read_lock();
  1609. __for_each_thread(sig, t)
  1610. accumulate_thread_rusage(t, r);
  1611. rcu_read_unlock();
  1612. break;
  1613. default:
  1614. BUG();
  1615. }
  1616. if (need_seqretry(&sig->stats_lock, seq)) {
  1617. seq = 1;
  1618. goto retry;
  1619. }
  1620. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  1621. if (who == RUSAGE_CHILDREN)
  1622. goto out_children;
  1623. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1624. utime += tgutime;
  1625. stime += tgstime;
  1626. out_thread:
  1627. mm = get_task_mm(p);
  1628. if (mm) {
  1629. setmax_mm_hiwater_rss(&maxrss, mm);
  1630. mmput(mm);
  1631. }
  1632. out_children:
  1633. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1634. r->ru_utime = ns_to_kernel_old_timeval(utime);
  1635. r->ru_stime = ns_to_kernel_old_timeval(stime);
  1636. }
  1637. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1638. {
  1639. struct rusage r;
  1640. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1641. who != RUSAGE_THREAD)
  1642. return -EINVAL;
  1643. getrusage(current, who, &r);
  1644. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1645. }
  1646. #ifdef CONFIG_COMPAT
  1647. COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
  1648. {
  1649. struct rusage r;
  1650. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1651. who != RUSAGE_THREAD)
  1652. return -EINVAL;
  1653. getrusage(current, who, &r);
  1654. return put_compat_rusage(&r, ru);
  1655. }
  1656. #endif
  1657. SYSCALL_DEFINE1(umask, int, mask)
  1658. {
  1659. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1660. return mask;
  1661. }
  1662. static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
  1663. {
  1664. struct fd exe;
  1665. struct inode *inode;
  1666. int err;
  1667. exe = fdget(fd);
  1668. if (!fd_file(exe))
  1669. return -EBADF;
  1670. inode = file_inode(fd_file(exe));
  1671. /*
  1672. * Because the original mm->exe_file points to executable file, make
  1673. * sure that this one is executable as well, to avoid breaking an
  1674. * overall picture.
  1675. */
  1676. err = -EACCES;
  1677. if (!S_ISREG(inode->i_mode) || path_noexec(&fd_file(exe)->f_path))
  1678. goto exit;
  1679. err = file_permission(fd_file(exe), MAY_EXEC);
  1680. if (err)
  1681. goto exit;
  1682. err = replace_mm_exe_file(mm, fd_file(exe));
  1683. exit:
  1684. fdput(exe);
  1685. return err;
  1686. }
  1687. /*
  1688. * Check arithmetic relations of passed addresses.
  1689. *
  1690. * WARNING: we don't require any capability here so be very careful
  1691. * in what is allowed for modification from userspace.
  1692. */
  1693. static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
  1694. {
  1695. unsigned long mmap_max_addr = TASK_SIZE;
  1696. int error = -EINVAL, i;
  1697. static const unsigned char offsets[] = {
  1698. offsetof(struct prctl_mm_map, start_code),
  1699. offsetof(struct prctl_mm_map, end_code),
  1700. offsetof(struct prctl_mm_map, start_data),
  1701. offsetof(struct prctl_mm_map, end_data),
  1702. offsetof(struct prctl_mm_map, start_brk),
  1703. offsetof(struct prctl_mm_map, brk),
  1704. offsetof(struct prctl_mm_map, start_stack),
  1705. offsetof(struct prctl_mm_map, arg_start),
  1706. offsetof(struct prctl_mm_map, arg_end),
  1707. offsetof(struct prctl_mm_map, env_start),
  1708. offsetof(struct prctl_mm_map, env_end),
  1709. };
  1710. /*
  1711. * Make sure the members are not somewhere outside
  1712. * of allowed address space.
  1713. */
  1714. for (i = 0; i < ARRAY_SIZE(offsets); i++) {
  1715. u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
  1716. if ((unsigned long)val >= mmap_max_addr ||
  1717. (unsigned long)val < mmap_min_addr)
  1718. goto out;
  1719. }
  1720. /*
  1721. * Make sure the pairs are ordered.
  1722. */
  1723. #define __prctl_check_order(__m1, __op, __m2) \
  1724. ((unsigned long)prctl_map->__m1 __op \
  1725. (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
  1726. error = __prctl_check_order(start_code, <, end_code);
  1727. error |= __prctl_check_order(start_data,<=, end_data);
  1728. error |= __prctl_check_order(start_brk, <=, brk);
  1729. error |= __prctl_check_order(arg_start, <=, arg_end);
  1730. error |= __prctl_check_order(env_start, <=, env_end);
  1731. if (error)
  1732. goto out;
  1733. #undef __prctl_check_order
  1734. error = -EINVAL;
  1735. /*
  1736. * Neither we should allow to override limits if they set.
  1737. */
  1738. if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
  1739. prctl_map->start_brk, prctl_map->end_data,
  1740. prctl_map->start_data))
  1741. goto out;
  1742. error = 0;
  1743. out:
  1744. return error;
  1745. }
  1746. #ifdef CONFIG_CHECKPOINT_RESTORE
  1747. static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
  1748. {
  1749. struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
  1750. unsigned long user_auxv[AT_VECTOR_SIZE];
  1751. struct mm_struct *mm = current->mm;
  1752. int error;
  1753. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1754. BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
  1755. if (opt == PR_SET_MM_MAP_SIZE)
  1756. return put_user((unsigned int)sizeof(prctl_map),
  1757. (unsigned int __user *)addr);
  1758. if (data_size != sizeof(prctl_map))
  1759. return -EINVAL;
  1760. if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
  1761. return -EFAULT;
  1762. error = validate_prctl_map_addr(&prctl_map);
  1763. if (error)
  1764. return error;
  1765. if (prctl_map.auxv_size) {
  1766. /*
  1767. * Someone is trying to cheat the auxv vector.
  1768. */
  1769. if (!prctl_map.auxv ||
  1770. prctl_map.auxv_size > sizeof(mm->saved_auxv))
  1771. return -EINVAL;
  1772. memset(user_auxv, 0, sizeof(user_auxv));
  1773. if (copy_from_user(user_auxv,
  1774. (const void __user *)prctl_map.auxv,
  1775. prctl_map.auxv_size))
  1776. return -EFAULT;
  1777. /* Last entry must be AT_NULL as specification requires */
  1778. user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
  1779. user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
  1780. }
  1781. if (prctl_map.exe_fd != (u32)-1) {
  1782. /*
  1783. * Check if the current user is checkpoint/restore capable.
  1784. * At the time of this writing, it checks for CAP_SYS_ADMIN
  1785. * or CAP_CHECKPOINT_RESTORE.
  1786. * Note that a user with access to ptrace can masquerade an
  1787. * arbitrary program as any executable, even setuid ones.
  1788. * This may have implications in the tomoyo subsystem.
  1789. */
  1790. if (!checkpoint_restore_ns_capable(current_user_ns()))
  1791. return -EPERM;
  1792. error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
  1793. if (error)
  1794. return error;
  1795. }
  1796. /*
  1797. * arg_lock protects concurrent updates but we still need mmap_lock for
  1798. * read to exclude races with sys_brk.
  1799. */
  1800. mmap_read_lock(mm);
  1801. /*
  1802. * We don't validate if these members are pointing to
  1803. * real present VMAs because application may have correspond
  1804. * VMAs already unmapped and kernel uses these members for statistics
  1805. * output in procfs mostly, except
  1806. *
  1807. * - @start_brk/@brk which are used in do_brk_flags but kernel lookups
  1808. * for VMAs when updating these members so anything wrong written
  1809. * here cause kernel to swear at userspace program but won't lead
  1810. * to any problem in kernel itself
  1811. */
  1812. spin_lock(&mm->arg_lock);
  1813. mm->start_code = prctl_map.start_code;
  1814. mm->end_code = prctl_map.end_code;
  1815. mm->start_data = prctl_map.start_data;
  1816. mm->end_data = prctl_map.end_data;
  1817. mm->start_brk = prctl_map.start_brk;
  1818. mm->brk = prctl_map.brk;
  1819. mm->start_stack = prctl_map.start_stack;
  1820. mm->arg_start = prctl_map.arg_start;
  1821. mm->arg_end = prctl_map.arg_end;
  1822. mm->env_start = prctl_map.env_start;
  1823. mm->env_end = prctl_map.env_end;
  1824. spin_unlock(&mm->arg_lock);
  1825. /*
  1826. * Note this update of @saved_auxv is lockless thus
  1827. * if someone reads this member in procfs while we're
  1828. * updating -- it may get partly updated results. It's
  1829. * known and acceptable trade off: we leave it as is to
  1830. * not introduce additional locks here making the kernel
  1831. * more complex.
  1832. */
  1833. if (prctl_map.auxv_size)
  1834. memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
  1835. mmap_read_unlock(mm);
  1836. return 0;
  1837. }
  1838. #endif /* CONFIG_CHECKPOINT_RESTORE */
  1839. static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
  1840. unsigned long len)
  1841. {
  1842. /*
  1843. * This doesn't move the auxiliary vector itself since it's pinned to
  1844. * mm_struct, but it permits filling the vector with new values. It's
  1845. * up to the caller to provide sane values here, otherwise userspace
  1846. * tools which use this vector might be unhappy.
  1847. */
  1848. unsigned long user_auxv[AT_VECTOR_SIZE] = {};
  1849. if (len > sizeof(user_auxv))
  1850. return -EINVAL;
  1851. if (copy_from_user(user_auxv, (const void __user *)addr, len))
  1852. return -EFAULT;
  1853. /* Make sure the last entry is always AT_NULL */
  1854. user_auxv[AT_VECTOR_SIZE - 2] = 0;
  1855. user_auxv[AT_VECTOR_SIZE - 1] = 0;
  1856. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1857. task_lock(current);
  1858. memcpy(mm->saved_auxv, user_auxv, len);
  1859. task_unlock(current);
  1860. return 0;
  1861. }
  1862. static int prctl_set_mm(int opt, unsigned long addr,
  1863. unsigned long arg4, unsigned long arg5)
  1864. {
  1865. struct mm_struct *mm = current->mm;
  1866. struct prctl_mm_map prctl_map = {
  1867. .auxv = NULL,
  1868. .auxv_size = 0,
  1869. .exe_fd = -1,
  1870. };
  1871. struct vm_area_struct *vma;
  1872. int error;
  1873. if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
  1874. opt != PR_SET_MM_MAP &&
  1875. opt != PR_SET_MM_MAP_SIZE)))
  1876. return -EINVAL;
  1877. #ifdef CONFIG_CHECKPOINT_RESTORE
  1878. if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
  1879. return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
  1880. #endif
  1881. if (!capable(CAP_SYS_RESOURCE))
  1882. return -EPERM;
  1883. if (opt == PR_SET_MM_EXE_FILE)
  1884. return prctl_set_mm_exe_file(mm, (unsigned int)addr);
  1885. if (opt == PR_SET_MM_AUXV)
  1886. return prctl_set_auxv(mm, addr, arg4);
  1887. if (addr >= TASK_SIZE || addr < mmap_min_addr)
  1888. return -EINVAL;
  1889. error = -EINVAL;
  1890. /*
  1891. * arg_lock protects concurrent updates of arg boundaries, we need
  1892. * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
  1893. * validation.
  1894. */
  1895. mmap_read_lock(mm);
  1896. vma = find_vma(mm, addr);
  1897. spin_lock(&mm->arg_lock);
  1898. prctl_map.start_code = mm->start_code;
  1899. prctl_map.end_code = mm->end_code;
  1900. prctl_map.start_data = mm->start_data;
  1901. prctl_map.end_data = mm->end_data;
  1902. prctl_map.start_brk = mm->start_brk;
  1903. prctl_map.brk = mm->brk;
  1904. prctl_map.start_stack = mm->start_stack;
  1905. prctl_map.arg_start = mm->arg_start;
  1906. prctl_map.arg_end = mm->arg_end;
  1907. prctl_map.env_start = mm->env_start;
  1908. prctl_map.env_end = mm->env_end;
  1909. switch (opt) {
  1910. case PR_SET_MM_START_CODE:
  1911. prctl_map.start_code = addr;
  1912. break;
  1913. case PR_SET_MM_END_CODE:
  1914. prctl_map.end_code = addr;
  1915. break;
  1916. case PR_SET_MM_START_DATA:
  1917. prctl_map.start_data = addr;
  1918. break;
  1919. case PR_SET_MM_END_DATA:
  1920. prctl_map.end_data = addr;
  1921. break;
  1922. case PR_SET_MM_START_STACK:
  1923. prctl_map.start_stack = addr;
  1924. break;
  1925. case PR_SET_MM_START_BRK:
  1926. prctl_map.start_brk = addr;
  1927. break;
  1928. case PR_SET_MM_BRK:
  1929. prctl_map.brk = addr;
  1930. break;
  1931. case PR_SET_MM_ARG_START:
  1932. prctl_map.arg_start = addr;
  1933. break;
  1934. case PR_SET_MM_ARG_END:
  1935. prctl_map.arg_end = addr;
  1936. break;
  1937. case PR_SET_MM_ENV_START:
  1938. prctl_map.env_start = addr;
  1939. break;
  1940. case PR_SET_MM_ENV_END:
  1941. prctl_map.env_end = addr;
  1942. break;
  1943. default:
  1944. goto out;
  1945. }
  1946. error = validate_prctl_map_addr(&prctl_map);
  1947. if (error)
  1948. goto out;
  1949. switch (opt) {
  1950. /*
  1951. * If command line arguments and environment
  1952. * are placed somewhere else on stack, we can
  1953. * set them up here, ARG_START/END to setup
  1954. * command line arguments and ENV_START/END
  1955. * for environment.
  1956. */
  1957. case PR_SET_MM_START_STACK:
  1958. case PR_SET_MM_ARG_START:
  1959. case PR_SET_MM_ARG_END:
  1960. case PR_SET_MM_ENV_START:
  1961. case PR_SET_MM_ENV_END:
  1962. if (!vma) {
  1963. error = -EFAULT;
  1964. goto out;
  1965. }
  1966. }
  1967. mm->start_code = prctl_map.start_code;
  1968. mm->end_code = prctl_map.end_code;
  1969. mm->start_data = prctl_map.start_data;
  1970. mm->end_data = prctl_map.end_data;
  1971. mm->start_brk = prctl_map.start_brk;
  1972. mm->brk = prctl_map.brk;
  1973. mm->start_stack = prctl_map.start_stack;
  1974. mm->arg_start = prctl_map.arg_start;
  1975. mm->arg_end = prctl_map.arg_end;
  1976. mm->env_start = prctl_map.env_start;
  1977. mm->env_end = prctl_map.env_end;
  1978. error = 0;
  1979. out:
  1980. spin_unlock(&mm->arg_lock);
  1981. mmap_read_unlock(mm);
  1982. return error;
  1983. }
  1984. #ifdef CONFIG_CHECKPOINT_RESTORE
  1985. static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
  1986. {
  1987. return put_user(me->clear_child_tid, tid_addr);
  1988. }
  1989. #else
  1990. static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
  1991. {
  1992. return -EINVAL;
  1993. }
  1994. #endif
  1995. static int propagate_has_child_subreaper(struct task_struct *p, void *data)
  1996. {
  1997. /*
  1998. * If task has has_child_subreaper - all its descendants
  1999. * already have these flag too and new descendants will
  2000. * inherit it on fork, skip them.
  2001. *
  2002. * If we've found child_reaper - skip descendants in
  2003. * it's subtree as they will never get out pidns.
  2004. */
  2005. if (p->signal->has_child_subreaper ||
  2006. is_child_reaper(task_pid(p)))
  2007. return 0;
  2008. p->signal->has_child_subreaper = 1;
  2009. return 1;
  2010. }
  2011. int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
  2012. {
  2013. return -EINVAL;
  2014. }
  2015. int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
  2016. unsigned long ctrl)
  2017. {
  2018. return -EINVAL;
  2019. }
  2020. #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
  2021. #ifdef CONFIG_ANON_VMA_NAME
  2022. #define ANON_VMA_NAME_MAX_LEN 80
  2023. #define ANON_VMA_NAME_INVALID_CHARS "\\`$[]"
  2024. static inline bool is_valid_name_char(char ch)
  2025. {
  2026. /* printable ascii characters, excluding ANON_VMA_NAME_INVALID_CHARS */
  2027. return ch > 0x1f && ch < 0x7f &&
  2028. !strchr(ANON_VMA_NAME_INVALID_CHARS, ch);
  2029. }
  2030. static int prctl_set_vma(unsigned long opt, unsigned long addr,
  2031. unsigned long size, unsigned long arg)
  2032. {
  2033. struct mm_struct *mm = current->mm;
  2034. const char __user *uname;
  2035. struct anon_vma_name *anon_name = NULL;
  2036. int error;
  2037. switch (opt) {
  2038. case PR_SET_VMA_ANON_NAME:
  2039. uname = (const char __user *)arg;
  2040. if (uname) {
  2041. char *name, *pch;
  2042. name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN);
  2043. if (IS_ERR(name))
  2044. return PTR_ERR(name);
  2045. for (pch = name; *pch != '\0'; pch++) {
  2046. if (!is_valid_name_char(*pch)) {
  2047. kfree(name);
  2048. return -EINVAL;
  2049. }
  2050. }
  2051. /* anon_vma has its own copy */
  2052. anon_name = anon_vma_name_alloc(name);
  2053. kfree(name);
  2054. if (!anon_name)
  2055. return -ENOMEM;
  2056. }
  2057. mmap_write_lock(mm);
  2058. error = madvise_set_anon_name(mm, addr, size, anon_name);
  2059. mmap_write_unlock(mm);
  2060. anon_vma_name_put(anon_name);
  2061. break;
  2062. default:
  2063. error = -EINVAL;
  2064. }
  2065. return error;
  2066. }
  2067. #else /* CONFIG_ANON_VMA_NAME */
  2068. static int prctl_set_vma(unsigned long opt, unsigned long start,
  2069. unsigned long size, unsigned long arg)
  2070. {
  2071. return -EINVAL;
  2072. }
  2073. #endif /* CONFIG_ANON_VMA_NAME */
  2074. static inline unsigned long get_current_mdwe(void)
  2075. {
  2076. unsigned long ret = 0;
  2077. if (test_bit(MMF_HAS_MDWE, &current->mm->flags))
  2078. ret |= PR_MDWE_REFUSE_EXEC_GAIN;
  2079. if (test_bit(MMF_HAS_MDWE_NO_INHERIT, &current->mm->flags))
  2080. ret |= PR_MDWE_NO_INHERIT;
  2081. return ret;
  2082. }
  2083. static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
  2084. unsigned long arg4, unsigned long arg5)
  2085. {
  2086. unsigned long current_bits;
  2087. if (arg3 || arg4 || arg5)
  2088. return -EINVAL;
  2089. if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT))
  2090. return -EINVAL;
  2091. /* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */
  2092. if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN))
  2093. return -EINVAL;
  2094. /*
  2095. * EOPNOTSUPP might be more appropriate here in principle, but
  2096. * existing userspace depends on EINVAL specifically.
  2097. */
  2098. if (!arch_memory_deny_write_exec_supported())
  2099. return -EINVAL;
  2100. current_bits = get_current_mdwe();
  2101. if (current_bits && current_bits != bits)
  2102. return -EPERM; /* Cannot unset the flags */
  2103. if (bits & PR_MDWE_NO_INHERIT)
  2104. set_bit(MMF_HAS_MDWE_NO_INHERIT, &current->mm->flags);
  2105. if (bits & PR_MDWE_REFUSE_EXEC_GAIN)
  2106. set_bit(MMF_HAS_MDWE, &current->mm->flags);
  2107. return 0;
  2108. }
  2109. static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
  2110. unsigned long arg4, unsigned long arg5)
  2111. {
  2112. if (arg2 || arg3 || arg4 || arg5)
  2113. return -EINVAL;
  2114. return get_current_mdwe();
  2115. }
  2116. static int prctl_get_auxv(void __user *addr, unsigned long len)
  2117. {
  2118. struct mm_struct *mm = current->mm;
  2119. unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
  2120. if (size && copy_to_user(addr, mm->saved_auxv, size))
  2121. return -EFAULT;
  2122. return sizeof(mm->saved_auxv);
  2123. }
  2124. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  2125. unsigned long, arg4, unsigned long, arg5)
  2126. {
  2127. struct task_struct *me = current;
  2128. unsigned char comm[sizeof(me->comm)];
  2129. long error;
  2130. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  2131. if (error != -ENOSYS)
  2132. return error;
  2133. error = 0;
  2134. switch (option) {
  2135. case PR_SET_PDEATHSIG:
  2136. if (!valid_signal(arg2)) {
  2137. error = -EINVAL;
  2138. break;
  2139. }
  2140. me->pdeath_signal = arg2;
  2141. break;
  2142. case PR_GET_PDEATHSIG:
  2143. error = put_user(me->pdeath_signal, (int __user *)arg2);
  2144. break;
  2145. case PR_GET_DUMPABLE:
  2146. error = get_dumpable(me->mm);
  2147. break;
  2148. case PR_SET_DUMPABLE:
  2149. if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
  2150. error = -EINVAL;
  2151. break;
  2152. }
  2153. set_dumpable(me->mm, arg2);
  2154. break;
  2155. case PR_SET_UNALIGN:
  2156. error = SET_UNALIGN_CTL(me, arg2);
  2157. break;
  2158. case PR_GET_UNALIGN:
  2159. error = GET_UNALIGN_CTL(me, arg2);
  2160. break;
  2161. case PR_SET_FPEMU:
  2162. error = SET_FPEMU_CTL(me, arg2);
  2163. break;
  2164. case PR_GET_FPEMU:
  2165. error = GET_FPEMU_CTL(me, arg2);
  2166. break;
  2167. case PR_SET_FPEXC:
  2168. error = SET_FPEXC_CTL(me, arg2);
  2169. break;
  2170. case PR_GET_FPEXC:
  2171. error = GET_FPEXC_CTL(me, arg2);
  2172. break;
  2173. case PR_GET_TIMING:
  2174. error = PR_TIMING_STATISTICAL;
  2175. break;
  2176. case PR_SET_TIMING:
  2177. if (arg2 != PR_TIMING_STATISTICAL)
  2178. error = -EINVAL;
  2179. break;
  2180. case PR_SET_NAME:
  2181. comm[sizeof(me->comm) - 1] = 0;
  2182. if (strncpy_from_user(comm, (char __user *)arg2,
  2183. sizeof(me->comm) - 1) < 0)
  2184. return -EFAULT;
  2185. set_task_comm(me, comm);
  2186. proc_comm_connector(me);
  2187. break;
  2188. case PR_GET_NAME:
  2189. get_task_comm(comm, me);
  2190. if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
  2191. return -EFAULT;
  2192. break;
  2193. case PR_GET_ENDIAN:
  2194. error = GET_ENDIAN(me, arg2);
  2195. break;
  2196. case PR_SET_ENDIAN:
  2197. error = SET_ENDIAN(me, arg2);
  2198. break;
  2199. case PR_GET_SECCOMP:
  2200. error = prctl_get_seccomp();
  2201. break;
  2202. case PR_SET_SECCOMP:
  2203. error = prctl_set_seccomp(arg2, (char __user *)arg3);
  2204. break;
  2205. case PR_GET_TSC:
  2206. error = GET_TSC_CTL(arg2);
  2207. break;
  2208. case PR_SET_TSC:
  2209. error = SET_TSC_CTL(arg2);
  2210. break;
  2211. case PR_TASK_PERF_EVENTS_DISABLE:
  2212. error = perf_event_task_disable();
  2213. break;
  2214. case PR_TASK_PERF_EVENTS_ENABLE:
  2215. error = perf_event_task_enable();
  2216. break;
  2217. case PR_GET_TIMERSLACK:
  2218. if (current->timer_slack_ns > ULONG_MAX)
  2219. error = ULONG_MAX;
  2220. else
  2221. error = current->timer_slack_ns;
  2222. break;
  2223. case PR_SET_TIMERSLACK:
  2224. if (rt_or_dl_task_policy(current))
  2225. break;
  2226. if (arg2 <= 0)
  2227. current->timer_slack_ns =
  2228. current->default_timer_slack_ns;
  2229. else
  2230. current->timer_slack_ns = arg2;
  2231. break;
  2232. case PR_MCE_KILL:
  2233. if (arg4 | arg5)
  2234. return -EINVAL;
  2235. switch (arg2) {
  2236. case PR_MCE_KILL_CLEAR:
  2237. if (arg3 != 0)
  2238. return -EINVAL;
  2239. current->flags &= ~PF_MCE_PROCESS;
  2240. break;
  2241. case PR_MCE_KILL_SET:
  2242. current->flags |= PF_MCE_PROCESS;
  2243. if (arg3 == PR_MCE_KILL_EARLY)
  2244. current->flags |= PF_MCE_EARLY;
  2245. else if (arg3 == PR_MCE_KILL_LATE)
  2246. current->flags &= ~PF_MCE_EARLY;
  2247. else if (arg3 == PR_MCE_KILL_DEFAULT)
  2248. current->flags &=
  2249. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  2250. else
  2251. return -EINVAL;
  2252. break;
  2253. default:
  2254. return -EINVAL;
  2255. }
  2256. break;
  2257. case PR_MCE_KILL_GET:
  2258. if (arg2 | arg3 | arg4 | arg5)
  2259. return -EINVAL;
  2260. if (current->flags & PF_MCE_PROCESS)
  2261. error = (current->flags & PF_MCE_EARLY) ?
  2262. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  2263. else
  2264. error = PR_MCE_KILL_DEFAULT;
  2265. break;
  2266. case PR_SET_MM:
  2267. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  2268. break;
  2269. case PR_GET_TID_ADDRESS:
  2270. error = prctl_get_tid_address(me, (int __user * __user *)arg2);
  2271. break;
  2272. case PR_SET_CHILD_SUBREAPER:
  2273. me->signal->is_child_subreaper = !!arg2;
  2274. if (!arg2)
  2275. break;
  2276. walk_process_tree(me, propagate_has_child_subreaper, NULL);
  2277. break;
  2278. case PR_GET_CHILD_SUBREAPER:
  2279. error = put_user(me->signal->is_child_subreaper,
  2280. (int __user *)arg2);
  2281. break;
  2282. case PR_SET_NO_NEW_PRIVS:
  2283. if (arg2 != 1 || arg3 || arg4 || arg5)
  2284. return -EINVAL;
  2285. task_set_no_new_privs(current);
  2286. break;
  2287. case PR_GET_NO_NEW_PRIVS:
  2288. if (arg2 || arg3 || arg4 || arg5)
  2289. return -EINVAL;
  2290. return task_no_new_privs(current) ? 1 : 0;
  2291. case PR_GET_THP_DISABLE:
  2292. if (arg2 || arg3 || arg4 || arg5)
  2293. return -EINVAL;
  2294. error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
  2295. break;
  2296. case PR_SET_THP_DISABLE:
  2297. if (arg3 || arg4 || arg5)
  2298. return -EINVAL;
  2299. if (mmap_write_lock_killable(me->mm))
  2300. return -EINTR;
  2301. if (arg2)
  2302. set_bit(MMF_DISABLE_THP, &me->mm->flags);
  2303. else
  2304. clear_bit(MMF_DISABLE_THP, &me->mm->flags);
  2305. mmap_write_unlock(me->mm);
  2306. break;
  2307. case PR_MPX_ENABLE_MANAGEMENT:
  2308. case PR_MPX_DISABLE_MANAGEMENT:
  2309. /* No longer implemented: */
  2310. return -EINVAL;
  2311. case PR_SET_FP_MODE:
  2312. error = SET_FP_MODE(me, arg2);
  2313. break;
  2314. case PR_GET_FP_MODE:
  2315. error = GET_FP_MODE(me);
  2316. break;
  2317. case PR_SVE_SET_VL:
  2318. error = SVE_SET_VL(arg2);
  2319. break;
  2320. case PR_SVE_GET_VL:
  2321. error = SVE_GET_VL();
  2322. break;
  2323. case PR_SME_SET_VL:
  2324. error = SME_SET_VL(arg2);
  2325. break;
  2326. case PR_SME_GET_VL:
  2327. error = SME_GET_VL();
  2328. break;
  2329. case PR_GET_SPECULATION_CTRL:
  2330. if (arg3 || arg4 || arg5)
  2331. return -EINVAL;
  2332. error = arch_prctl_spec_ctrl_get(me, arg2);
  2333. break;
  2334. case PR_SET_SPECULATION_CTRL:
  2335. if (arg4 || arg5)
  2336. return -EINVAL;
  2337. error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
  2338. break;
  2339. case PR_PAC_RESET_KEYS:
  2340. if (arg3 || arg4 || arg5)
  2341. return -EINVAL;
  2342. error = PAC_RESET_KEYS(me, arg2);
  2343. break;
  2344. case PR_PAC_SET_ENABLED_KEYS:
  2345. if (arg4 || arg5)
  2346. return -EINVAL;
  2347. error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
  2348. break;
  2349. case PR_PAC_GET_ENABLED_KEYS:
  2350. if (arg2 || arg3 || arg4 || arg5)
  2351. return -EINVAL;
  2352. error = PAC_GET_ENABLED_KEYS(me);
  2353. break;
  2354. case PR_SET_TAGGED_ADDR_CTRL:
  2355. if (arg3 || arg4 || arg5)
  2356. return -EINVAL;
  2357. error = SET_TAGGED_ADDR_CTRL(arg2);
  2358. break;
  2359. case PR_GET_TAGGED_ADDR_CTRL:
  2360. if (arg2 || arg3 || arg4 || arg5)
  2361. return -EINVAL;
  2362. error = GET_TAGGED_ADDR_CTRL();
  2363. break;
  2364. case PR_SET_IO_FLUSHER:
  2365. if (!capable(CAP_SYS_RESOURCE))
  2366. return -EPERM;
  2367. if (arg3 || arg4 || arg5)
  2368. return -EINVAL;
  2369. if (arg2 == 1)
  2370. current->flags |= PR_IO_FLUSHER;
  2371. else if (!arg2)
  2372. current->flags &= ~PR_IO_FLUSHER;
  2373. else
  2374. return -EINVAL;
  2375. break;
  2376. case PR_GET_IO_FLUSHER:
  2377. if (!capable(CAP_SYS_RESOURCE))
  2378. return -EPERM;
  2379. if (arg2 || arg3 || arg4 || arg5)
  2380. return -EINVAL;
  2381. error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
  2382. break;
  2383. case PR_SET_SYSCALL_USER_DISPATCH:
  2384. error = set_syscall_user_dispatch(arg2, arg3, arg4,
  2385. (char __user *) arg5);
  2386. break;
  2387. #ifdef CONFIG_SCHED_CORE
  2388. case PR_SCHED_CORE:
  2389. error = sched_core_share_pid(arg2, arg3, arg4, arg5);
  2390. break;
  2391. #endif
  2392. case PR_SET_MDWE:
  2393. error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
  2394. break;
  2395. case PR_GET_MDWE:
  2396. error = prctl_get_mdwe(arg2, arg3, arg4, arg5);
  2397. break;
  2398. case PR_PPC_GET_DEXCR:
  2399. if (arg3 || arg4 || arg5)
  2400. return -EINVAL;
  2401. error = PPC_GET_DEXCR_ASPECT(me, arg2);
  2402. break;
  2403. case PR_PPC_SET_DEXCR:
  2404. if (arg4 || arg5)
  2405. return -EINVAL;
  2406. error = PPC_SET_DEXCR_ASPECT(me, arg2, arg3);
  2407. break;
  2408. case PR_SET_VMA:
  2409. error = prctl_set_vma(arg2, arg3, arg4, arg5);
  2410. break;
  2411. case PR_GET_AUXV:
  2412. if (arg4 || arg5)
  2413. return -EINVAL;
  2414. error = prctl_get_auxv((void __user *)arg2, arg3);
  2415. break;
  2416. #ifdef CONFIG_KSM
  2417. case PR_SET_MEMORY_MERGE:
  2418. if (arg3 || arg4 || arg5)
  2419. return -EINVAL;
  2420. if (mmap_write_lock_killable(me->mm))
  2421. return -EINTR;
  2422. if (arg2)
  2423. error = ksm_enable_merge_any(me->mm);
  2424. else
  2425. error = ksm_disable_merge_any(me->mm);
  2426. mmap_write_unlock(me->mm);
  2427. break;
  2428. case PR_GET_MEMORY_MERGE:
  2429. if (arg2 || arg3 || arg4 || arg5)
  2430. return -EINVAL;
  2431. error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
  2432. break;
  2433. #endif
  2434. case PR_RISCV_V_SET_CONTROL:
  2435. error = RISCV_V_SET_CONTROL(arg2);
  2436. break;
  2437. case PR_RISCV_V_GET_CONTROL:
  2438. error = RISCV_V_GET_CONTROL();
  2439. break;
  2440. case PR_RISCV_SET_ICACHE_FLUSH_CTX:
  2441. error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3);
  2442. break;
  2443. default:
  2444. error = -EINVAL;
  2445. break;
  2446. }
  2447. return error;
  2448. }
  2449. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  2450. struct getcpu_cache __user *, unused)
  2451. {
  2452. int err = 0;
  2453. int cpu = raw_smp_processor_id();
  2454. if (cpup)
  2455. err |= put_user(cpu, cpup);
  2456. if (nodep)
  2457. err |= put_user(cpu_to_node(cpu), nodep);
  2458. return err ? -EFAULT : 0;
  2459. }
  2460. /**
  2461. * do_sysinfo - fill in sysinfo struct
  2462. * @info: pointer to buffer to fill
  2463. */
  2464. static int do_sysinfo(struct sysinfo *info)
  2465. {
  2466. unsigned long mem_total, sav_total;
  2467. unsigned int mem_unit, bitcount;
  2468. struct timespec64 tp;
  2469. memset(info, 0, sizeof(struct sysinfo));
  2470. ktime_get_boottime_ts64(&tp);
  2471. timens_add_boottime(&tp);
  2472. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  2473. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  2474. info->procs = nr_threads;
  2475. si_meminfo(info);
  2476. si_swapinfo(info);
  2477. /*
  2478. * If the sum of all the available memory (i.e. ram + swap)
  2479. * is less than can be stored in a 32 bit unsigned long then
  2480. * we can be binary compatible with 2.2.x kernels. If not,
  2481. * well, in that case 2.2.x was broken anyways...
  2482. *
  2483. * -Erik Andersen <andersee@debian.org>
  2484. */
  2485. mem_total = info->totalram + info->totalswap;
  2486. if (mem_total < info->totalram || mem_total < info->totalswap)
  2487. goto out;
  2488. bitcount = 0;
  2489. mem_unit = info->mem_unit;
  2490. while (mem_unit > 1) {
  2491. bitcount++;
  2492. mem_unit >>= 1;
  2493. sav_total = mem_total;
  2494. mem_total <<= 1;
  2495. if (mem_total < sav_total)
  2496. goto out;
  2497. }
  2498. /*
  2499. * If mem_total did not overflow, multiply all memory values by
  2500. * info->mem_unit and set it to 1. This leaves things compatible
  2501. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  2502. * kernels...
  2503. */
  2504. info->mem_unit = 1;
  2505. info->totalram <<= bitcount;
  2506. info->freeram <<= bitcount;
  2507. info->sharedram <<= bitcount;
  2508. info->bufferram <<= bitcount;
  2509. info->totalswap <<= bitcount;
  2510. info->freeswap <<= bitcount;
  2511. info->totalhigh <<= bitcount;
  2512. info->freehigh <<= bitcount;
  2513. out:
  2514. return 0;
  2515. }
  2516. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  2517. {
  2518. struct sysinfo val;
  2519. do_sysinfo(&val);
  2520. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  2521. return -EFAULT;
  2522. return 0;
  2523. }
  2524. #ifdef CONFIG_COMPAT
  2525. struct compat_sysinfo {
  2526. s32 uptime;
  2527. u32 loads[3];
  2528. u32 totalram;
  2529. u32 freeram;
  2530. u32 sharedram;
  2531. u32 bufferram;
  2532. u32 totalswap;
  2533. u32 freeswap;
  2534. u16 procs;
  2535. u16 pad;
  2536. u32 totalhigh;
  2537. u32 freehigh;
  2538. u32 mem_unit;
  2539. char _f[20-2*sizeof(u32)-sizeof(int)];
  2540. };
  2541. COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
  2542. {
  2543. struct sysinfo s;
  2544. struct compat_sysinfo s_32;
  2545. do_sysinfo(&s);
  2546. /* Check to see if any memory value is too large for 32-bit and scale
  2547. * down if needed
  2548. */
  2549. if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
  2550. int bitcount = 0;
  2551. while (s.mem_unit < PAGE_SIZE) {
  2552. s.mem_unit <<= 1;
  2553. bitcount++;
  2554. }
  2555. s.totalram >>= bitcount;
  2556. s.freeram >>= bitcount;
  2557. s.sharedram >>= bitcount;
  2558. s.bufferram >>= bitcount;
  2559. s.totalswap >>= bitcount;
  2560. s.freeswap >>= bitcount;
  2561. s.totalhigh >>= bitcount;
  2562. s.freehigh >>= bitcount;
  2563. }
  2564. memset(&s_32, 0, sizeof(s_32));
  2565. s_32.uptime = s.uptime;
  2566. s_32.loads[0] = s.loads[0];
  2567. s_32.loads[1] = s.loads[1];
  2568. s_32.loads[2] = s.loads[2];
  2569. s_32.totalram = s.totalram;
  2570. s_32.freeram = s.freeram;
  2571. s_32.sharedram = s.sharedram;
  2572. s_32.bufferram = s.bufferram;
  2573. s_32.totalswap = s.totalswap;
  2574. s_32.freeswap = s.freeswap;
  2575. s_32.procs = s.procs;
  2576. s_32.totalhigh = s.totalhigh;
  2577. s_32.freehigh = s.freehigh;
  2578. s_32.mem_unit = s.mem_unit;
  2579. if (copy_to_user(info, &s_32, sizeof(s_32)))
  2580. return -EFAULT;
  2581. return 0;
  2582. }
  2583. #endif /* CONFIG_COMPAT */