sys.c 57 KB

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