sys.c 54 KB

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