sys.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kexec.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/capability.h>
  20. #include <linux/device.h>
  21. #include <linux/key.h>
  22. #include <linux/times.h>
  23. #include <linux/posix-timers.h>
  24. #include <linux/security.h>
  25. #include <linux/dcookies.h>
  26. #include <linux/suspend.h>
  27. #include <linux/tty.h>
  28. #include <linux/signal.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/compat.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/kprobes.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/io.h>
  36. #include <asm/unistd.h>
  37. #ifndef SET_UNALIGN_CTL
  38. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  39. #endif
  40. #ifndef GET_UNALIGN_CTL
  41. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  42. #endif
  43. #ifndef SET_FPEMU_CTL
  44. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  45. #endif
  46. #ifndef GET_FPEMU_CTL
  47. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  48. #endif
  49. #ifndef SET_FPEXC_CTL
  50. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  51. #endif
  52. #ifndef GET_FPEXC_CTL
  53. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  54. #endif
  55. #ifndef GET_ENDIAN
  56. # define GET_ENDIAN(a,b) (-EINVAL)
  57. #endif
  58. #ifndef SET_ENDIAN
  59. # define SET_ENDIAN(a,b) (-EINVAL)
  60. #endif
  61. /*
  62. * this is where the system-wide overflow UID and GID are defined, for
  63. * architectures that now have 32-bit UID/GID but didn't in the past
  64. */
  65. int overflowuid = DEFAULT_OVERFLOWUID;
  66. int overflowgid = DEFAULT_OVERFLOWGID;
  67. #ifdef CONFIG_UID16
  68. EXPORT_SYMBOL(overflowuid);
  69. EXPORT_SYMBOL(overflowgid);
  70. #endif
  71. /*
  72. * the same as above, but for filesystems which can only store a 16-bit
  73. * UID and GID. as such, this is needed on all architectures
  74. */
  75. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  76. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  77. EXPORT_SYMBOL(fs_overflowuid);
  78. EXPORT_SYMBOL(fs_overflowgid);
  79. /*
  80. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  81. */
  82. int C_A_D = 1;
  83. struct pid *cad_pid;
  84. EXPORT_SYMBOL(cad_pid);
  85. /*
  86. * Notifier list for kernel code which wants to be called
  87. * at shutdown. This is used to stop any idling DMA operations
  88. * and the like.
  89. */
  90. static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  91. /*
  92. * Notifier chain core routines. The exported routines below
  93. * are layered on top of these, with appropriate locking added.
  94. */
  95. static int notifier_chain_register(struct notifier_block **nl,
  96. struct notifier_block *n)
  97. {
  98. while ((*nl) != NULL) {
  99. if (n->priority > (*nl)->priority)
  100. break;
  101. nl = &((*nl)->next);
  102. }
  103. n->next = *nl;
  104. rcu_assign_pointer(*nl, n);
  105. return 0;
  106. }
  107. static int notifier_chain_unregister(struct notifier_block **nl,
  108. struct notifier_block *n)
  109. {
  110. while ((*nl) != NULL) {
  111. if ((*nl) == n) {
  112. rcu_assign_pointer(*nl, n->next);
  113. return 0;
  114. }
  115. nl = &((*nl)->next);
  116. }
  117. return -ENOENT;
  118. }
  119. static int __kprobes notifier_call_chain(struct notifier_block **nl,
  120. unsigned long val, void *v)
  121. {
  122. int ret = NOTIFY_DONE;
  123. struct notifier_block *nb, *next_nb;
  124. nb = rcu_dereference(*nl);
  125. while (nb) {
  126. next_nb = rcu_dereference(nb->next);
  127. ret = nb->notifier_call(nb, val, v);
  128. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  129. break;
  130. nb = next_nb;
  131. }
  132. return ret;
  133. }
  134. /*
  135. * Atomic notifier chain routines. Registration and unregistration
  136. * use a mutex, and call_chain is synchronized by RCU (no locks).
  137. */
  138. /**
  139. * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
  140. * @nh: Pointer to head of the atomic notifier chain
  141. * @n: New entry in notifier chain
  142. *
  143. * Adds a notifier to an atomic notifier chain.
  144. *
  145. * Currently always returns zero.
  146. */
  147. int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  148. struct notifier_block *n)
  149. {
  150. unsigned long flags;
  151. int ret;
  152. spin_lock_irqsave(&nh->lock, flags);
  153. ret = notifier_chain_register(&nh->head, n);
  154. spin_unlock_irqrestore(&nh->lock, flags);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
  158. /**
  159. * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
  160. * @nh: Pointer to head of the atomic notifier chain
  161. * @n: Entry to remove from notifier chain
  162. *
  163. * Removes a notifier from an atomic notifier chain.
  164. *
  165. * Returns zero on success or %-ENOENT on failure.
  166. */
  167. int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
  168. struct notifier_block *n)
  169. {
  170. unsigned long flags;
  171. int ret;
  172. spin_lock_irqsave(&nh->lock, flags);
  173. ret = notifier_chain_unregister(&nh->head, n);
  174. spin_unlock_irqrestore(&nh->lock, flags);
  175. synchronize_rcu();
  176. return ret;
  177. }
  178. EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
  179. /**
  180. * atomic_notifier_call_chain - Call functions in an atomic notifier chain
  181. * @nh: Pointer to head of the atomic notifier chain
  182. * @val: Value passed unmodified to notifier function
  183. * @v: Pointer passed unmodified to notifier function
  184. *
  185. * Calls each function in a notifier chain in turn. The functions
  186. * run in an atomic context, so they must not block.
  187. * This routine uses RCU to synchronize with changes to the chain.
  188. *
  189. * If the return value of the notifier can be and'ed
  190. * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
  191. * will return immediately, with the return value of
  192. * the notifier function which halted execution.
  193. * Otherwise the return value is the return value
  194. * of the last notifier function called.
  195. */
  196. int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  197. unsigned long val, void *v)
  198. {
  199. int ret;
  200. rcu_read_lock();
  201. ret = notifier_call_chain(&nh->head, val, v);
  202. rcu_read_unlock();
  203. return ret;
  204. }
  205. EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
  206. /*
  207. * Blocking notifier chain routines. All access to the chain is
  208. * synchronized by an rwsem.
  209. */
  210. /**
  211. * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
  212. * @nh: Pointer to head of the blocking notifier chain
  213. * @n: New entry in notifier chain
  214. *
  215. * Adds a notifier to a blocking notifier chain.
  216. * Must be called in process context.
  217. *
  218. * Currently always returns zero.
  219. */
  220. int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
  221. struct notifier_block *n)
  222. {
  223. int ret;
  224. /*
  225. * This code gets used during boot-up, when task switching is
  226. * not yet working and interrupts must remain disabled. At
  227. * such times we must not call down_write().
  228. */
  229. if (unlikely(system_state == SYSTEM_BOOTING))
  230. return notifier_chain_register(&nh->head, n);
  231. down_write(&nh->rwsem);
  232. ret = notifier_chain_register(&nh->head, n);
  233. up_write(&nh->rwsem);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
  237. /**
  238. * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
  239. * @nh: Pointer to head of the blocking notifier chain
  240. * @n: Entry to remove from notifier chain
  241. *
  242. * Removes a notifier from a blocking notifier chain.
  243. * Must be called from process context.
  244. *
  245. * Returns zero on success or %-ENOENT on failure.
  246. */
  247. int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
  248. struct notifier_block *n)
  249. {
  250. int ret;
  251. /*
  252. * This code gets used during boot-up, when task switching is
  253. * not yet working and interrupts must remain disabled. At
  254. * such times we must not call down_write().
  255. */
  256. if (unlikely(system_state == SYSTEM_BOOTING))
  257. return notifier_chain_unregister(&nh->head, n);
  258. down_write(&nh->rwsem);
  259. ret = notifier_chain_unregister(&nh->head, n);
  260. up_write(&nh->rwsem);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
  264. /**
  265. * blocking_notifier_call_chain - Call functions in a blocking notifier chain
  266. * @nh: Pointer to head of the blocking notifier chain
  267. * @val: Value passed unmodified to notifier function
  268. * @v: Pointer passed unmodified to notifier function
  269. *
  270. * Calls each function in a notifier chain in turn. The functions
  271. * run in a process context, so they are allowed to block.
  272. *
  273. * If the return value of the notifier can be and'ed
  274. * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
  275. * will return immediately, with the return value of
  276. * the notifier function which halted execution.
  277. * Otherwise the return value is the return value
  278. * of the last notifier function called.
  279. */
  280. int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  281. unsigned long val, void *v)
  282. {
  283. int ret;
  284. down_read(&nh->rwsem);
  285. ret = notifier_call_chain(&nh->head, val, v);
  286. up_read(&nh->rwsem);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
  290. /*
  291. * Raw notifier chain routines. There is no protection;
  292. * the caller must provide it. Use at your own risk!
  293. */
  294. /**
  295. * raw_notifier_chain_register - Add notifier to a raw notifier chain
  296. * @nh: Pointer to head of the raw notifier chain
  297. * @n: New entry in notifier chain
  298. *
  299. * Adds a notifier to a raw notifier chain.
  300. * All locking must be provided by the caller.
  301. *
  302. * Currently always returns zero.
  303. */
  304. int raw_notifier_chain_register(struct raw_notifier_head *nh,
  305. struct notifier_block *n)
  306. {
  307. return notifier_chain_register(&nh->head, n);
  308. }
  309. EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
  310. /**
  311. * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
  312. * @nh: Pointer to head of the raw notifier chain
  313. * @n: Entry to remove from notifier chain
  314. *
  315. * Removes a notifier from a raw notifier chain.
  316. * All locking must be provided by the caller.
  317. *
  318. * Returns zero on success or %-ENOENT on failure.
  319. */
  320. int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
  321. struct notifier_block *n)
  322. {
  323. return notifier_chain_unregister(&nh->head, n);
  324. }
  325. EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
  326. /**
  327. * raw_notifier_call_chain - Call functions in a raw notifier chain
  328. * @nh: Pointer to head of the raw notifier chain
  329. * @val: Value passed unmodified to notifier function
  330. * @v: Pointer passed unmodified to notifier function
  331. *
  332. * Calls each function in a notifier chain in turn. The functions
  333. * run in an undefined context.
  334. * All locking must be provided by the caller.
  335. *
  336. * If the return value of the notifier can be and'ed
  337. * with %NOTIFY_STOP_MASK then raw_notifier_call_chain
  338. * will return immediately, with the return value of
  339. * the notifier function which halted execution.
  340. * Otherwise the return value is the return value
  341. * of the last notifier function called.
  342. */
  343. int raw_notifier_call_chain(struct raw_notifier_head *nh,
  344. unsigned long val, void *v)
  345. {
  346. return notifier_call_chain(&nh->head, val, v);
  347. }
  348. EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
  349. /**
  350. * register_reboot_notifier - Register function to be called at reboot time
  351. * @nb: Info about notifier function to be called
  352. *
  353. * Registers a function with the list of functions
  354. * to be called at reboot time.
  355. *
  356. * Currently always returns zero, as blocking_notifier_chain_register
  357. * always returns zero.
  358. */
  359. int register_reboot_notifier(struct notifier_block * nb)
  360. {
  361. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  362. }
  363. EXPORT_SYMBOL(register_reboot_notifier);
  364. /**
  365. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  366. * @nb: Hook to be unregistered
  367. *
  368. * Unregisters a previously registered reboot
  369. * notifier function.
  370. *
  371. * Returns zero on success, or %-ENOENT on failure.
  372. */
  373. int unregister_reboot_notifier(struct notifier_block * nb)
  374. {
  375. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  376. }
  377. EXPORT_SYMBOL(unregister_reboot_notifier);
  378. static int set_one_prio(struct task_struct *p, int niceval, int error)
  379. {
  380. int no_nice;
  381. if (p->uid != current->euid &&
  382. p->euid != current->euid && !capable(CAP_SYS_NICE)) {
  383. error = -EPERM;
  384. goto out;
  385. }
  386. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  387. error = -EACCES;
  388. goto out;
  389. }
  390. no_nice = security_task_setnice(p, niceval);
  391. if (no_nice) {
  392. error = no_nice;
  393. goto out;
  394. }
  395. if (error == -ESRCH)
  396. error = 0;
  397. set_user_nice(p, niceval);
  398. out:
  399. return error;
  400. }
  401. asmlinkage long sys_setpriority(int which, int who, int niceval)
  402. {
  403. struct task_struct *g, *p;
  404. struct user_struct *user;
  405. int error = -EINVAL;
  406. if (which > 2 || which < 0)
  407. goto out;
  408. /* normalize: avoid signed division (rounding problems) */
  409. error = -ESRCH;
  410. if (niceval < -20)
  411. niceval = -20;
  412. if (niceval > 19)
  413. niceval = 19;
  414. read_lock(&tasklist_lock);
  415. switch (which) {
  416. case PRIO_PROCESS:
  417. if (!who)
  418. who = current->pid;
  419. p = find_task_by_pid(who);
  420. if (p)
  421. error = set_one_prio(p, niceval, error);
  422. break;
  423. case PRIO_PGRP:
  424. if (!who)
  425. who = process_group(current);
  426. do_each_task_pid(who, PIDTYPE_PGID, p) {
  427. error = set_one_prio(p, niceval, error);
  428. } while_each_task_pid(who, PIDTYPE_PGID, p);
  429. break;
  430. case PRIO_USER:
  431. user = current->user;
  432. if (!who)
  433. who = current->uid;
  434. else
  435. if ((who != current->uid) && !(user = find_user(who)))
  436. goto out_unlock; /* No processes for this user */
  437. do_each_thread(g, p)
  438. if (p->uid == who)
  439. error = set_one_prio(p, niceval, error);
  440. while_each_thread(g, p);
  441. if (who != current->uid)
  442. free_uid(user); /* For find_user() */
  443. break;
  444. }
  445. out_unlock:
  446. read_unlock(&tasklist_lock);
  447. out:
  448. return error;
  449. }
  450. /*
  451. * Ugh. To avoid negative return values, "getpriority()" will
  452. * not return the normal nice-value, but a negated value that
  453. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  454. * to stay compatible.
  455. */
  456. asmlinkage long sys_getpriority(int which, int who)
  457. {
  458. struct task_struct *g, *p;
  459. struct user_struct *user;
  460. long niceval, retval = -ESRCH;
  461. if (which > 2 || which < 0)
  462. return -EINVAL;
  463. read_lock(&tasklist_lock);
  464. switch (which) {
  465. case PRIO_PROCESS:
  466. if (!who)
  467. who = current->pid;
  468. p = find_task_by_pid(who);
  469. if (p) {
  470. niceval = 20 - task_nice(p);
  471. if (niceval > retval)
  472. retval = niceval;
  473. }
  474. break;
  475. case PRIO_PGRP:
  476. if (!who)
  477. who = process_group(current);
  478. do_each_task_pid(who, PIDTYPE_PGID, p) {
  479. niceval = 20 - task_nice(p);
  480. if (niceval > retval)
  481. retval = niceval;
  482. } while_each_task_pid(who, PIDTYPE_PGID, p);
  483. break;
  484. case PRIO_USER:
  485. user = current->user;
  486. if (!who)
  487. who = current->uid;
  488. else
  489. if ((who != current->uid) && !(user = find_user(who)))
  490. goto out_unlock; /* No processes for this user */
  491. do_each_thread(g, p)
  492. if (p->uid == who) {
  493. niceval = 20 - task_nice(p);
  494. if (niceval > retval)
  495. retval = niceval;
  496. }
  497. while_each_thread(g, p);
  498. if (who != current->uid)
  499. free_uid(user); /* for find_user() */
  500. break;
  501. }
  502. out_unlock:
  503. read_unlock(&tasklist_lock);
  504. return retval;
  505. }
  506. /**
  507. * emergency_restart - reboot the system
  508. *
  509. * Without shutting down any hardware or taking any locks
  510. * reboot the system. This is called when we know we are in
  511. * trouble so this is our best effort to reboot. This is
  512. * safe to call in interrupt context.
  513. */
  514. void emergency_restart(void)
  515. {
  516. machine_emergency_restart();
  517. }
  518. EXPORT_SYMBOL_GPL(emergency_restart);
  519. static void kernel_restart_prepare(char *cmd)
  520. {
  521. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  522. system_state = SYSTEM_RESTART;
  523. device_shutdown();
  524. }
  525. /**
  526. * kernel_restart - reboot the system
  527. * @cmd: pointer to buffer containing command to execute for restart
  528. * or %NULL
  529. *
  530. * Shutdown everything and perform a clean reboot.
  531. * This is not safe to call in interrupt context.
  532. */
  533. void kernel_restart(char *cmd)
  534. {
  535. kernel_restart_prepare(cmd);
  536. if (!cmd)
  537. printk(KERN_EMERG "Restarting system.\n");
  538. else
  539. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  540. machine_restart(cmd);
  541. }
  542. EXPORT_SYMBOL_GPL(kernel_restart);
  543. /**
  544. * kernel_kexec - reboot the system
  545. *
  546. * Move into place and start executing a preloaded standalone
  547. * executable. If nothing was preloaded return an error.
  548. */
  549. static void kernel_kexec(void)
  550. {
  551. #ifdef CONFIG_KEXEC
  552. struct kimage *image;
  553. image = xchg(&kexec_image, NULL);
  554. if (!image)
  555. return;
  556. kernel_restart_prepare(NULL);
  557. printk(KERN_EMERG "Starting new kernel\n");
  558. machine_shutdown();
  559. machine_kexec(image);
  560. #endif
  561. }
  562. void kernel_shutdown_prepare(enum system_states state)
  563. {
  564. blocking_notifier_call_chain(&reboot_notifier_list,
  565. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  566. system_state = state;
  567. device_shutdown();
  568. }
  569. /**
  570. * kernel_halt - halt the system
  571. *
  572. * Shutdown everything and perform a clean system halt.
  573. */
  574. void kernel_halt(void)
  575. {
  576. kernel_shutdown_prepare(SYSTEM_HALT);
  577. printk(KERN_EMERG "System halted.\n");
  578. machine_halt();
  579. }
  580. EXPORT_SYMBOL_GPL(kernel_halt);
  581. /**
  582. * kernel_power_off - power_off the system
  583. *
  584. * Shutdown everything and perform a clean system power_off.
  585. */
  586. void kernel_power_off(void)
  587. {
  588. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  589. printk(KERN_EMERG "Power down.\n");
  590. machine_power_off();
  591. }
  592. EXPORT_SYMBOL_GPL(kernel_power_off);
  593. /*
  594. * Reboot system call: for obvious reasons only root may call it,
  595. * and even root needs to set up some magic numbers in the registers
  596. * so that some mistake won't make this reboot the whole machine.
  597. * You can also set the meaning of the ctrl-alt-del-key here.
  598. *
  599. * reboot doesn't sync: do that yourself before calling this.
  600. */
  601. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  602. {
  603. char buffer[256];
  604. /* We only trust the superuser with rebooting the system. */
  605. if (!capable(CAP_SYS_BOOT))
  606. return -EPERM;
  607. /* For safety, we require "magic" arguments. */
  608. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  609. (magic2 != LINUX_REBOOT_MAGIC2 &&
  610. magic2 != LINUX_REBOOT_MAGIC2A &&
  611. magic2 != LINUX_REBOOT_MAGIC2B &&
  612. magic2 != LINUX_REBOOT_MAGIC2C))
  613. return -EINVAL;
  614. /* Instead of trying to make the power_off code look like
  615. * halt when pm_power_off is not set do it the easy way.
  616. */
  617. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  618. cmd = LINUX_REBOOT_CMD_HALT;
  619. lock_kernel();
  620. switch (cmd) {
  621. case LINUX_REBOOT_CMD_RESTART:
  622. kernel_restart(NULL);
  623. break;
  624. case LINUX_REBOOT_CMD_CAD_ON:
  625. C_A_D = 1;
  626. break;
  627. case LINUX_REBOOT_CMD_CAD_OFF:
  628. C_A_D = 0;
  629. break;
  630. case LINUX_REBOOT_CMD_HALT:
  631. kernel_halt();
  632. unlock_kernel();
  633. do_exit(0);
  634. break;
  635. case LINUX_REBOOT_CMD_POWER_OFF:
  636. kernel_power_off();
  637. unlock_kernel();
  638. do_exit(0);
  639. break;
  640. case LINUX_REBOOT_CMD_RESTART2:
  641. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  642. unlock_kernel();
  643. return -EFAULT;
  644. }
  645. buffer[sizeof(buffer) - 1] = '\0';
  646. kernel_restart(buffer);
  647. break;
  648. case LINUX_REBOOT_CMD_KEXEC:
  649. kernel_kexec();
  650. unlock_kernel();
  651. return -EINVAL;
  652. #ifdef CONFIG_SOFTWARE_SUSPEND
  653. case LINUX_REBOOT_CMD_SW_SUSPEND:
  654. {
  655. int ret = software_suspend();
  656. unlock_kernel();
  657. return ret;
  658. }
  659. #endif
  660. default:
  661. unlock_kernel();
  662. return -EINVAL;
  663. }
  664. unlock_kernel();
  665. return 0;
  666. }
  667. static void deferred_cad(void *dummy)
  668. {
  669. kernel_restart(NULL);
  670. }
  671. /*
  672. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  673. * As it's called within an interrupt, it may NOT sync: the only choice
  674. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  675. */
  676. void ctrl_alt_del(void)
  677. {
  678. static DECLARE_WORK(cad_work, deferred_cad, NULL);
  679. if (C_A_D)
  680. schedule_work(&cad_work);
  681. else
  682. kill_cad_pid(SIGINT, 1);
  683. }
  684. /*
  685. * Unprivileged users may change the real gid to the effective gid
  686. * or vice versa. (BSD-style)
  687. *
  688. * If you set the real gid at all, or set the effective gid to a value not
  689. * equal to the real gid, then the saved gid is set to the new effective gid.
  690. *
  691. * This makes it possible for a setgid program to completely drop its
  692. * privileges, which is often a useful assertion to make when you are doing
  693. * a security audit over a program.
  694. *
  695. * The general idea is that a program which uses just setregid() will be
  696. * 100% compatible with BSD. A program which uses just setgid() will be
  697. * 100% compatible with POSIX with saved IDs.
  698. *
  699. * SMP: There are not races, the GIDs are checked only by filesystem
  700. * operations (as far as semantic preservation is concerned).
  701. */
  702. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  703. {
  704. int old_rgid = current->gid;
  705. int old_egid = current->egid;
  706. int new_rgid = old_rgid;
  707. int new_egid = old_egid;
  708. int retval;
  709. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  710. if (retval)
  711. return retval;
  712. if (rgid != (gid_t) -1) {
  713. if ((old_rgid == rgid) ||
  714. (current->egid==rgid) ||
  715. capable(CAP_SETGID))
  716. new_rgid = rgid;
  717. else
  718. return -EPERM;
  719. }
  720. if (egid != (gid_t) -1) {
  721. if ((old_rgid == egid) ||
  722. (current->egid == egid) ||
  723. (current->sgid == egid) ||
  724. capable(CAP_SETGID))
  725. new_egid = egid;
  726. else
  727. return -EPERM;
  728. }
  729. if (new_egid != old_egid) {
  730. current->mm->dumpable = suid_dumpable;
  731. smp_wmb();
  732. }
  733. if (rgid != (gid_t) -1 ||
  734. (egid != (gid_t) -1 && egid != old_rgid))
  735. current->sgid = new_egid;
  736. current->fsgid = new_egid;
  737. current->egid = new_egid;
  738. current->gid = new_rgid;
  739. key_fsgid_changed(current);
  740. proc_id_connector(current, PROC_EVENT_GID);
  741. return 0;
  742. }
  743. /*
  744. * setgid() is implemented like SysV w/ SAVED_IDS
  745. *
  746. * SMP: Same implicit races as above.
  747. */
  748. asmlinkage long sys_setgid(gid_t gid)
  749. {
  750. int old_egid = current->egid;
  751. int retval;
  752. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  753. if (retval)
  754. return retval;
  755. if (capable(CAP_SETGID)) {
  756. if (old_egid != gid) {
  757. current->mm->dumpable = suid_dumpable;
  758. smp_wmb();
  759. }
  760. current->gid = current->egid = current->sgid = current->fsgid = gid;
  761. } else if ((gid == current->gid) || (gid == current->sgid)) {
  762. if (old_egid != gid) {
  763. current->mm->dumpable = suid_dumpable;
  764. smp_wmb();
  765. }
  766. current->egid = current->fsgid = gid;
  767. }
  768. else
  769. return -EPERM;
  770. key_fsgid_changed(current);
  771. proc_id_connector(current, PROC_EVENT_GID);
  772. return 0;
  773. }
  774. static int set_user(uid_t new_ruid, int dumpclear)
  775. {
  776. struct user_struct *new_user;
  777. new_user = alloc_uid(new_ruid);
  778. if (!new_user)
  779. return -EAGAIN;
  780. if (atomic_read(&new_user->processes) >=
  781. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  782. new_user != &root_user) {
  783. free_uid(new_user);
  784. return -EAGAIN;
  785. }
  786. switch_uid(new_user);
  787. if (dumpclear) {
  788. current->mm->dumpable = suid_dumpable;
  789. smp_wmb();
  790. }
  791. current->uid = new_ruid;
  792. return 0;
  793. }
  794. /*
  795. * Unprivileged users may change the real uid to the effective uid
  796. * or vice versa. (BSD-style)
  797. *
  798. * If you set the real uid at all, or set the effective uid to a value not
  799. * equal to the real uid, then the saved uid is set to the new effective uid.
  800. *
  801. * This makes it possible for a setuid program to completely drop its
  802. * privileges, which is often a useful assertion to make when you are doing
  803. * a security audit over a program.
  804. *
  805. * The general idea is that a program which uses just setreuid() will be
  806. * 100% compatible with BSD. A program which uses just setuid() will be
  807. * 100% compatible with POSIX with saved IDs.
  808. */
  809. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  810. {
  811. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  812. int retval;
  813. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  814. if (retval)
  815. return retval;
  816. new_ruid = old_ruid = current->uid;
  817. new_euid = old_euid = current->euid;
  818. old_suid = current->suid;
  819. if (ruid != (uid_t) -1) {
  820. new_ruid = ruid;
  821. if ((old_ruid != ruid) &&
  822. (current->euid != ruid) &&
  823. !capable(CAP_SETUID))
  824. return -EPERM;
  825. }
  826. if (euid != (uid_t) -1) {
  827. new_euid = euid;
  828. if ((old_ruid != euid) &&
  829. (current->euid != euid) &&
  830. (current->suid != euid) &&
  831. !capable(CAP_SETUID))
  832. return -EPERM;
  833. }
  834. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  835. return -EAGAIN;
  836. if (new_euid != old_euid) {
  837. current->mm->dumpable = suid_dumpable;
  838. smp_wmb();
  839. }
  840. current->fsuid = current->euid = new_euid;
  841. if (ruid != (uid_t) -1 ||
  842. (euid != (uid_t) -1 && euid != old_ruid))
  843. current->suid = current->euid;
  844. current->fsuid = current->euid;
  845. key_fsuid_changed(current);
  846. proc_id_connector(current, PROC_EVENT_UID);
  847. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  848. }
  849. /*
  850. * setuid() is implemented like SysV with SAVED_IDS
  851. *
  852. * Note that SAVED_ID's is deficient in that a setuid root program
  853. * like sendmail, for example, cannot set its uid to be a normal
  854. * user and then switch back, because if you're root, setuid() sets
  855. * the saved uid too. If you don't like this, blame the bright people
  856. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  857. * will allow a root program to temporarily drop privileges and be able to
  858. * regain them by swapping the real and effective uid.
  859. */
  860. asmlinkage long sys_setuid(uid_t uid)
  861. {
  862. int old_euid = current->euid;
  863. int old_ruid, old_suid, new_ruid, new_suid;
  864. int retval;
  865. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  866. if (retval)
  867. return retval;
  868. old_ruid = new_ruid = current->uid;
  869. old_suid = current->suid;
  870. new_suid = old_suid;
  871. if (capable(CAP_SETUID)) {
  872. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  873. return -EAGAIN;
  874. new_suid = uid;
  875. } else if ((uid != current->uid) && (uid != new_suid))
  876. return -EPERM;
  877. if (old_euid != uid) {
  878. current->mm->dumpable = suid_dumpable;
  879. smp_wmb();
  880. }
  881. current->fsuid = current->euid = uid;
  882. current->suid = new_suid;
  883. key_fsuid_changed(current);
  884. proc_id_connector(current, PROC_EVENT_UID);
  885. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  886. }
  887. /*
  888. * This function implements a generic ability to update ruid, euid,
  889. * and suid. This allows you to implement the 4.4 compatible seteuid().
  890. */
  891. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  892. {
  893. int old_ruid = current->uid;
  894. int old_euid = current->euid;
  895. int old_suid = current->suid;
  896. int retval;
  897. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  898. if (retval)
  899. return retval;
  900. if (!capable(CAP_SETUID)) {
  901. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  902. (ruid != current->euid) && (ruid != current->suid))
  903. return -EPERM;
  904. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  905. (euid != current->euid) && (euid != current->suid))
  906. return -EPERM;
  907. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  908. (suid != current->euid) && (suid != current->suid))
  909. return -EPERM;
  910. }
  911. if (ruid != (uid_t) -1) {
  912. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  913. return -EAGAIN;
  914. }
  915. if (euid != (uid_t) -1) {
  916. if (euid != current->euid) {
  917. current->mm->dumpable = suid_dumpable;
  918. smp_wmb();
  919. }
  920. current->euid = euid;
  921. }
  922. current->fsuid = current->euid;
  923. if (suid != (uid_t) -1)
  924. current->suid = suid;
  925. key_fsuid_changed(current);
  926. proc_id_connector(current, PROC_EVENT_UID);
  927. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  928. }
  929. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  930. {
  931. int retval;
  932. if (!(retval = put_user(current->uid, ruid)) &&
  933. !(retval = put_user(current->euid, euid)))
  934. retval = put_user(current->suid, suid);
  935. return retval;
  936. }
  937. /*
  938. * Same as above, but for rgid, egid, sgid.
  939. */
  940. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  941. {
  942. int retval;
  943. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  944. if (retval)
  945. return retval;
  946. if (!capable(CAP_SETGID)) {
  947. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  948. (rgid != current->egid) && (rgid != current->sgid))
  949. return -EPERM;
  950. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  951. (egid != current->egid) && (egid != current->sgid))
  952. return -EPERM;
  953. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  954. (sgid != current->egid) && (sgid != current->sgid))
  955. return -EPERM;
  956. }
  957. if (egid != (gid_t) -1) {
  958. if (egid != current->egid) {
  959. current->mm->dumpable = suid_dumpable;
  960. smp_wmb();
  961. }
  962. current->egid = egid;
  963. }
  964. current->fsgid = current->egid;
  965. if (rgid != (gid_t) -1)
  966. current->gid = rgid;
  967. if (sgid != (gid_t) -1)
  968. current->sgid = sgid;
  969. key_fsgid_changed(current);
  970. proc_id_connector(current, PROC_EVENT_GID);
  971. return 0;
  972. }
  973. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  974. {
  975. int retval;
  976. if (!(retval = put_user(current->gid, rgid)) &&
  977. !(retval = put_user(current->egid, egid)))
  978. retval = put_user(current->sgid, sgid);
  979. return retval;
  980. }
  981. /*
  982. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  983. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  984. * whatever uid it wants to). It normally shadows "euid", except when
  985. * explicitly set by setfsuid() or for access..
  986. */
  987. asmlinkage long sys_setfsuid(uid_t uid)
  988. {
  989. int old_fsuid;
  990. old_fsuid = current->fsuid;
  991. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  992. return old_fsuid;
  993. if (uid == current->uid || uid == current->euid ||
  994. uid == current->suid || uid == current->fsuid ||
  995. capable(CAP_SETUID)) {
  996. if (uid != old_fsuid) {
  997. current->mm->dumpable = suid_dumpable;
  998. smp_wmb();
  999. }
  1000. current->fsuid = uid;
  1001. }
  1002. key_fsuid_changed(current);
  1003. proc_id_connector(current, PROC_EVENT_UID);
  1004. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1005. return old_fsuid;
  1006. }
  1007. /*
  1008. * Samma på svenska..
  1009. */
  1010. asmlinkage long sys_setfsgid(gid_t gid)
  1011. {
  1012. int old_fsgid;
  1013. old_fsgid = current->fsgid;
  1014. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1015. return old_fsgid;
  1016. if (gid == current->gid || gid == current->egid ||
  1017. gid == current->sgid || gid == current->fsgid ||
  1018. capable(CAP_SETGID)) {
  1019. if (gid != old_fsgid) {
  1020. current->mm->dumpable = suid_dumpable;
  1021. smp_wmb();
  1022. }
  1023. current->fsgid = gid;
  1024. key_fsgid_changed(current);
  1025. proc_id_connector(current, PROC_EVENT_GID);
  1026. }
  1027. return old_fsgid;
  1028. }
  1029. asmlinkage long sys_times(struct tms __user * tbuf)
  1030. {
  1031. /*
  1032. * In the SMP world we might just be unlucky and have one of
  1033. * the times increment as we use it. Since the value is an
  1034. * atomically safe type this is just fine. Conceptually its
  1035. * as if the syscall took an instant longer to occur.
  1036. */
  1037. if (tbuf) {
  1038. struct tms tmp;
  1039. struct task_struct *tsk = current;
  1040. struct task_struct *t;
  1041. cputime_t utime, stime, cutime, cstime;
  1042. spin_lock_irq(&tsk->sighand->siglock);
  1043. utime = tsk->signal->utime;
  1044. stime = tsk->signal->stime;
  1045. t = tsk;
  1046. do {
  1047. utime = cputime_add(utime, t->utime);
  1048. stime = cputime_add(stime, t->stime);
  1049. t = next_thread(t);
  1050. } while (t != tsk);
  1051. cutime = tsk->signal->cutime;
  1052. cstime = tsk->signal->cstime;
  1053. spin_unlock_irq(&tsk->sighand->siglock);
  1054. tmp.tms_utime = cputime_to_clock_t(utime);
  1055. tmp.tms_stime = cputime_to_clock_t(stime);
  1056. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1057. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1058. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1059. return -EFAULT;
  1060. }
  1061. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1062. }
  1063. /*
  1064. * This needs some heavy checking ...
  1065. * I just haven't the stomach for it. I also don't fully
  1066. * understand sessions/pgrp etc. Let somebody who does explain it.
  1067. *
  1068. * OK, I think I have the protection semantics right.... this is really
  1069. * only important on a multi-user system anyway, to make sure one user
  1070. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1071. *
  1072. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1073. * LBT 04.03.94
  1074. */
  1075. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1076. {
  1077. struct task_struct *p;
  1078. struct task_struct *group_leader = current->group_leader;
  1079. int err = -EINVAL;
  1080. if (!pid)
  1081. pid = group_leader->pid;
  1082. if (!pgid)
  1083. pgid = pid;
  1084. if (pgid < 0)
  1085. return -EINVAL;
  1086. /* From this point forward we keep holding onto the tasklist lock
  1087. * so that our parent does not change from under us. -DaveM
  1088. */
  1089. write_lock_irq(&tasklist_lock);
  1090. err = -ESRCH;
  1091. p = find_task_by_pid(pid);
  1092. if (!p)
  1093. goto out;
  1094. err = -EINVAL;
  1095. if (!thread_group_leader(p))
  1096. goto out;
  1097. if (p->real_parent == group_leader) {
  1098. err = -EPERM;
  1099. if (p->signal->session != group_leader->signal->session)
  1100. goto out;
  1101. err = -EACCES;
  1102. if (p->did_exec)
  1103. goto out;
  1104. } else {
  1105. err = -ESRCH;
  1106. if (p != group_leader)
  1107. goto out;
  1108. }
  1109. err = -EPERM;
  1110. if (p->signal->leader)
  1111. goto out;
  1112. if (pgid != pid) {
  1113. struct task_struct *p;
  1114. do_each_task_pid(pgid, PIDTYPE_PGID, p) {
  1115. if (p->signal->session == group_leader->signal->session)
  1116. goto ok_pgid;
  1117. } while_each_task_pid(pgid, PIDTYPE_PGID, p);
  1118. goto out;
  1119. }
  1120. ok_pgid:
  1121. err = security_task_setpgid(p, pgid);
  1122. if (err)
  1123. goto out;
  1124. if (process_group(p) != pgid) {
  1125. detach_pid(p, PIDTYPE_PGID);
  1126. p->signal->pgrp = pgid;
  1127. attach_pid(p, PIDTYPE_PGID, pgid);
  1128. }
  1129. err = 0;
  1130. out:
  1131. /* All paths lead to here, thus we are safe. -DaveM */
  1132. write_unlock_irq(&tasklist_lock);
  1133. return err;
  1134. }
  1135. asmlinkage long sys_getpgid(pid_t pid)
  1136. {
  1137. if (!pid)
  1138. return process_group(current);
  1139. else {
  1140. int retval;
  1141. struct task_struct *p;
  1142. read_lock(&tasklist_lock);
  1143. p = find_task_by_pid(pid);
  1144. retval = -ESRCH;
  1145. if (p) {
  1146. retval = security_task_getpgid(p);
  1147. if (!retval)
  1148. retval = process_group(p);
  1149. }
  1150. read_unlock(&tasklist_lock);
  1151. return retval;
  1152. }
  1153. }
  1154. #ifdef __ARCH_WANT_SYS_GETPGRP
  1155. asmlinkage long sys_getpgrp(void)
  1156. {
  1157. /* SMP - assuming writes are word atomic this is fine */
  1158. return process_group(current);
  1159. }
  1160. #endif
  1161. asmlinkage long sys_getsid(pid_t pid)
  1162. {
  1163. if (!pid)
  1164. return current->signal->session;
  1165. else {
  1166. int retval;
  1167. struct task_struct *p;
  1168. read_lock(&tasklist_lock);
  1169. p = find_task_by_pid(pid);
  1170. retval = -ESRCH;
  1171. if (p) {
  1172. retval = security_task_getsid(p);
  1173. if (!retval)
  1174. retval = p->signal->session;
  1175. }
  1176. read_unlock(&tasklist_lock);
  1177. return retval;
  1178. }
  1179. }
  1180. asmlinkage long sys_setsid(void)
  1181. {
  1182. struct task_struct *group_leader = current->group_leader;
  1183. pid_t session;
  1184. int err = -EPERM;
  1185. mutex_lock(&tty_mutex);
  1186. write_lock_irq(&tasklist_lock);
  1187. /* Fail if I am already a session leader */
  1188. if (group_leader->signal->leader)
  1189. goto out;
  1190. session = group_leader->pid;
  1191. /* Fail if a process group id already exists that equals the
  1192. * proposed session id.
  1193. *
  1194. * Don't check if session id == 1 because kernel threads use this
  1195. * session id and so the check will always fail and make it so
  1196. * init cannot successfully call setsid.
  1197. */
  1198. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1199. goto out;
  1200. group_leader->signal->leader = 1;
  1201. __set_special_pids(session, session);
  1202. group_leader->signal->tty = NULL;
  1203. group_leader->signal->tty_old_pgrp = 0;
  1204. err = process_group(group_leader);
  1205. out:
  1206. write_unlock_irq(&tasklist_lock);
  1207. mutex_unlock(&tty_mutex);
  1208. return err;
  1209. }
  1210. /*
  1211. * Supplementary group IDs
  1212. */
  1213. /* init to 2 - one for init_task, one to ensure it is never freed */
  1214. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1215. struct group_info *groups_alloc(int gidsetsize)
  1216. {
  1217. struct group_info *group_info;
  1218. int nblocks;
  1219. int i;
  1220. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1221. /* Make sure we always allocate at least one indirect block pointer */
  1222. nblocks = nblocks ? : 1;
  1223. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1224. if (!group_info)
  1225. return NULL;
  1226. group_info->ngroups = gidsetsize;
  1227. group_info->nblocks = nblocks;
  1228. atomic_set(&group_info->usage, 1);
  1229. if (gidsetsize <= NGROUPS_SMALL)
  1230. group_info->blocks[0] = group_info->small_block;
  1231. else {
  1232. for (i = 0; i < nblocks; i++) {
  1233. gid_t *b;
  1234. b = (void *)__get_free_page(GFP_USER);
  1235. if (!b)
  1236. goto out_undo_partial_alloc;
  1237. group_info->blocks[i] = b;
  1238. }
  1239. }
  1240. return group_info;
  1241. out_undo_partial_alloc:
  1242. while (--i >= 0) {
  1243. free_page((unsigned long)group_info->blocks[i]);
  1244. }
  1245. kfree(group_info);
  1246. return NULL;
  1247. }
  1248. EXPORT_SYMBOL(groups_alloc);
  1249. void groups_free(struct group_info *group_info)
  1250. {
  1251. if (group_info->blocks[0] != group_info->small_block) {
  1252. int i;
  1253. for (i = 0; i < group_info->nblocks; i++)
  1254. free_page((unsigned long)group_info->blocks[i]);
  1255. }
  1256. kfree(group_info);
  1257. }
  1258. EXPORT_SYMBOL(groups_free);
  1259. /* export the group_info to a user-space array */
  1260. static int groups_to_user(gid_t __user *grouplist,
  1261. struct group_info *group_info)
  1262. {
  1263. int i;
  1264. int count = group_info->ngroups;
  1265. for (i = 0; i < group_info->nblocks; i++) {
  1266. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1267. int off = i * NGROUPS_PER_BLOCK;
  1268. int len = cp_count * sizeof(*grouplist);
  1269. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1270. return -EFAULT;
  1271. count -= cp_count;
  1272. }
  1273. return 0;
  1274. }
  1275. /* fill a group_info from a user-space array - it must be allocated already */
  1276. static int groups_from_user(struct group_info *group_info,
  1277. gid_t __user *grouplist)
  1278. {
  1279. int i;
  1280. int count = group_info->ngroups;
  1281. for (i = 0; i < group_info->nblocks; i++) {
  1282. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1283. int off = i * NGROUPS_PER_BLOCK;
  1284. int len = cp_count * sizeof(*grouplist);
  1285. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1286. return -EFAULT;
  1287. count -= cp_count;
  1288. }
  1289. return 0;
  1290. }
  1291. /* a simple Shell sort */
  1292. static void groups_sort(struct group_info *group_info)
  1293. {
  1294. int base, max, stride;
  1295. int gidsetsize = group_info->ngroups;
  1296. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1297. ; /* nothing */
  1298. stride /= 3;
  1299. while (stride) {
  1300. max = gidsetsize - stride;
  1301. for (base = 0; base < max; base++) {
  1302. int left = base;
  1303. int right = left + stride;
  1304. gid_t tmp = GROUP_AT(group_info, right);
  1305. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1306. GROUP_AT(group_info, right) =
  1307. GROUP_AT(group_info, left);
  1308. right = left;
  1309. left -= stride;
  1310. }
  1311. GROUP_AT(group_info, right) = tmp;
  1312. }
  1313. stride /= 3;
  1314. }
  1315. }
  1316. /* a simple bsearch */
  1317. int groups_search(struct group_info *group_info, gid_t grp)
  1318. {
  1319. unsigned int left, right;
  1320. if (!group_info)
  1321. return 0;
  1322. left = 0;
  1323. right = group_info->ngroups;
  1324. while (left < right) {
  1325. unsigned int mid = (left+right)/2;
  1326. int cmp = grp - GROUP_AT(group_info, mid);
  1327. if (cmp > 0)
  1328. left = mid + 1;
  1329. else if (cmp < 0)
  1330. right = mid;
  1331. else
  1332. return 1;
  1333. }
  1334. return 0;
  1335. }
  1336. /* validate and set current->group_info */
  1337. int set_current_groups(struct group_info *group_info)
  1338. {
  1339. int retval;
  1340. struct group_info *old_info;
  1341. retval = security_task_setgroups(group_info);
  1342. if (retval)
  1343. return retval;
  1344. groups_sort(group_info);
  1345. get_group_info(group_info);
  1346. task_lock(current);
  1347. old_info = current->group_info;
  1348. current->group_info = group_info;
  1349. task_unlock(current);
  1350. put_group_info(old_info);
  1351. return 0;
  1352. }
  1353. EXPORT_SYMBOL(set_current_groups);
  1354. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1355. {
  1356. int i = 0;
  1357. /*
  1358. * SMP: Nobody else can change our grouplist. Thus we are
  1359. * safe.
  1360. */
  1361. if (gidsetsize < 0)
  1362. return -EINVAL;
  1363. /* no need to grab task_lock here; it cannot change */
  1364. i = current->group_info->ngroups;
  1365. if (gidsetsize) {
  1366. if (i > gidsetsize) {
  1367. i = -EINVAL;
  1368. goto out;
  1369. }
  1370. if (groups_to_user(grouplist, current->group_info)) {
  1371. i = -EFAULT;
  1372. goto out;
  1373. }
  1374. }
  1375. out:
  1376. return i;
  1377. }
  1378. /*
  1379. * SMP: Our groups are copy-on-write. We can set them safely
  1380. * without another task interfering.
  1381. */
  1382. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1383. {
  1384. struct group_info *group_info;
  1385. int retval;
  1386. if (!capable(CAP_SETGID))
  1387. return -EPERM;
  1388. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1389. return -EINVAL;
  1390. group_info = groups_alloc(gidsetsize);
  1391. if (!group_info)
  1392. return -ENOMEM;
  1393. retval = groups_from_user(group_info, grouplist);
  1394. if (retval) {
  1395. put_group_info(group_info);
  1396. return retval;
  1397. }
  1398. retval = set_current_groups(group_info);
  1399. put_group_info(group_info);
  1400. return retval;
  1401. }
  1402. /*
  1403. * Check whether we're fsgid/egid or in the supplemental group..
  1404. */
  1405. int in_group_p(gid_t grp)
  1406. {
  1407. int retval = 1;
  1408. if (grp != current->fsgid)
  1409. retval = groups_search(current->group_info, grp);
  1410. return retval;
  1411. }
  1412. EXPORT_SYMBOL(in_group_p);
  1413. int in_egroup_p(gid_t grp)
  1414. {
  1415. int retval = 1;
  1416. if (grp != current->egid)
  1417. retval = groups_search(current->group_info, grp);
  1418. return retval;
  1419. }
  1420. EXPORT_SYMBOL(in_egroup_p);
  1421. DECLARE_RWSEM(uts_sem);
  1422. EXPORT_SYMBOL(uts_sem);
  1423. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1424. {
  1425. int errno = 0;
  1426. down_read(&uts_sem);
  1427. if (copy_to_user(name, utsname(), sizeof *name))
  1428. errno = -EFAULT;
  1429. up_read(&uts_sem);
  1430. return errno;
  1431. }
  1432. asmlinkage long sys_sethostname(char __user *name, int len)
  1433. {
  1434. int errno;
  1435. char tmp[__NEW_UTS_LEN];
  1436. if (!capable(CAP_SYS_ADMIN))
  1437. return -EPERM;
  1438. if (len < 0 || len > __NEW_UTS_LEN)
  1439. return -EINVAL;
  1440. down_write(&uts_sem);
  1441. errno = -EFAULT;
  1442. if (!copy_from_user(tmp, name, len)) {
  1443. memcpy(utsname()->nodename, tmp, len);
  1444. utsname()->nodename[len] = 0;
  1445. errno = 0;
  1446. }
  1447. up_write(&uts_sem);
  1448. return errno;
  1449. }
  1450. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1451. asmlinkage long sys_gethostname(char __user *name, int len)
  1452. {
  1453. int i, errno;
  1454. if (len < 0)
  1455. return -EINVAL;
  1456. down_read(&uts_sem);
  1457. i = 1 + strlen(utsname()->nodename);
  1458. if (i > len)
  1459. i = len;
  1460. errno = 0;
  1461. if (copy_to_user(name, utsname()->nodename, i))
  1462. errno = -EFAULT;
  1463. up_read(&uts_sem);
  1464. return errno;
  1465. }
  1466. #endif
  1467. /*
  1468. * Only setdomainname; getdomainname can be implemented by calling
  1469. * uname()
  1470. */
  1471. asmlinkage long sys_setdomainname(char __user *name, int len)
  1472. {
  1473. int errno;
  1474. char tmp[__NEW_UTS_LEN];
  1475. if (!capable(CAP_SYS_ADMIN))
  1476. return -EPERM;
  1477. if (len < 0 || len > __NEW_UTS_LEN)
  1478. return -EINVAL;
  1479. down_write(&uts_sem);
  1480. errno = -EFAULT;
  1481. if (!copy_from_user(tmp, name, len)) {
  1482. memcpy(utsname()->domainname, tmp, len);
  1483. utsname()->domainname[len] = 0;
  1484. errno = 0;
  1485. }
  1486. up_write(&uts_sem);
  1487. return errno;
  1488. }
  1489. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1490. {
  1491. if (resource >= RLIM_NLIMITS)
  1492. return -EINVAL;
  1493. else {
  1494. struct rlimit value;
  1495. task_lock(current->group_leader);
  1496. value = current->signal->rlim[resource];
  1497. task_unlock(current->group_leader);
  1498. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1499. }
  1500. }
  1501. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1502. /*
  1503. * Back compatibility for getrlimit. Needed for some apps.
  1504. */
  1505. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1506. {
  1507. struct rlimit x;
  1508. if (resource >= RLIM_NLIMITS)
  1509. return -EINVAL;
  1510. task_lock(current->group_leader);
  1511. x = current->signal->rlim[resource];
  1512. task_unlock(current->group_leader);
  1513. if (x.rlim_cur > 0x7FFFFFFF)
  1514. x.rlim_cur = 0x7FFFFFFF;
  1515. if (x.rlim_max > 0x7FFFFFFF)
  1516. x.rlim_max = 0x7FFFFFFF;
  1517. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1518. }
  1519. #endif
  1520. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1521. {
  1522. struct rlimit new_rlim, *old_rlim;
  1523. unsigned long it_prof_secs;
  1524. int retval;
  1525. if (resource >= RLIM_NLIMITS)
  1526. return -EINVAL;
  1527. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1528. return -EFAULT;
  1529. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1530. return -EINVAL;
  1531. old_rlim = current->signal->rlim + resource;
  1532. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1533. !capable(CAP_SYS_RESOURCE))
  1534. return -EPERM;
  1535. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1536. return -EPERM;
  1537. retval = security_task_setrlimit(resource, &new_rlim);
  1538. if (retval)
  1539. return retval;
  1540. task_lock(current->group_leader);
  1541. *old_rlim = new_rlim;
  1542. task_unlock(current->group_leader);
  1543. if (resource != RLIMIT_CPU)
  1544. goto out;
  1545. /*
  1546. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1547. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1548. * very long-standing error, and fixing it now risks breakage of
  1549. * applications, so we live with it
  1550. */
  1551. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1552. goto out;
  1553. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1554. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1555. unsigned long rlim_cur = new_rlim.rlim_cur;
  1556. cputime_t cputime;
  1557. if (rlim_cur == 0) {
  1558. /*
  1559. * The caller is asking for an immediate RLIMIT_CPU
  1560. * expiry. But we use the zero value to mean "it was
  1561. * never set". So let's cheat and make it one second
  1562. * instead
  1563. */
  1564. rlim_cur = 1;
  1565. }
  1566. cputime = secs_to_cputime(rlim_cur);
  1567. read_lock(&tasklist_lock);
  1568. spin_lock_irq(&current->sighand->siglock);
  1569. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1570. spin_unlock_irq(&current->sighand->siglock);
  1571. read_unlock(&tasklist_lock);
  1572. }
  1573. out:
  1574. return 0;
  1575. }
  1576. /*
  1577. * It would make sense to put struct rusage in the task_struct,
  1578. * except that would make the task_struct be *really big*. After
  1579. * task_struct gets moved into malloc'ed memory, it would
  1580. * make sense to do this. It will make moving the rest of the information
  1581. * a lot simpler! (Which we're not doing right now because we're not
  1582. * measuring them yet).
  1583. *
  1584. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1585. * races with threads incrementing their own counters. But since word
  1586. * reads are atomic, we either get new values or old values and we don't
  1587. * care which for the sums. We always take the siglock to protect reading
  1588. * the c* fields from p->signal from races with exit.c updating those
  1589. * fields when reaping, so a sample either gets all the additions of a
  1590. * given child after it's reaped, or none so this sample is before reaping.
  1591. *
  1592. * Locking:
  1593. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1594. * for the cases current multithreaded, non-current single threaded
  1595. * non-current multithreaded. Thread traversal is now safe with
  1596. * the siglock held.
  1597. * Strictly speaking, we donot need to take the siglock if we are current and
  1598. * single threaded, as no one else can take our signal_struct away, no one
  1599. * else can reap the children to update signal->c* counters, and no one else
  1600. * can race with the signal-> fields. If we do not take any lock, the
  1601. * signal-> fields could be read out of order while another thread was just
  1602. * exiting. So we should place a read memory barrier when we avoid the lock.
  1603. * On the writer side, write memory barrier is implied in __exit_signal
  1604. * as __exit_signal releases the siglock spinlock after updating the signal->
  1605. * fields. But we don't do this yet to keep things simple.
  1606. *
  1607. */
  1608. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1609. {
  1610. struct task_struct *t;
  1611. unsigned long flags;
  1612. cputime_t utime, stime;
  1613. memset((char *) r, 0, sizeof *r);
  1614. utime = stime = cputime_zero;
  1615. rcu_read_lock();
  1616. if (!lock_task_sighand(p, &flags)) {
  1617. rcu_read_unlock();
  1618. return;
  1619. }
  1620. switch (who) {
  1621. case RUSAGE_BOTH:
  1622. case RUSAGE_CHILDREN:
  1623. utime = p->signal->cutime;
  1624. stime = p->signal->cstime;
  1625. r->ru_nvcsw = p->signal->cnvcsw;
  1626. r->ru_nivcsw = p->signal->cnivcsw;
  1627. r->ru_minflt = p->signal->cmin_flt;
  1628. r->ru_majflt = p->signal->cmaj_flt;
  1629. if (who == RUSAGE_CHILDREN)
  1630. break;
  1631. case RUSAGE_SELF:
  1632. utime = cputime_add(utime, p->signal->utime);
  1633. stime = cputime_add(stime, p->signal->stime);
  1634. r->ru_nvcsw += p->signal->nvcsw;
  1635. r->ru_nivcsw += p->signal->nivcsw;
  1636. r->ru_minflt += p->signal->min_flt;
  1637. r->ru_majflt += p->signal->maj_flt;
  1638. t = p;
  1639. do {
  1640. utime = cputime_add(utime, t->utime);
  1641. stime = cputime_add(stime, t->stime);
  1642. r->ru_nvcsw += t->nvcsw;
  1643. r->ru_nivcsw += t->nivcsw;
  1644. r->ru_minflt += t->min_flt;
  1645. r->ru_majflt += t->maj_flt;
  1646. t = next_thread(t);
  1647. } while (t != p);
  1648. break;
  1649. default:
  1650. BUG();
  1651. }
  1652. unlock_task_sighand(p, &flags);
  1653. rcu_read_unlock();
  1654. cputime_to_timeval(utime, &r->ru_utime);
  1655. cputime_to_timeval(stime, &r->ru_stime);
  1656. }
  1657. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1658. {
  1659. struct rusage r;
  1660. k_getrusage(p, who, &r);
  1661. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1662. }
  1663. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1664. {
  1665. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1666. return -EINVAL;
  1667. return getrusage(current, who, ru);
  1668. }
  1669. asmlinkage long sys_umask(int mask)
  1670. {
  1671. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1672. return mask;
  1673. }
  1674. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1675. unsigned long arg4, unsigned long arg5)
  1676. {
  1677. long error;
  1678. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1679. if (error)
  1680. return error;
  1681. switch (option) {
  1682. case PR_SET_PDEATHSIG:
  1683. if (!valid_signal(arg2)) {
  1684. error = -EINVAL;
  1685. break;
  1686. }
  1687. current->pdeath_signal = arg2;
  1688. break;
  1689. case PR_GET_PDEATHSIG:
  1690. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1691. break;
  1692. case PR_GET_DUMPABLE:
  1693. error = current->mm->dumpable;
  1694. break;
  1695. case PR_SET_DUMPABLE:
  1696. if (arg2 < 0 || arg2 > 1) {
  1697. error = -EINVAL;
  1698. break;
  1699. }
  1700. current->mm->dumpable = arg2;
  1701. break;
  1702. case PR_SET_UNALIGN:
  1703. error = SET_UNALIGN_CTL(current, arg2);
  1704. break;
  1705. case PR_GET_UNALIGN:
  1706. error = GET_UNALIGN_CTL(current, arg2);
  1707. break;
  1708. case PR_SET_FPEMU:
  1709. error = SET_FPEMU_CTL(current, arg2);
  1710. break;
  1711. case PR_GET_FPEMU:
  1712. error = GET_FPEMU_CTL(current, arg2);
  1713. break;
  1714. case PR_SET_FPEXC:
  1715. error = SET_FPEXC_CTL(current, arg2);
  1716. break;
  1717. case PR_GET_FPEXC:
  1718. error = GET_FPEXC_CTL(current, arg2);
  1719. break;
  1720. case PR_GET_TIMING:
  1721. error = PR_TIMING_STATISTICAL;
  1722. break;
  1723. case PR_SET_TIMING:
  1724. if (arg2 == PR_TIMING_STATISTICAL)
  1725. error = 0;
  1726. else
  1727. error = -EINVAL;
  1728. break;
  1729. case PR_GET_KEEPCAPS:
  1730. if (current->keep_capabilities)
  1731. error = 1;
  1732. break;
  1733. case PR_SET_KEEPCAPS:
  1734. if (arg2 != 0 && arg2 != 1) {
  1735. error = -EINVAL;
  1736. break;
  1737. }
  1738. current->keep_capabilities = arg2;
  1739. break;
  1740. case PR_SET_NAME: {
  1741. struct task_struct *me = current;
  1742. unsigned char ncomm[sizeof(me->comm)];
  1743. ncomm[sizeof(me->comm)-1] = 0;
  1744. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1745. sizeof(me->comm)-1) < 0)
  1746. return -EFAULT;
  1747. set_task_comm(me, ncomm);
  1748. return 0;
  1749. }
  1750. case PR_GET_NAME: {
  1751. struct task_struct *me = current;
  1752. unsigned char tcomm[sizeof(me->comm)];
  1753. get_task_comm(tcomm, me);
  1754. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1755. return -EFAULT;
  1756. return 0;
  1757. }
  1758. case PR_GET_ENDIAN:
  1759. error = GET_ENDIAN(current, arg2);
  1760. break;
  1761. case PR_SET_ENDIAN:
  1762. error = SET_ENDIAN(current, arg2);
  1763. break;
  1764. default:
  1765. error = -EINVAL;
  1766. break;
  1767. }
  1768. return error;
  1769. }
  1770. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1771. struct getcpu_cache __user *cache)
  1772. {
  1773. int err = 0;
  1774. int cpu = raw_smp_processor_id();
  1775. if (cpup)
  1776. err |= put_user(cpu, cpup);
  1777. if (nodep)
  1778. err |= put_user(cpu_to_node(cpu), nodep);
  1779. if (cache) {
  1780. /*
  1781. * The cache is not needed for this implementation,
  1782. * but make sure user programs pass something
  1783. * valid. vsyscall implementations can instead make
  1784. * good use of the cache. Only use t0 and t1 because
  1785. * these are available in both 32bit and 64bit ABI (no
  1786. * need for a compat_getcpu). 32bit has enough
  1787. * padding
  1788. */
  1789. unsigned long t0, t1;
  1790. get_user(t0, &cache->blob[0]);
  1791. get_user(t1, &cache->blob[1]);
  1792. t0++;
  1793. t1++;
  1794. put_user(t0, &cache->blob[0]);
  1795. put_user(t1, &cache->blob[1]);
  1796. }
  1797. return err ? -EFAULT : 0;
  1798. }