sys.c 54 KB

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