sys.c 51 KB

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