sys.c 52 KB

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