sys.c 57 KB

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