sys.c 60 KB

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