sys.c 62 KB

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