sys.c 53 KB

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