sys.c 64 KB

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