sys.c 58 KB

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