sys.c 66 KB

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