sys.c 56 KB

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