sys.c 52 KB

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