sys.c 66 KB

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