sys.c 62 KB

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