sys.c 61 KB

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