sys.c 51 KB

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