signal.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  1. /*
  2. * linux/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
  7. *
  8. * 2003-06-02 Jim Houston - Concurrent Computer Corp.
  9. * Changes to use preallocated sigqueue structures
  10. * to allow signals to be sent reliably.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include <linux/fs.h>
  17. #include <linux/tty.h>
  18. #include <linux/binfmts.h>
  19. #include <linux/security.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/signal.h>
  23. #include <linux/signalfd.h>
  24. #include <linux/ratelimit.h>
  25. #include <linux/tracehook.h>
  26. #include <linux/capability.h>
  27. #include <linux/freezer.h>
  28. #include <linux/pid_namespace.h>
  29. #include <linux/nsproxy.h>
  30. #define CREATE_TRACE_POINTS
  31. #include <trace/events/signal.h>
  32. #include <asm/param.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/unistd.h>
  35. #include <asm/siginfo.h>
  36. #include "audit.h" /* audit_signal_info() */
  37. /*
  38. * SLAB caches for signal bits.
  39. */
  40. static struct kmem_cache *sigqueue_cachep;
  41. int print_fatal_signals __read_mostly;
  42. static void __user *sig_handler(struct task_struct *t, int sig)
  43. {
  44. return t->sighand->action[sig - 1].sa.sa_handler;
  45. }
  46. static int sig_handler_ignored(void __user *handler, int sig)
  47. {
  48. /* Is it explicitly or implicitly ignored? */
  49. return handler == SIG_IGN ||
  50. (handler == SIG_DFL && sig_kernel_ignore(sig));
  51. }
  52. static int sig_task_ignored(struct task_struct *t, int sig,
  53. int from_ancestor_ns)
  54. {
  55. void __user *handler;
  56. handler = sig_handler(t, sig);
  57. if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
  58. handler == SIG_DFL && !from_ancestor_ns)
  59. return 1;
  60. return sig_handler_ignored(handler, sig);
  61. }
  62. static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
  63. {
  64. /*
  65. * Blocked signals are never ignored, since the
  66. * signal handler may change by the time it is
  67. * unblocked.
  68. */
  69. if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
  70. return 0;
  71. if (!sig_task_ignored(t, sig, from_ancestor_ns))
  72. return 0;
  73. /*
  74. * Tracers may want to know about even ignored signals.
  75. */
  76. return !tracehook_consider_ignored_signal(t, sig);
  77. }
  78. /*
  79. * Re-calculate pending state from the set of locally pending
  80. * signals, globally pending signals, and blocked signals.
  81. */
  82. static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
  83. {
  84. unsigned long ready;
  85. long i;
  86. switch (_NSIG_WORDS) {
  87. default:
  88. for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
  89. ready |= signal->sig[i] &~ blocked->sig[i];
  90. break;
  91. case 4: ready = signal->sig[3] &~ blocked->sig[3];
  92. ready |= signal->sig[2] &~ blocked->sig[2];
  93. ready |= signal->sig[1] &~ blocked->sig[1];
  94. ready |= signal->sig[0] &~ blocked->sig[0];
  95. break;
  96. case 2: ready = signal->sig[1] &~ blocked->sig[1];
  97. ready |= signal->sig[0] &~ blocked->sig[0];
  98. break;
  99. case 1: ready = signal->sig[0] &~ blocked->sig[0];
  100. }
  101. return ready != 0;
  102. }
  103. #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
  104. static int recalc_sigpending_tsk(struct task_struct *t)
  105. {
  106. if (t->signal->group_stop_count > 0 ||
  107. PENDING(&t->pending, &t->blocked) ||
  108. PENDING(&t->signal->shared_pending, &t->blocked)) {
  109. set_tsk_thread_flag(t, TIF_SIGPENDING);
  110. return 1;
  111. }
  112. /*
  113. * We must never clear the flag in another thread, or in current
  114. * when it's possible the current syscall is returning -ERESTART*.
  115. * So we don't clear it here, and only callers who know they should do.
  116. */
  117. return 0;
  118. }
  119. /*
  120. * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
  121. * This is superfluous when called on current, the wakeup is a harmless no-op.
  122. */
  123. void recalc_sigpending_and_wake(struct task_struct *t)
  124. {
  125. if (recalc_sigpending_tsk(t))
  126. signal_wake_up(t, 0);
  127. }
  128. void recalc_sigpending(void)
  129. {
  130. if (unlikely(tracehook_force_sigpending()))
  131. set_thread_flag(TIF_SIGPENDING);
  132. else if (!recalc_sigpending_tsk(current) && !freezing(current))
  133. clear_thread_flag(TIF_SIGPENDING);
  134. }
  135. /* Given the mask, find the first available signal that should be serviced. */
  136. #define SYNCHRONOUS_MASK \
  137. (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
  138. sigmask(SIGTRAP) | sigmask(SIGFPE))
  139. int next_signal(struct sigpending *pending, sigset_t *mask)
  140. {
  141. unsigned long i, *s, *m, x;
  142. int sig = 0;
  143. s = pending->signal.sig;
  144. m = mask->sig;
  145. /*
  146. * Handle the first word specially: it contains the
  147. * synchronous signals that need to be dequeued first.
  148. */
  149. x = *s &~ *m;
  150. if (x) {
  151. if (x & SYNCHRONOUS_MASK)
  152. x &= SYNCHRONOUS_MASK;
  153. sig = ffz(~x) + 1;
  154. return sig;
  155. }
  156. switch (_NSIG_WORDS) {
  157. default:
  158. for (i = 1; i < _NSIG_WORDS; ++i) {
  159. x = *++s &~ *++m;
  160. if (!x)
  161. continue;
  162. sig = ffz(~x) + i*_NSIG_BPW + 1;
  163. break;
  164. }
  165. break;
  166. case 2:
  167. x = s[1] &~ m[1];
  168. if (!x)
  169. break;
  170. sig = ffz(~x) + _NSIG_BPW + 1;
  171. break;
  172. case 1:
  173. /* Nothing to do */
  174. break;
  175. }
  176. return sig;
  177. }
  178. static inline void print_dropped_signal(int sig)
  179. {
  180. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  181. if (!print_fatal_signals)
  182. return;
  183. if (!__ratelimit(&ratelimit_state))
  184. return;
  185. printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
  186. current->comm, current->pid, sig);
  187. }
  188. /*
  189. * allocate a new signal queue record
  190. * - this may be called without locks if and only if t == current, otherwise an
  191. * appropriate lock must be held to stop the target task from exiting
  192. */
  193. static struct sigqueue *
  194. __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  195. {
  196. struct sigqueue *q = NULL;
  197. struct user_struct *user;
  198. /*
  199. * Protect access to @t credentials. This can go away when all
  200. * callers hold rcu read lock.
  201. */
  202. rcu_read_lock();
  203. user = get_uid(__task_cred(t)->user);
  204. atomic_inc(&user->sigpending);
  205. rcu_read_unlock();
  206. if (override_rlimit ||
  207. atomic_read(&user->sigpending) <=
  208. task_rlimit(t, RLIMIT_SIGPENDING)) {
  209. q = kmem_cache_alloc(sigqueue_cachep, flags);
  210. } else {
  211. print_dropped_signal(sig);
  212. }
  213. if (unlikely(q == NULL)) {
  214. atomic_dec(&user->sigpending);
  215. free_uid(user);
  216. } else {
  217. INIT_LIST_HEAD(&q->list);
  218. q->flags = 0;
  219. q->user = user;
  220. }
  221. return q;
  222. }
  223. static void __sigqueue_free(struct sigqueue *q)
  224. {
  225. if (q->flags & SIGQUEUE_PREALLOC)
  226. return;
  227. atomic_dec(&q->user->sigpending);
  228. free_uid(q->user);
  229. kmem_cache_free(sigqueue_cachep, q);
  230. }
  231. void flush_sigqueue(struct sigpending *queue)
  232. {
  233. struct sigqueue *q;
  234. sigemptyset(&queue->signal);
  235. while (!list_empty(&queue->list)) {
  236. q = list_entry(queue->list.next, struct sigqueue , list);
  237. list_del_init(&q->list);
  238. __sigqueue_free(q);
  239. }
  240. }
  241. /*
  242. * Flush all pending signals for a task.
  243. */
  244. void __flush_signals(struct task_struct *t)
  245. {
  246. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  247. flush_sigqueue(&t->pending);
  248. flush_sigqueue(&t->signal->shared_pending);
  249. }
  250. void flush_signals(struct task_struct *t)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&t->sighand->siglock, flags);
  254. __flush_signals(t);
  255. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  256. }
  257. static void __flush_itimer_signals(struct sigpending *pending)
  258. {
  259. sigset_t signal, retain;
  260. struct sigqueue *q, *n;
  261. signal = pending->signal;
  262. sigemptyset(&retain);
  263. list_for_each_entry_safe(q, n, &pending->list, list) {
  264. int sig = q->info.si_signo;
  265. if (likely(q->info.si_code != SI_TIMER)) {
  266. sigaddset(&retain, sig);
  267. } else {
  268. sigdelset(&signal, sig);
  269. list_del_init(&q->list);
  270. __sigqueue_free(q);
  271. }
  272. }
  273. sigorsets(&pending->signal, &signal, &retain);
  274. }
  275. void flush_itimer_signals(void)
  276. {
  277. struct task_struct *tsk = current;
  278. unsigned long flags;
  279. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  280. __flush_itimer_signals(&tsk->pending);
  281. __flush_itimer_signals(&tsk->signal->shared_pending);
  282. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  283. }
  284. void ignore_signals(struct task_struct *t)
  285. {
  286. int i;
  287. for (i = 0; i < _NSIG; ++i)
  288. t->sighand->action[i].sa.sa_handler = SIG_IGN;
  289. flush_signals(t);
  290. }
  291. /*
  292. * Flush all handlers for a task.
  293. */
  294. void
  295. flush_signal_handlers(struct task_struct *t, int force_default)
  296. {
  297. int i;
  298. struct k_sigaction *ka = &t->sighand->action[0];
  299. for (i = _NSIG ; i != 0 ; i--) {
  300. if (force_default || ka->sa.sa_handler != SIG_IGN)
  301. ka->sa.sa_handler = SIG_DFL;
  302. ka->sa.sa_flags = 0;
  303. sigemptyset(&ka->sa.sa_mask);
  304. ka++;
  305. }
  306. }
  307. int unhandled_signal(struct task_struct *tsk, int sig)
  308. {
  309. void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
  310. if (is_global_init(tsk))
  311. return 1;
  312. if (handler != SIG_IGN && handler != SIG_DFL)
  313. return 0;
  314. return !tracehook_consider_fatal_signal(tsk, sig);
  315. }
  316. /*
  317. * Notify the system that a driver wants to block all signals for this
  318. * process, and wants to be notified if any signals at all were to be
  319. * sent/acted upon. If the notifier routine returns non-zero, then the
  320. * signal will be acted upon after all. If the notifier routine returns 0,
  321. * then then signal will be blocked. Only one block per process is
  322. * allowed. priv is a pointer to private data that the notifier routine
  323. * can use to determine if the signal should be blocked or not.
  324. */
  325. void
  326. block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
  327. {
  328. unsigned long flags;
  329. spin_lock_irqsave(&current->sighand->siglock, flags);
  330. current->notifier_mask = mask;
  331. current->notifier_data = priv;
  332. current->notifier = notifier;
  333. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  334. }
  335. /* Notify the system that blocking has ended. */
  336. void
  337. unblock_all_signals(void)
  338. {
  339. unsigned long flags;
  340. spin_lock_irqsave(&current->sighand->siglock, flags);
  341. current->notifier = NULL;
  342. current->notifier_data = NULL;
  343. recalc_sigpending();
  344. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  345. }
  346. static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  347. {
  348. struct sigqueue *q, *first = NULL;
  349. /*
  350. * Collect the siginfo appropriate to this signal. Check if
  351. * there is another siginfo for the same signal.
  352. */
  353. list_for_each_entry(q, &list->list, list) {
  354. if (q->info.si_signo == sig) {
  355. if (first)
  356. goto still_pending;
  357. first = q;
  358. }
  359. }
  360. sigdelset(&list->signal, sig);
  361. if (first) {
  362. still_pending:
  363. list_del_init(&first->list);
  364. copy_siginfo(info, &first->info);
  365. __sigqueue_free(first);
  366. } else {
  367. /*
  368. * Ok, it wasn't in the queue. This must be
  369. * a fast-pathed signal or we must have been
  370. * out of queue space. So zero out the info.
  371. */
  372. info->si_signo = sig;
  373. info->si_errno = 0;
  374. info->si_code = SI_USER;
  375. info->si_pid = 0;
  376. info->si_uid = 0;
  377. }
  378. }
  379. static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  380. siginfo_t *info)
  381. {
  382. int sig = next_signal(pending, mask);
  383. if (sig) {
  384. if (current->notifier) {
  385. if (sigismember(current->notifier_mask, sig)) {
  386. if (!(current->notifier)(current->notifier_data)) {
  387. clear_thread_flag(TIF_SIGPENDING);
  388. return 0;
  389. }
  390. }
  391. }
  392. collect_signal(sig, pending, info);
  393. }
  394. return sig;
  395. }
  396. /*
  397. * Dequeue a signal and return the element to the caller, which is
  398. * expected to free it.
  399. *
  400. * All callers have to hold the siglock.
  401. */
  402. int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  403. {
  404. int signr;
  405. /* We only dequeue private signals from ourselves, we don't let
  406. * signalfd steal them
  407. */
  408. signr = __dequeue_signal(&tsk->pending, mask, info);
  409. if (!signr) {
  410. signr = __dequeue_signal(&tsk->signal->shared_pending,
  411. mask, info);
  412. /*
  413. * itimer signal ?
  414. *
  415. * itimers are process shared and we restart periodic
  416. * itimers in the signal delivery path to prevent DoS
  417. * attacks in the high resolution timer case. This is
  418. * compliant with the old way of self-restarting
  419. * itimers, as the SIGALRM is a legacy signal and only
  420. * queued once. Changing the restart behaviour to
  421. * restart the timer in the signal dequeue path is
  422. * reducing the timer noise on heavy loaded !highres
  423. * systems too.
  424. */
  425. if (unlikely(signr == SIGALRM)) {
  426. struct hrtimer *tmr = &tsk->signal->real_timer;
  427. if (!hrtimer_is_queued(tmr) &&
  428. tsk->signal->it_real_incr.tv64 != 0) {
  429. hrtimer_forward(tmr, tmr->base->get_time(),
  430. tsk->signal->it_real_incr);
  431. hrtimer_restart(tmr);
  432. }
  433. }
  434. }
  435. recalc_sigpending();
  436. if (!signr)
  437. return 0;
  438. if (unlikely(sig_kernel_stop(signr))) {
  439. /*
  440. * Set a marker that we have dequeued a stop signal. Our
  441. * caller might release the siglock and then the pending
  442. * stop signal it is about to process is no longer in the
  443. * pending bitmasks, but must still be cleared by a SIGCONT
  444. * (and overruled by a SIGKILL). So those cases clear this
  445. * shared flag after we've set it. Note that this flag may
  446. * remain set after the signal we return is ignored or
  447. * handled. That doesn't matter because its only purpose
  448. * is to alert stop-signal processing code when another
  449. * processor has come along and cleared the flag.
  450. */
  451. tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
  452. }
  453. if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
  454. /*
  455. * Release the siglock to ensure proper locking order
  456. * of timer locks outside of siglocks. Note, we leave
  457. * irqs disabled here, since the posix-timers code is
  458. * about to disable them again anyway.
  459. */
  460. spin_unlock(&tsk->sighand->siglock);
  461. do_schedule_next_timer(info);
  462. spin_lock(&tsk->sighand->siglock);
  463. }
  464. return signr;
  465. }
  466. /*
  467. * Tell a process that it has a new active signal..
  468. *
  469. * NOTE! we rely on the previous spin_lock to
  470. * lock interrupts for us! We can only be called with
  471. * "siglock" held, and the local interrupt must
  472. * have been disabled when that got acquired!
  473. *
  474. * No need to set need_resched since signal event passing
  475. * goes through ->blocked
  476. */
  477. void signal_wake_up(struct task_struct *t, int resume)
  478. {
  479. unsigned int mask;
  480. set_tsk_thread_flag(t, TIF_SIGPENDING);
  481. /*
  482. * For SIGKILL, we want to wake it up in the stopped/traced/killable
  483. * case. We don't check t->state here because there is a race with it
  484. * executing another processor and just now entering stopped state.
  485. * By using wake_up_state, we ensure the process will wake up and
  486. * handle its death signal.
  487. */
  488. mask = TASK_INTERRUPTIBLE;
  489. if (resume)
  490. mask |= TASK_WAKEKILL;
  491. if (!wake_up_state(t, mask))
  492. kick_process(t);
  493. }
  494. /*
  495. * Remove signals in mask from the pending set and queue.
  496. * Returns 1 if any signals were found.
  497. *
  498. * All callers must be holding the siglock.
  499. *
  500. * This version takes a sigset mask and looks at all signals,
  501. * not just those in the first mask word.
  502. */
  503. static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
  504. {
  505. struct sigqueue *q, *n;
  506. sigset_t m;
  507. sigandsets(&m, mask, &s->signal);
  508. if (sigisemptyset(&m))
  509. return 0;
  510. signandsets(&s->signal, &s->signal, mask);
  511. list_for_each_entry_safe(q, n, &s->list, list) {
  512. if (sigismember(mask, q->info.si_signo)) {
  513. list_del_init(&q->list);
  514. __sigqueue_free(q);
  515. }
  516. }
  517. return 1;
  518. }
  519. /*
  520. * Remove signals in mask from the pending set and queue.
  521. * Returns 1 if any signals were found.
  522. *
  523. * All callers must be holding the siglock.
  524. */
  525. static int rm_from_queue(unsigned long mask, struct sigpending *s)
  526. {
  527. struct sigqueue *q, *n;
  528. if (!sigtestsetmask(&s->signal, mask))
  529. return 0;
  530. sigdelsetmask(&s->signal, mask);
  531. list_for_each_entry_safe(q, n, &s->list, list) {
  532. if (q->info.si_signo < SIGRTMIN &&
  533. (mask & sigmask(q->info.si_signo))) {
  534. list_del_init(&q->list);
  535. __sigqueue_free(q);
  536. }
  537. }
  538. return 1;
  539. }
  540. static inline int is_si_special(const struct siginfo *info)
  541. {
  542. return info <= SEND_SIG_FORCED;
  543. }
  544. static inline bool si_fromuser(const struct siginfo *info)
  545. {
  546. return info == SEND_SIG_NOINFO ||
  547. (!is_si_special(info) && SI_FROMUSER(info));
  548. }
  549. /*
  550. * called with RCU read lock from check_kill_permission()
  551. */
  552. static int kill_ok_by_cred(struct task_struct *t)
  553. {
  554. const struct cred *cred = current_cred();
  555. const struct cred *tcred = __task_cred(t);
  556. if (cred->user->user_ns == tcred->user->user_ns &&
  557. (cred->euid == tcred->suid ||
  558. cred->euid == tcred->uid ||
  559. cred->uid == tcred->suid ||
  560. cred->uid == tcred->uid))
  561. return 1;
  562. if (ns_capable(tcred->user->user_ns, CAP_KILL))
  563. return 1;
  564. return 0;
  565. }
  566. /*
  567. * Bad permissions for sending the signal
  568. * - the caller must hold the RCU read lock
  569. */
  570. static int check_kill_permission(int sig, struct siginfo *info,
  571. struct task_struct *t)
  572. {
  573. struct pid *sid;
  574. int error;
  575. if (!valid_signal(sig))
  576. return -EINVAL;
  577. if (!si_fromuser(info))
  578. return 0;
  579. error = audit_signal_info(sig, t); /* Let audit system see the signal */
  580. if (error)
  581. return error;
  582. if (!same_thread_group(current, t) &&
  583. !kill_ok_by_cred(t)) {
  584. switch (sig) {
  585. case SIGCONT:
  586. sid = task_session(t);
  587. /*
  588. * We don't return the error if sid == NULL. The
  589. * task was unhashed, the caller must notice this.
  590. */
  591. if (!sid || sid == task_session(current))
  592. break;
  593. default:
  594. return -EPERM;
  595. }
  596. }
  597. return security_task_kill(t, info, sig, 0);
  598. }
  599. /*
  600. * Handle magic process-wide effects of stop/continue signals. Unlike
  601. * the signal actions, these happen immediately at signal-generation
  602. * time regardless of blocking, ignoring, or handling. This does the
  603. * actual continuing for SIGCONT, but not the actual stopping for stop
  604. * signals. The process stop is done as a signal action for SIG_DFL.
  605. *
  606. * Returns true if the signal should be actually delivered, otherwise
  607. * it should be dropped.
  608. */
  609. static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
  610. {
  611. struct signal_struct *signal = p->signal;
  612. struct task_struct *t;
  613. if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
  614. /*
  615. * The process is in the middle of dying, nothing to do.
  616. */
  617. } else if (sig_kernel_stop(sig)) {
  618. /*
  619. * This is a stop signal. Remove SIGCONT from all queues.
  620. */
  621. rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
  622. t = p;
  623. do {
  624. rm_from_queue(sigmask(SIGCONT), &t->pending);
  625. } while_each_thread(p, t);
  626. } else if (sig == SIGCONT) {
  627. unsigned int why;
  628. /*
  629. * Remove all stop signals from all queues,
  630. * and wake all threads.
  631. */
  632. rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
  633. t = p;
  634. do {
  635. unsigned int state;
  636. rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
  637. /*
  638. * If there is a handler for SIGCONT, we must make
  639. * sure that no thread returns to user mode before
  640. * we post the signal, in case it was the only
  641. * thread eligible to run the signal handler--then
  642. * it must not do anything between resuming and
  643. * running the handler. With the TIF_SIGPENDING
  644. * flag set, the thread will pause and acquire the
  645. * siglock that we hold now and until we've queued
  646. * the pending signal.
  647. *
  648. * Wake up the stopped thread _after_ setting
  649. * TIF_SIGPENDING
  650. */
  651. state = __TASK_STOPPED;
  652. if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
  653. set_tsk_thread_flag(t, TIF_SIGPENDING);
  654. state |= TASK_INTERRUPTIBLE;
  655. }
  656. wake_up_state(t, state);
  657. } while_each_thread(p, t);
  658. /*
  659. * Notify the parent with CLD_CONTINUED if we were stopped.
  660. *
  661. * If we were in the middle of a group stop, we pretend it
  662. * was already finished, and then continued. Since SIGCHLD
  663. * doesn't queue we report only CLD_STOPPED, as if the next
  664. * CLD_CONTINUED was dropped.
  665. */
  666. why = 0;
  667. if (signal->flags & SIGNAL_STOP_STOPPED)
  668. why |= SIGNAL_CLD_CONTINUED;
  669. else if (signal->group_stop_count)
  670. why |= SIGNAL_CLD_STOPPED;
  671. if (why) {
  672. /*
  673. * The first thread which returns from do_signal_stop()
  674. * will take ->siglock, notice SIGNAL_CLD_MASK, and
  675. * notify its parent. See get_signal_to_deliver().
  676. */
  677. signal->flags = why | SIGNAL_STOP_CONTINUED;
  678. signal->group_stop_count = 0;
  679. signal->group_exit_code = 0;
  680. } else {
  681. /*
  682. * We are not stopped, but there could be a stop
  683. * signal in the middle of being processed after
  684. * being removed from the queue. Clear that too.
  685. */
  686. signal->flags &= ~SIGNAL_STOP_DEQUEUED;
  687. }
  688. }
  689. return !sig_ignored(p, sig, from_ancestor_ns);
  690. }
  691. /*
  692. * Test if P wants to take SIG. After we've checked all threads with this,
  693. * it's equivalent to finding no threads not blocking SIG. Any threads not
  694. * blocking SIG were ruled out because they are not running and already
  695. * have pending signals. Such threads will dequeue from the shared queue
  696. * as soon as they're available, so putting the signal on the shared queue
  697. * will be equivalent to sending it to one such thread.
  698. */
  699. static inline int wants_signal(int sig, struct task_struct *p)
  700. {
  701. if (sigismember(&p->blocked, sig))
  702. return 0;
  703. if (p->flags & PF_EXITING)
  704. return 0;
  705. if (sig == SIGKILL)
  706. return 1;
  707. if (task_is_stopped_or_traced(p))
  708. return 0;
  709. return task_curr(p) || !signal_pending(p);
  710. }
  711. static void complete_signal(int sig, struct task_struct *p, int group)
  712. {
  713. struct signal_struct *signal = p->signal;
  714. struct task_struct *t;
  715. /*
  716. * Now find a thread we can wake up to take the signal off the queue.
  717. *
  718. * If the main thread wants the signal, it gets first crack.
  719. * Probably the least surprising to the average bear.
  720. */
  721. if (wants_signal(sig, p))
  722. t = p;
  723. else if (!group || thread_group_empty(p))
  724. /*
  725. * There is just one thread and it does not need to be woken.
  726. * It will dequeue unblocked signals before it runs again.
  727. */
  728. return;
  729. else {
  730. /*
  731. * Otherwise try to find a suitable thread.
  732. */
  733. t = signal->curr_target;
  734. while (!wants_signal(sig, t)) {
  735. t = next_thread(t);
  736. if (t == signal->curr_target)
  737. /*
  738. * No thread needs to be woken.
  739. * Any eligible threads will see
  740. * the signal in the queue soon.
  741. */
  742. return;
  743. }
  744. signal->curr_target = t;
  745. }
  746. /*
  747. * Found a killable thread. If the signal will be fatal,
  748. * then start taking the whole group down immediately.
  749. */
  750. if (sig_fatal(p, sig) &&
  751. !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
  752. !sigismember(&t->real_blocked, sig) &&
  753. (sig == SIGKILL ||
  754. !tracehook_consider_fatal_signal(t, sig))) {
  755. /*
  756. * This signal will be fatal to the whole group.
  757. */
  758. if (!sig_kernel_coredump(sig)) {
  759. /*
  760. * Start a group exit and wake everybody up.
  761. * This way we don't have other threads
  762. * running and doing things after a slower
  763. * thread has the fatal signal pending.
  764. */
  765. signal->flags = SIGNAL_GROUP_EXIT;
  766. signal->group_exit_code = sig;
  767. signal->group_stop_count = 0;
  768. t = p;
  769. do {
  770. sigaddset(&t->pending.signal, SIGKILL);
  771. signal_wake_up(t, 1);
  772. } while_each_thread(p, t);
  773. return;
  774. }
  775. }
  776. /*
  777. * The signal is already in the shared-pending queue.
  778. * Tell the chosen thread to wake up and dequeue it.
  779. */
  780. signal_wake_up(t, sig == SIGKILL);
  781. return;
  782. }
  783. static inline int legacy_queue(struct sigpending *signals, int sig)
  784. {
  785. return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
  786. }
  787. static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
  788. int group, int from_ancestor_ns)
  789. {
  790. struct sigpending *pending;
  791. struct sigqueue *q;
  792. int override_rlimit;
  793. trace_signal_generate(sig, info, t);
  794. assert_spin_locked(&t->sighand->siglock);
  795. if (!prepare_signal(sig, t, from_ancestor_ns))
  796. return 0;
  797. pending = group ? &t->signal->shared_pending : &t->pending;
  798. /*
  799. * Short-circuit ignored signals and support queuing
  800. * exactly one non-rt signal, so that we can get more
  801. * detailed information about the cause of the signal.
  802. */
  803. if (legacy_queue(pending, sig))
  804. return 0;
  805. /*
  806. * fast-pathed signals for kernel-internal things like SIGSTOP
  807. * or SIGKILL.
  808. */
  809. if (info == SEND_SIG_FORCED)
  810. goto out_set;
  811. /*
  812. * Real-time signals must be queued if sent by sigqueue, or
  813. * some other real-time mechanism. It is implementation
  814. * defined whether kill() does so. We attempt to do so, on
  815. * the principle of least surprise, but since kill is not
  816. * allowed to fail with EAGAIN when low on memory we just
  817. * make sure at least one signal gets delivered and don't
  818. * pass on the info struct.
  819. */
  820. if (sig < SIGRTMIN)
  821. override_rlimit = (is_si_special(info) || info->si_code >= 0);
  822. else
  823. override_rlimit = 0;
  824. q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
  825. override_rlimit);
  826. if (q) {
  827. list_add_tail(&q->list, &pending->list);
  828. switch ((unsigned long) info) {
  829. case (unsigned long) SEND_SIG_NOINFO:
  830. q->info.si_signo = sig;
  831. q->info.si_errno = 0;
  832. q->info.si_code = SI_USER;
  833. q->info.si_pid = task_tgid_nr_ns(current,
  834. task_active_pid_ns(t));
  835. q->info.si_uid = current_uid();
  836. break;
  837. case (unsigned long) SEND_SIG_PRIV:
  838. q->info.si_signo = sig;
  839. q->info.si_errno = 0;
  840. q->info.si_code = SI_KERNEL;
  841. q->info.si_pid = 0;
  842. q->info.si_uid = 0;
  843. break;
  844. default:
  845. copy_siginfo(&q->info, info);
  846. if (from_ancestor_ns)
  847. q->info.si_pid = 0;
  848. break;
  849. }
  850. } else if (!is_si_special(info)) {
  851. if (sig >= SIGRTMIN && info->si_code != SI_USER) {
  852. /*
  853. * Queue overflow, abort. We may abort if the
  854. * signal was rt and sent by user using something
  855. * other than kill().
  856. */
  857. trace_signal_overflow_fail(sig, group, info);
  858. return -EAGAIN;
  859. } else {
  860. /*
  861. * This is a silent loss of information. We still
  862. * send the signal, but the *info bits are lost.
  863. */
  864. trace_signal_lose_info(sig, group, info);
  865. }
  866. }
  867. out_set:
  868. signalfd_notify(t, sig);
  869. sigaddset(&pending->signal, sig);
  870. complete_signal(sig, t, group);
  871. return 0;
  872. }
  873. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  874. int group)
  875. {
  876. int from_ancestor_ns = 0;
  877. #ifdef CONFIG_PID_NS
  878. from_ancestor_ns = si_fromuser(info) &&
  879. !task_pid_nr_ns(current, task_active_pid_ns(t));
  880. #endif
  881. return __send_signal(sig, info, t, group, from_ancestor_ns);
  882. }
  883. static void print_fatal_signal(struct pt_regs *regs, int signr)
  884. {
  885. printk("%s/%d: potentially unexpected fatal signal %d.\n",
  886. current->comm, task_pid_nr(current), signr);
  887. #if defined(__i386__) && !defined(__arch_um__)
  888. printk("code at %08lx: ", regs->ip);
  889. {
  890. int i;
  891. for (i = 0; i < 16; i++) {
  892. unsigned char insn;
  893. if (get_user(insn, (unsigned char *)(regs->ip + i)))
  894. break;
  895. printk("%02x ", insn);
  896. }
  897. }
  898. #endif
  899. printk("\n");
  900. preempt_disable();
  901. show_regs(regs);
  902. preempt_enable();
  903. }
  904. static int __init setup_print_fatal_signals(char *str)
  905. {
  906. get_option (&str, &print_fatal_signals);
  907. return 1;
  908. }
  909. __setup("print-fatal-signals=", setup_print_fatal_signals);
  910. int
  911. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  912. {
  913. return send_signal(sig, info, p, 1);
  914. }
  915. static int
  916. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  917. {
  918. return send_signal(sig, info, t, 0);
  919. }
  920. int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  921. bool group)
  922. {
  923. unsigned long flags;
  924. int ret = -ESRCH;
  925. if (lock_task_sighand(p, &flags)) {
  926. ret = send_signal(sig, info, p, group);
  927. unlock_task_sighand(p, &flags);
  928. }
  929. return ret;
  930. }
  931. /*
  932. * Force a signal that the process can't ignore: if necessary
  933. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  934. *
  935. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  936. * since we do not want to have a signal handler that was blocked
  937. * be invoked when user space had explicitly blocked it.
  938. *
  939. * We don't want to have recursive SIGSEGV's etc, for example,
  940. * that is why we also clear SIGNAL_UNKILLABLE.
  941. */
  942. int
  943. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  944. {
  945. unsigned long int flags;
  946. int ret, blocked, ignored;
  947. struct k_sigaction *action;
  948. spin_lock_irqsave(&t->sighand->siglock, flags);
  949. action = &t->sighand->action[sig-1];
  950. ignored = action->sa.sa_handler == SIG_IGN;
  951. blocked = sigismember(&t->blocked, sig);
  952. if (blocked || ignored) {
  953. action->sa.sa_handler = SIG_DFL;
  954. if (blocked) {
  955. sigdelset(&t->blocked, sig);
  956. recalc_sigpending_and_wake(t);
  957. }
  958. }
  959. if (action->sa.sa_handler == SIG_DFL)
  960. t->signal->flags &= ~SIGNAL_UNKILLABLE;
  961. ret = specific_send_sig_info(sig, info, t);
  962. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  963. return ret;
  964. }
  965. /*
  966. * Nuke all other threads in the group.
  967. */
  968. int zap_other_threads(struct task_struct *p)
  969. {
  970. struct task_struct *t = p;
  971. int count = 0;
  972. p->signal->group_stop_count = 0;
  973. while_each_thread(p, t) {
  974. count++;
  975. /* Don't bother with already dead threads */
  976. if (t->exit_state)
  977. continue;
  978. sigaddset(&t->pending.signal, SIGKILL);
  979. signal_wake_up(t, 1);
  980. }
  981. return count;
  982. }
  983. struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  984. unsigned long *flags)
  985. {
  986. struct sighand_struct *sighand;
  987. rcu_read_lock();
  988. for (;;) {
  989. sighand = rcu_dereference(tsk->sighand);
  990. if (unlikely(sighand == NULL))
  991. break;
  992. spin_lock_irqsave(&sighand->siglock, *flags);
  993. if (likely(sighand == tsk->sighand))
  994. break;
  995. spin_unlock_irqrestore(&sighand->siglock, *flags);
  996. }
  997. rcu_read_unlock();
  998. return sighand;
  999. }
  1000. /*
  1001. * send signal info to all the members of a group
  1002. */
  1003. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1004. {
  1005. int ret;
  1006. rcu_read_lock();
  1007. ret = check_kill_permission(sig, info, p);
  1008. rcu_read_unlock();
  1009. if (!ret && sig)
  1010. ret = do_send_sig_info(sig, info, p, true);
  1011. return ret;
  1012. }
  1013. /*
  1014. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1015. * control characters do (^C, ^Z etc)
  1016. * - the caller must hold at least a readlock on tasklist_lock
  1017. */
  1018. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1019. {
  1020. struct task_struct *p = NULL;
  1021. int retval, success;
  1022. success = 0;
  1023. retval = -ESRCH;
  1024. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1025. int err = group_send_sig_info(sig, info, p);
  1026. success |= !err;
  1027. retval = err;
  1028. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1029. return success ? 0 : retval;
  1030. }
  1031. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1032. {
  1033. int error = -ESRCH;
  1034. struct task_struct *p;
  1035. rcu_read_lock();
  1036. retry:
  1037. p = pid_task(pid, PIDTYPE_PID);
  1038. if (p) {
  1039. error = group_send_sig_info(sig, info, p);
  1040. if (unlikely(error == -ESRCH))
  1041. /*
  1042. * The task was unhashed in between, try again.
  1043. * If it is dead, pid_task() will return NULL,
  1044. * if we race with de_thread() it will find the
  1045. * new leader.
  1046. */
  1047. goto retry;
  1048. }
  1049. rcu_read_unlock();
  1050. return error;
  1051. }
  1052. int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1053. {
  1054. int error;
  1055. rcu_read_lock();
  1056. error = kill_pid_info(sig, info, find_vpid(pid));
  1057. rcu_read_unlock();
  1058. return error;
  1059. }
  1060. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1061. int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
  1062. uid_t uid, uid_t euid, u32 secid)
  1063. {
  1064. int ret = -EINVAL;
  1065. struct task_struct *p;
  1066. const struct cred *pcred;
  1067. unsigned long flags;
  1068. if (!valid_signal(sig))
  1069. return ret;
  1070. rcu_read_lock();
  1071. p = pid_task(pid, PIDTYPE_PID);
  1072. if (!p) {
  1073. ret = -ESRCH;
  1074. goto out_unlock;
  1075. }
  1076. pcred = __task_cred(p);
  1077. if (si_fromuser(info) &&
  1078. euid != pcred->suid && euid != pcred->uid &&
  1079. uid != pcred->suid && uid != pcred->uid) {
  1080. ret = -EPERM;
  1081. goto out_unlock;
  1082. }
  1083. ret = security_task_kill(p, info, sig, secid);
  1084. if (ret)
  1085. goto out_unlock;
  1086. if (sig) {
  1087. if (lock_task_sighand(p, &flags)) {
  1088. ret = __send_signal(sig, info, p, 1, 0);
  1089. unlock_task_sighand(p, &flags);
  1090. } else
  1091. ret = -ESRCH;
  1092. }
  1093. out_unlock:
  1094. rcu_read_unlock();
  1095. return ret;
  1096. }
  1097. EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
  1098. /*
  1099. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1100. *
  1101. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1102. * is probably wrong. Should make it like BSD or SYSV.
  1103. */
  1104. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1105. {
  1106. int ret;
  1107. if (pid > 0) {
  1108. rcu_read_lock();
  1109. ret = kill_pid_info(sig, info, find_vpid(pid));
  1110. rcu_read_unlock();
  1111. return ret;
  1112. }
  1113. read_lock(&tasklist_lock);
  1114. if (pid != -1) {
  1115. ret = __kill_pgrp_info(sig, info,
  1116. pid ? find_vpid(-pid) : task_pgrp(current));
  1117. } else {
  1118. int retval = 0, count = 0;
  1119. struct task_struct * p;
  1120. for_each_process(p) {
  1121. if (task_pid_vnr(p) > 1 &&
  1122. !same_thread_group(p, current)) {
  1123. int err = group_send_sig_info(sig, info, p);
  1124. ++count;
  1125. if (err != -EPERM)
  1126. retval = err;
  1127. }
  1128. }
  1129. ret = count ? retval : -ESRCH;
  1130. }
  1131. read_unlock(&tasklist_lock);
  1132. return ret;
  1133. }
  1134. /*
  1135. * These are for backward compatibility with the rest of the kernel source.
  1136. */
  1137. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1138. {
  1139. /*
  1140. * Make sure legacy kernel users don't send in bad values
  1141. * (normal paths check this in check_kill_permission).
  1142. */
  1143. if (!valid_signal(sig))
  1144. return -EINVAL;
  1145. return do_send_sig_info(sig, info, p, false);
  1146. }
  1147. #define __si_special(priv) \
  1148. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1149. int
  1150. send_sig(int sig, struct task_struct *p, int priv)
  1151. {
  1152. return send_sig_info(sig, __si_special(priv), p);
  1153. }
  1154. void
  1155. force_sig(int sig, struct task_struct *p)
  1156. {
  1157. force_sig_info(sig, SEND_SIG_PRIV, p);
  1158. }
  1159. /*
  1160. * When things go south during signal handling, we
  1161. * will force a SIGSEGV. And if the signal that caused
  1162. * the problem was already a SIGSEGV, we'll want to
  1163. * make sure we don't even try to deliver the signal..
  1164. */
  1165. int
  1166. force_sigsegv(int sig, struct task_struct *p)
  1167. {
  1168. if (sig == SIGSEGV) {
  1169. unsigned long flags;
  1170. spin_lock_irqsave(&p->sighand->siglock, flags);
  1171. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1172. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1173. }
  1174. force_sig(SIGSEGV, p);
  1175. return 0;
  1176. }
  1177. int kill_pgrp(struct pid *pid, int sig, int priv)
  1178. {
  1179. int ret;
  1180. read_lock(&tasklist_lock);
  1181. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1182. read_unlock(&tasklist_lock);
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL(kill_pgrp);
  1186. int kill_pid(struct pid *pid, int sig, int priv)
  1187. {
  1188. return kill_pid_info(sig, __si_special(priv), pid);
  1189. }
  1190. EXPORT_SYMBOL(kill_pid);
  1191. /*
  1192. * These functions support sending signals using preallocated sigqueue
  1193. * structures. This is needed "because realtime applications cannot
  1194. * afford to lose notifications of asynchronous events, like timer
  1195. * expirations or I/O completions". In the case of POSIX Timers
  1196. * we allocate the sigqueue structure from the timer_create. If this
  1197. * allocation fails we are able to report the failure to the application
  1198. * with an EAGAIN error.
  1199. */
  1200. struct sigqueue *sigqueue_alloc(void)
  1201. {
  1202. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1203. if (q)
  1204. q->flags |= SIGQUEUE_PREALLOC;
  1205. return q;
  1206. }
  1207. void sigqueue_free(struct sigqueue *q)
  1208. {
  1209. unsigned long flags;
  1210. spinlock_t *lock = &current->sighand->siglock;
  1211. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1212. /*
  1213. * We must hold ->siglock while testing q->list
  1214. * to serialize with collect_signal() or with
  1215. * __exit_signal()->flush_sigqueue().
  1216. */
  1217. spin_lock_irqsave(lock, flags);
  1218. q->flags &= ~SIGQUEUE_PREALLOC;
  1219. /*
  1220. * If it is queued it will be freed when dequeued,
  1221. * like the "regular" sigqueue.
  1222. */
  1223. if (!list_empty(&q->list))
  1224. q = NULL;
  1225. spin_unlock_irqrestore(lock, flags);
  1226. if (q)
  1227. __sigqueue_free(q);
  1228. }
  1229. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  1230. {
  1231. int sig = q->info.si_signo;
  1232. struct sigpending *pending;
  1233. unsigned long flags;
  1234. int ret;
  1235. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1236. ret = -1;
  1237. if (!likely(lock_task_sighand(t, &flags)))
  1238. goto ret;
  1239. ret = 1; /* the signal is ignored */
  1240. if (!prepare_signal(sig, t, 0))
  1241. goto out;
  1242. ret = 0;
  1243. if (unlikely(!list_empty(&q->list))) {
  1244. /*
  1245. * If an SI_TIMER entry is already queue just increment
  1246. * the overrun count.
  1247. */
  1248. BUG_ON(q->info.si_code != SI_TIMER);
  1249. q->info.si_overrun++;
  1250. goto out;
  1251. }
  1252. q->info.si_overrun = 0;
  1253. signalfd_notify(t, sig);
  1254. pending = group ? &t->signal->shared_pending : &t->pending;
  1255. list_add_tail(&q->list, &pending->list);
  1256. sigaddset(&pending->signal, sig);
  1257. complete_signal(sig, t, group);
  1258. out:
  1259. unlock_task_sighand(t, &flags);
  1260. ret:
  1261. return ret;
  1262. }
  1263. /*
  1264. * Let a parent know about the death of a child.
  1265. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1266. *
  1267. * Returns -1 if our parent ignored us and so we've switched to
  1268. * self-reaping, or else @sig.
  1269. */
  1270. int do_notify_parent(struct task_struct *tsk, int sig)
  1271. {
  1272. struct siginfo info;
  1273. unsigned long flags;
  1274. struct sighand_struct *psig;
  1275. int ret = sig;
  1276. BUG_ON(sig == -1);
  1277. /* do_notify_parent_cldstop should have been called instead. */
  1278. BUG_ON(task_is_stopped_or_traced(tsk));
  1279. BUG_ON(!task_ptrace(tsk) &&
  1280. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1281. info.si_signo = sig;
  1282. info.si_errno = 0;
  1283. /*
  1284. * we are under tasklist_lock here so our parent is tied to
  1285. * us and cannot exit and release its namespace.
  1286. *
  1287. * the only it can is to switch its nsproxy with sys_unshare,
  1288. * bu uncharing pid namespaces is not allowed, so we'll always
  1289. * see relevant namespace
  1290. *
  1291. * write_lock() currently calls preempt_disable() which is the
  1292. * same as rcu_read_lock(), but according to Oleg, this is not
  1293. * correct to rely on this
  1294. */
  1295. rcu_read_lock();
  1296. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1297. info.si_uid = __task_cred(tsk)->uid;
  1298. rcu_read_unlock();
  1299. info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
  1300. tsk->signal->utime));
  1301. info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
  1302. tsk->signal->stime));
  1303. info.si_status = tsk->exit_code & 0x7f;
  1304. if (tsk->exit_code & 0x80)
  1305. info.si_code = CLD_DUMPED;
  1306. else if (tsk->exit_code & 0x7f)
  1307. info.si_code = CLD_KILLED;
  1308. else {
  1309. info.si_code = CLD_EXITED;
  1310. info.si_status = tsk->exit_code >> 8;
  1311. }
  1312. psig = tsk->parent->sighand;
  1313. spin_lock_irqsave(&psig->siglock, flags);
  1314. if (!task_ptrace(tsk) && sig == SIGCHLD &&
  1315. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1316. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1317. /*
  1318. * We are exiting and our parent doesn't care. POSIX.1
  1319. * defines special semantics for setting SIGCHLD to SIG_IGN
  1320. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1321. * automatically and not left for our parent's wait4 call.
  1322. * Rather than having the parent do it as a magic kind of
  1323. * signal handler, we just set this to tell do_exit that we
  1324. * can be cleaned up without becoming a zombie. Note that
  1325. * we still call __wake_up_parent in this case, because a
  1326. * blocked sys_wait4 might now return -ECHILD.
  1327. *
  1328. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1329. * is implementation-defined: we do (if you don't want
  1330. * it, just use SIG_IGN instead).
  1331. */
  1332. ret = tsk->exit_signal = -1;
  1333. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1334. sig = -1;
  1335. }
  1336. if (valid_signal(sig) && sig > 0)
  1337. __group_send_sig_info(sig, &info, tsk->parent);
  1338. __wake_up_parent(tsk, tsk->parent);
  1339. spin_unlock_irqrestore(&psig->siglock, flags);
  1340. return ret;
  1341. }
  1342. static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
  1343. {
  1344. struct siginfo info;
  1345. unsigned long flags;
  1346. struct task_struct *parent;
  1347. struct sighand_struct *sighand;
  1348. if (task_ptrace(tsk))
  1349. parent = tsk->parent;
  1350. else {
  1351. tsk = tsk->group_leader;
  1352. parent = tsk->real_parent;
  1353. }
  1354. info.si_signo = SIGCHLD;
  1355. info.si_errno = 0;
  1356. /*
  1357. * see comment in do_notify_parent() about the following 4 lines
  1358. */
  1359. rcu_read_lock();
  1360. info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
  1361. info.si_uid = __task_cred(tsk)->uid;
  1362. rcu_read_unlock();
  1363. info.si_utime = cputime_to_clock_t(tsk->utime);
  1364. info.si_stime = cputime_to_clock_t(tsk->stime);
  1365. info.si_code = why;
  1366. switch (why) {
  1367. case CLD_CONTINUED:
  1368. info.si_status = SIGCONT;
  1369. break;
  1370. case CLD_STOPPED:
  1371. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1372. break;
  1373. case CLD_TRAPPED:
  1374. info.si_status = tsk->exit_code & 0x7f;
  1375. break;
  1376. default:
  1377. BUG();
  1378. }
  1379. sighand = parent->sighand;
  1380. spin_lock_irqsave(&sighand->siglock, flags);
  1381. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1382. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1383. __group_send_sig_info(SIGCHLD, &info, parent);
  1384. /*
  1385. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1386. */
  1387. __wake_up_parent(tsk, parent);
  1388. spin_unlock_irqrestore(&sighand->siglock, flags);
  1389. }
  1390. static inline int may_ptrace_stop(void)
  1391. {
  1392. if (!likely(task_ptrace(current)))
  1393. return 0;
  1394. /*
  1395. * Are we in the middle of do_coredump?
  1396. * If so and our tracer is also part of the coredump stopping
  1397. * is a deadlock situation, and pointless because our tracer
  1398. * is dead so don't allow us to stop.
  1399. * If SIGKILL was already sent before the caller unlocked
  1400. * ->siglock we must see ->core_state != NULL. Otherwise it
  1401. * is safe to enter schedule().
  1402. */
  1403. if (unlikely(current->mm->core_state) &&
  1404. unlikely(current->mm == current->parent->mm))
  1405. return 0;
  1406. return 1;
  1407. }
  1408. /*
  1409. * Return non-zero if there is a SIGKILL that should be waking us up.
  1410. * Called with the siglock held.
  1411. */
  1412. static int sigkill_pending(struct task_struct *tsk)
  1413. {
  1414. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1415. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1416. }
  1417. /*
  1418. * This must be called with current->sighand->siglock held.
  1419. *
  1420. * This should be the path for all ptrace stops.
  1421. * We always set current->last_siginfo while stopped here.
  1422. * That makes it a way to test a stopped process for
  1423. * being ptrace-stopped vs being job-control-stopped.
  1424. *
  1425. * If we actually decide not to stop at all because the tracer
  1426. * is gone, we keep current->exit_code unless clear_code.
  1427. */
  1428. static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
  1429. __releases(&current->sighand->siglock)
  1430. __acquires(&current->sighand->siglock)
  1431. {
  1432. if (arch_ptrace_stop_needed(exit_code, info)) {
  1433. /*
  1434. * The arch code has something special to do before a
  1435. * ptrace stop. This is allowed to block, e.g. for faults
  1436. * on user stack pages. We can't keep the siglock while
  1437. * calling arch_ptrace_stop, so we must release it now.
  1438. * To preserve proper semantics, we must do this before
  1439. * any signal bookkeeping like checking group_stop_count.
  1440. * Meanwhile, a SIGKILL could come in before we retake the
  1441. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1442. * So after regaining the lock, we must check for SIGKILL.
  1443. */
  1444. spin_unlock_irq(&current->sighand->siglock);
  1445. arch_ptrace_stop(exit_code, info);
  1446. spin_lock_irq(&current->sighand->siglock);
  1447. if (sigkill_pending(current))
  1448. return;
  1449. }
  1450. /*
  1451. * If there is a group stop in progress,
  1452. * we must participate in the bookkeeping.
  1453. */
  1454. if (current->signal->group_stop_count > 0)
  1455. --current->signal->group_stop_count;
  1456. current->last_siginfo = info;
  1457. current->exit_code = exit_code;
  1458. /* Let the debugger run. */
  1459. __set_current_state(TASK_TRACED);
  1460. spin_unlock_irq(&current->sighand->siglock);
  1461. read_lock(&tasklist_lock);
  1462. if (may_ptrace_stop()) {
  1463. do_notify_parent_cldstop(current, CLD_TRAPPED);
  1464. /*
  1465. * Don't want to allow preemption here, because
  1466. * sys_ptrace() needs this task to be inactive.
  1467. *
  1468. * XXX: implement read_unlock_no_resched().
  1469. */
  1470. preempt_disable();
  1471. read_unlock(&tasklist_lock);
  1472. preempt_enable_no_resched();
  1473. schedule();
  1474. } else {
  1475. /*
  1476. * By the time we got the lock, our tracer went away.
  1477. * Don't drop the lock yet, another tracer may come.
  1478. */
  1479. __set_current_state(TASK_RUNNING);
  1480. if (clear_code)
  1481. current->exit_code = 0;
  1482. read_unlock(&tasklist_lock);
  1483. }
  1484. /*
  1485. * While in TASK_TRACED, we were considered "frozen enough".
  1486. * Now that we woke up, it's crucial if we're supposed to be
  1487. * frozen that we freeze now before running anything substantial.
  1488. */
  1489. try_to_freeze();
  1490. /*
  1491. * We are back. Now reacquire the siglock before touching
  1492. * last_siginfo, so that we are sure to have synchronized with
  1493. * any signal-sending on another CPU that wants to examine it.
  1494. */
  1495. spin_lock_irq(&current->sighand->siglock);
  1496. current->last_siginfo = NULL;
  1497. /*
  1498. * Queued signals ignored us while we were stopped for tracing.
  1499. * So check for any that we should take before resuming user mode.
  1500. * This sets TIF_SIGPENDING, but never clears it.
  1501. */
  1502. recalc_sigpending_tsk(current);
  1503. }
  1504. void ptrace_notify(int exit_code)
  1505. {
  1506. siginfo_t info;
  1507. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1508. memset(&info, 0, sizeof info);
  1509. info.si_signo = SIGTRAP;
  1510. info.si_code = exit_code;
  1511. info.si_pid = task_pid_vnr(current);
  1512. info.si_uid = current_uid();
  1513. /* Let the debugger run. */
  1514. spin_lock_irq(&current->sighand->siglock);
  1515. ptrace_stop(exit_code, 1, &info);
  1516. spin_unlock_irq(&current->sighand->siglock);
  1517. }
  1518. /*
  1519. * This performs the stopping for SIGSTOP and other stop signals.
  1520. * We have to stop all threads in the thread group.
  1521. * Returns non-zero if we've actually stopped and released the siglock.
  1522. * Returns zero if we didn't stop and still hold the siglock.
  1523. */
  1524. static int do_signal_stop(int signr)
  1525. {
  1526. struct signal_struct *sig = current->signal;
  1527. int notify;
  1528. if (!sig->group_stop_count) {
  1529. struct task_struct *t;
  1530. if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
  1531. unlikely(signal_group_exit(sig)))
  1532. return 0;
  1533. /*
  1534. * There is no group stop already in progress.
  1535. * We must initiate one now.
  1536. */
  1537. sig->group_exit_code = signr;
  1538. sig->group_stop_count = 1;
  1539. for (t = next_thread(current); t != current; t = next_thread(t))
  1540. /*
  1541. * Setting state to TASK_STOPPED for a group
  1542. * stop is always done with the siglock held,
  1543. * so this check has no races.
  1544. */
  1545. if (!(t->flags & PF_EXITING) &&
  1546. !task_is_stopped_or_traced(t)) {
  1547. sig->group_stop_count++;
  1548. signal_wake_up(t, 0);
  1549. }
  1550. }
  1551. /*
  1552. * If there are no other threads in the group, or if there is
  1553. * a group stop in progress and we are the last to stop, report
  1554. * to the parent. When ptraced, every thread reports itself.
  1555. */
  1556. notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
  1557. notify = tracehook_notify_jctl(notify, CLD_STOPPED);
  1558. /*
  1559. * tracehook_notify_jctl() can drop and reacquire siglock, so
  1560. * we keep ->group_stop_count != 0 before the call. If SIGCONT
  1561. * or SIGKILL comes in between ->group_stop_count == 0.
  1562. */
  1563. if (sig->group_stop_count) {
  1564. if (!--sig->group_stop_count)
  1565. sig->flags = SIGNAL_STOP_STOPPED;
  1566. current->exit_code = sig->group_exit_code;
  1567. __set_current_state(TASK_STOPPED);
  1568. }
  1569. spin_unlock_irq(&current->sighand->siglock);
  1570. if (notify) {
  1571. read_lock(&tasklist_lock);
  1572. do_notify_parent_cldstop(current, notify);
  1573. read_unlock(&tasklist_lock);
  1574. }
  1575. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1576. do {
  1577. schedule();
  1578. } while (try_to_freeze());
  1579. tracehook_finish_jctl();
  1580. current->exit_code = 0;
  1581. return 1;
  1582. }
  1583. static int ptrace_signal(int signr, siginfo_t *info,
  1584. struct pt_regs *regs, void *cookie)
  1585. {
  1586. if (!task_ptrace(current))
  1587. return signr;
  1588. ptrace_signal_deliver(regs, cookie);
  1589. /* Let the debugger run. */
  1590. ptrace_stop(signr, 0, info);
  1591. /* We're back. Did the debugger cancel the sig? */
  1592. signr = current->exit_code;
  1593. if (signr == 0)
  1594. return signr;
  1595. current->exit_code = 0;
  1596. /*
  1597. * Update the siginfo structure if the signal has
  1598. * changed. If the debugger wanted something
  1599. * specific in the siginfo structure then it should
  1600. * have updated *info via PTRACE_SETSIGINFO.
  1601. */
  1602. if (signr != info->si_signo) {
  1603. info->si_signo = signr;
  1604. info->si_errno = 0;
  1605. info->si_code = SI_USER;
  1606. info->si_pid = task_pid_vnr(current->parent);
  1607. info->si_uid = task_uid(current->parent);
  1608. }
  1609. /* If the (new) signal is now blocked, requeue it. */
  1610. if (sigismember(&current->blocked, signr)) {
  1611. specific_send_sig_info(signr, info, current);
  1612. signr = 0;
  1613. }
  1614. return signr;
  1615. }
  1616. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1617. struct pt_regs *regs, void *cookie)
  1618. {
  1619. struct sighand_struct *sighand = current->sighand;
  1620. struct signal_struct *signal = current->signal;
  1621. int signr;
  1622. relock:
  1623. /*
  1624. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1625. * While in TASK_STOPPED, we were considered "frozen enough".
  1626. * Now that we woke up, it's crucial if we're supposed to be
  1627. * frozen that we freeze now before running anything substantial.
  1628. */
  1629. try_to_freeze();
  1630. spin_lock_irq(&sighand->siglock);
  1631. /*
  1632. * Every stopped thread goes here after wakeup. Check to see if
  1633. * we should notify the parent, prepare_signal(SIGCONT) encodes
  1634. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  1635. */
  1636. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1637. int why = (signal->flags & SIGNAL_STOP_CONTINUED)
  1638. ? CLD_CONTINUED : CLD_STOPPED;
  1639. signal->flags &= ~SIGNAL_CLD_MASK;
  1640. why = tracehook_notify_jctl(why, CLD_CONTINUED);
  1641. spin_unlock_irq(&sighand->siglock);
  1642. if (why) {
  1643. read_lock(&tasklist_lock);
  1644. do_notify_parent_cldstop(current->group_leader, why);
  1645. read_unlock(&tasklist_lock);
  1646. }
  1647. goto relock;
  1648. }
  1649. for (;;) {
  1650. struct k_sigaction *ka;
  1651. /*
  1652. * Tracing can induce an artifical signal and choose sigaction.
  1653. * The return value in @signr determines the default action,
  1654. * but @info->si_signo is the signal number we will report.
  1655. */
  1656. signr = tracehook_get_signal(current, regs, info, return_ka);
  1657. if (unlikely(signr < 0))
  1658. goto relock;
  1659. if (unlikely(signr != 0))
  1660. ka = return_ka;
  1661. else {
  1662. if (unlikely(signal->group_stop_count > 0) &&
  1663. do_signal_stop(0))
  1664. goto relock;
  1665. signr = dequeue_signal(current, &current->blocked,
  1666. info);
  1667. if (!signr)
  1668. break; /* will return 0 */
  1669. if (signr != SIGKILL) {
  1670. signr = ptrace_signal(signr, info,
  1671. regs, cookie);
  1672. if (!signr)
  1673. continue;
  1674. }
  1675. ka = &sighand->action[signr-1];
  1676. }
  1677. /* Trace actually delivered signals. */
  1678. trace_signal_deliver(signr, info, ka);
  1679. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1680. continue;
  1681. if (ka->sa.sa_handler != SIG_DFL) {
  1682. /* Run the handler. */
  1683. *return_ka = *ka;
  1684. if (ka->sa.sa_flags & SA_ONESHOT)
  1685. ka->sa.sa_handler = SIG_DFL;
  1686. break; /* will return non-zero "signr" value */
  1687. }
  1688. /*
  1689. * Now we are doing the default action for this signal.
  1690. */
  1691. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1692. continue;
  1693. /*
  1694. * Global init gets no signals it doesn't want.
  1695. * Container-init gets no signals it doesn't want from same
  1696. * container.
  1697. *
  1698. * Note that if global/container-init sees a sig_kernel_only()
  1699. * signal here, the signal must have been generated internally
  1700. * or must have come from an ancestor namespace. In either
  1701. * case, the signal cannot be dropped.
  1702. */
  1703. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  1704. !sig_kernel_only(signr))
  1705. continue;
  1706. if (sig_kernel_stop(signr)) {
  1707. /*
  1708. * The default action is to stop all threads in
  1709. * the thread group. The job control signals
  1710. * do nothing in an orphaned pgrp, but SIGSTOP
  1711. * always works. Note that siglock needs to be
  1712. * dropped during the call to is_orphaned_pgrp()
  1713. * because of lock ordering with tasklist_lock.
  1714. * This allows an intervening SIGCONT to be posted.
  1715. * We need to check for that and bail out if necessary.
  1716. */
  1717. if (signr != SIGSTOP) {
  1718. spin_unlock_irq(&sighand->siglock);
  1719. /* signals can be posted during this window */
  1720. if (is_current_pgrp_orphaned())
  1721. goto relock;
  1722. spin_lock_irq(&sighand->siglock);
  1723. }
  1724. if (likely(do_signal_stop(info->si_signo))) {
  1725. /* It released the siglock. */
  1726. goto relock;
  1727. }
  1728. /*
  1729. * We didn't actually stop, due to a race
  1730. * with SIGCONT or something like that.
  1731. */
  1732. continue;
  1733. }
  1734. spin_unlock_irq(&sighand->siglock);
  1735. /*
  1736. * Anything else is fatal, maybe with a core dump.
  1737. */
  1738. current->flags |= PF_SIGNALED;
  1739. if (sig_kernel_coredump(signr)) {
  1740. if (print_fatal_signals)
  1741. print_fatal_signal(regs, info->si_signo);
  1742. /*
  1743. * If it was able to dump core, this kills all
  1744. * other threads in the group and synchronizes with
  1745. * their demise. If we lost the race with another
  1746. * thread getting here, it set group_exit_code
  1747. * first and our do_group_exit call below will use
  1748. * that value and ignore the one we pass it.
  1749. */
  1750. do_coredump(info->si_signo, info->si_signo, regs);
  1751. }
  1752. /*
  1753. * Death signals, no core dump.
  1754. */
  1755. do_group_exit(info->si_signo);
  1756. /* NOTREACHED */
  1757. }
  1758. spin_unlock_irq(&sighand->siglock);
  1759. return signr;
  1760. }
  1761. void exit_signals(struct task_struct *tsk)
  1762. {
  1763. int group_stop = 0;
  1764. struct task_struct *t;
  1765. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  1766. tsk->flags |= PF_EXITING;
  1767. return;
  1768. }
  1769. spin_lock_irq(&tsk->sighand->siglock);
  1770. /*
  1771. * From now this task is not visible for group-wide signals,
  1772. * see wants_signal(), do_signal_stop().
  1773. */
  1774. tsk->flags |= PF_EXITING;
  1775. if (!signal_pending(tsk))
  1776. goto out;
  1777. /*
  1778. * It could be that __group_complete_signal() choose us to
  1779. * notify about group-wide signal. Another thread should be
  1780. * woken now to take the signal since we will not.
  1781. */
  1782. for (t = tsk; (t = next_thread(t)) != tsk; )
  1783. if (!signal_pending(t) && !(t->flags & PF_EXITING))
  1784. recalc_sigpending_and_wake(t);
  1785. if (unlikely(tsk->signal->group_stop_count) &&
  1786. !--tsk->signal->group_stop_count) {
  1787. tsk->signal->flags = SIGNAL_STOP_STOPPED;
  1788. group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
  1789. }
  1790. out:
  1791. spin_unlock_irq(&tsk->sighand->siglock);
  1792. if (unlikely(group_stop)) {
  1793. read_lock(&tasklist_lock);
  1794. do_notify_parent_cldstop(tsk, group_stop);
  1795. read_unlock(&tasklist_lock);
  1796. }
  1797. }
  1798. EXPORT_SYMBOL(recalc_sigpending);
  1799. EXPORT_SYMBOL_GPL(dequeue_signal);
  1800. EXPORT_SYMBOL(flush_signals);
  1801. EXPORT_SYMBOL(force_sig);
  1802. EXPORT_SYMBOL(send_sig);
  1803. EXPORT_SYMBOL(send_sig_info);
  1804. EXPORT_SYMBOL(sigprocmask);
  1805. EXPORT_SYMBOL(block_all_signals);
  1806. EXPORT_SYMBOL(unblock_all_signals);
  1807. /*
  1808. * System call entry points.
  1809. */
  1810. SYSCALL_DEFINE0(restart_syscall)
  1811. {
  1812. struct restart_block *restart = &current_thread_info()->restart_block;
  1813. return restart->fn(restart);
  1814. }
  1815. long do_no_restart_syscall(struct restart_block *param)
  1816. {
  1817. return -EINTR;
  1818. }
  1819. /*
  1820. * We don't need to get the kernel lock - this is all local to this
  1821. * particular thread.. (and that's good, because this is _heavily_
  1822. * used by various programs)
  1823. */
  1824. /*
  1825. * This is also useful for kernel threads that want to temporarily
  1826. * (or permanently) block certain signals.
  1827. *
  1828. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  1829. * interface happily blocks "unblockable" signals like SIGKILL
  1830. * and friends.
  1831. */
  1832. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  1833. {
  1834. int error;
  1835. spin_lock_irq(&current->sighand->siglock);
  1836. if (oldset)
  1837. *oldset = current->blocked;
  1838. error = 0;
  1839. switch (how) {
  1840. case SIG_BLOCK:
  1841. sigorsets(&current->blocked, &current->blocked, set);
  1842. break;
  1843. case SIG_UNBLOCK:
  1844. signandsets(&current->blocked, &current->blocked, set);
  1845. break;
  1846. case SIG_SETMASK:
  1847. current->blocked = *set;
  1848. break;
  1849. default:
  1850. error = -EINVAL;
  1851. }
  1852. recalc_sigpending();
  1853. spin_unlock_irq(&current->sighand->siglock);
  1854. return error;
  1855. }
  1856. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
  1857. sigset_t __user *, oset, size_t, sigsetsize)
  1858. {
  1859. int error = -EINVAL;
  1860. sigset_t old_set, new_set;
  1861. /* XXX: Don't preclude handling different sized sigset_t's. */
  1862. if (sigsetsize != sizeof(sigset_t))
  1863. goto out;
  1864. if (set) {
  1865. error = -EFAULT;
  1866. if (copy_from_user(&new_set, set, sizeof(*set)))
  1867. goto out;
  1868. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1869. error = sigprocmask(how, &new_set, &old_set);
  1870. if (error)
  1871. goto out;
  1872. if (oset)
  1873. goto set_old;
  1874. } else if (oset) {
  1875. spin_lock_irq(&current->sighand->siglock);
  1876. old_set = current->blocked;
  1877. spin_unlock_irq(&current->sighand->siglock);
  1878. set_old:
  1879. error = -EFAULT;
  1880. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  1881. goto out;
  1882. }
  1883. error = 0;
  1884. out:
  1885. return error;
  1886. }
  1887. long do_sigpending(void __user *set, unsigned long sigsetsize)
  1888. {
  1889. long error = -EINVAL;
  1890. sigset_t pending;
  1891. if (sigsetsize > sizeof(sigset_t))
  1892. goto out;
  1893. spin_lock_irq(&current->sighand->siglock);
  1894. sigorsets(&pending, &current->pending.signal,
  1895. &current->signal->shared_pending.signal);
  1896. spin_unlock_irq(&current->sighand->siglock);
  1897. /* Outside the lock because only this thread touches it. */
  1898. sigandsets(&pending, &current->blocked, &pending);
  1899. error = -EFAULT;
  1900. if (!copy_to_user(set, &pending, sigsetsize))
  1901. error = 0;
  1902. out:
  1903. return error;
  1904. }
  1905. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
  1906. {
  1907. return do_sigpending(set, sigsetsize);
  1908. }
  1909. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  1910. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  1911. {
  1912. int err;
  1913. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  1914. return -EFAULT;
  1915. if (from->si_code < 0)
  1916. return __copy_to_user(to, from, sizeof(siginfo_t))
  1917. ? -EFAULT : 0;
  1918. /*
  1919. * If you change siginfo_t structure, please be sure
  1920. * this code is fixed accordingly.
  1921. * Please remember to update the signalfd_copyinfo() function
  1922. * inside fs/signalfd.c too, in case siginfo_t changes.
  1923. * It should never copy any pad contained in the structure
  1924. * to avoid security leaks, but must copy the generic
  1925. * 3 ints plus the relevant union member.
  1926. */
  1927. err = __put_user(from->si_signo, &to->si_signo);
  1928. err |= __put_user(from->si_errno, &to->si_errno);
  1929. err |= __put_user((short)from->si_code, &to->si_code);
  1930. switch (from->si_code & __SI_MASK) {
  1931. case __SI_KILL:
  1932. err |= __put_user(from->si_pid, &to->si_pid);
  1933. err |= __put_user(from->si_uid, &to->si_uid);
  1934. break;
  1935. case __SI_TIMER:
  1936. err |= __put_user(from->si_tid, &to->si_tid);
  1937. err |= __put_user(from->si_overrun, &to->si_overrun);
  1938. err |= __put_user(from->si_ptr, &to->si_ptr);
  1939. break;
  1940. case __SI_POLL:
  1941. err |= __put_user(from->si_band, &to->si_band);
  1942. err |= __put_user(from->si_fd, &to->si_fd);
  1943. break;
  1944. case __SI_FAULT:
  1945. err |= __put_user(from->si_addr, &to->si_addr);
  1946. #ifdef __ARCH_SI_TRAPNO
  1947. err |= __put_user(from->si_trapno, &to->si_trapno);
  1948. #endif
  1949. #ifdef BUS_MCEERR_AO
  1950. /*
  1951. * Other callers might not initialize the si_lsb field,
  1952. * so check explicitly for the right codes here.
  1953. */
  1954. if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
  1955. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  1956. #endif
  1957. break;
  1958. case __SI_CHLD:
  1959. err |= __put_user(from->si_pid, &to->si_pid);
  1960. err |= __put_user(from->si_uid, &to->si_uid);
  1961. err |= __put_user(from->si_status, &to->si_status);
  1962. err |= __put_user(from->si_utime, &to->si_utime);
  1963. err |= __put_user(from->si_stime, &to->si_stime);
  1964. break;
  1965. case __SI_RT: /* This is not generated by the kernel as of now. */
  1966. case __SI_MESGQ: /* But this is */
  1967. err |= __put_user(from->si_pid, &to->si_pid);
  1968. err |= __put_user(from->si_uid, &to->si_uid);
  1969. err |= __put_user(from->si_ptr, &to->si_ptr);
  1970. break;
  1971. default: /* this is just in case for now ... */
  1972. err |= __put_user(from->si_pid, &to->si_pid);
  1973. err |= __put_user(from->si_uid, &to->si_uid);
  1974. break;
  1975. }
  1976. return err;
  1977. }
  1978. #endif
  1979. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  1980. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  1981. size_t, sigsetsize)
  1982. {
  1983. int ret, sig;
  1984. sigset_t these;
  1985. struct timespec ts;
  1986. siginfo_t info;
  1987. long timeout = 0;
  1988. /* XXX: Don't preclude handling different sized sigset_t's. */
  1989. if (sigsetsize != sizeof(sigset_t))
  1990. return -EINVAL;
  1991. if (copy_from_user(&these, uthese, sizeof(these)))
  1992. return -EFAULT;
  1993. /*
  1994. * Invert the set of allowed signals to get those we
  1995. * want to block.
  1996. */
  1997. sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1998. signotset(&these);
  1999. if (uts) {
  2000. if (copy_from_user(&ts, uts, sizeof(ts)))
  2001. return -EFAULT;
  2002. if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
  2003. || ts.tv_sec < 0)
  2004. return -EINVAL;
  2005. }
  2006. spin_lock_irq(&current->sighand->siglock);
  2007. sig = dequeue_signal(current, &these, &info);
  2008. if (!sig) {
  2009. timeout = MAX_SCHEDULE_TIMEOUT;
  2010. if (uts)
  2011. timeout = (timespec_to_jiffies(&ts)
  2012. + (ts.tv_sec || ts.tv_nsec));
  2013. if (timeout) {
  2014. /*
  2015. * None ready -- temporarily unblock those we're
  2016. * interested while we are sleeping in so that we'll
  2017. * be awakened when they arrive.
  2018. */
  2019. current->real_blocked = current->blocked;
  2020. sigandsets(&current->blocked, &current->blocked, &these);
  2021. recalc_sigpending();
  2022. spin_unlock_irq(&current->sighand->siglock);
  2023. timeout = schedule_timeout_interruptible(timeout);
  2024. spin_lock_irq(&current->sighand->siglock);
  2025. sig = dequeue_signal(current, &these, &info);
  2026. current->blocked = current->real_blocked;
  2027. siginitset(&current->real_blocked, 0);
  2028. recalc_sigpending();
  2029. }
  2030. }
  2031. spin_unlock_irq(&current->sighand->siglock);
  2032. if (sig) {
  2033. ret = sig;
  2034. if (uinfo) {
  2035. if (copy_siginfo_to_user(uinfo, &info))
  2036. ret = -EFAULT;
  2037. }
  2038. } else {
  2039. ret = -EAGAIN;
  2040. if (timeout)
  2041. ret = -EINTR;
  2042. }
  2043. return ret;
  2044. }
  2045. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2046. {
  2047. struct siginfo info;
  2048. info.si_signo = sig;
  2049. info.si_errno = 0;
  2050. info.si_code = SI_USER;
  2051. info.si_pid = task_tgid_vnr(current);
  2052. info.si_uid = current_uid();
  2053. return kill_something_info(sig, &info, pid);
  2054. }
  2055. static int
  2056. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2057. {
  2058. struct task_struct *p;
  2059. int error = -ESRCH;
  2060. rcu_read_lock();
  2061. p = find_task_by_vpid(pid);
  2062. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2063. error = check_kill_permission(sig, info, p);
  2064. /*
  2065. * The null signal is a permissions and process existence
  2066. * probe. No signal is actually delivered.
  2067. */
  2068. if (!error && sig) {
  2069. error = do_send_sig_info(sig, info, p, false);
  2070. /*
  2071. * If lock_task_sighand() failed we pretend the task
  2072. * dies after receiving the signal. The window is tiny,
  2073. * and the signal is private anyway.
  2074. */
  2075. if (unlikely(error == -ESRCH))
  2076. error = 0;
  2077. }
  2078. }
  2079. rcu_read_unlock();
  2080. return error;
  2081. }
  2082. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2083. {
  2084. struct siginfo info;
  2085. info.si_signo = sig;
  2086. info.si_errno = 0;
  2087. info.si_code = SI_TKILL;
  2088. info.si_pid = task_tgid_vnr(current);
  2089. info.si_uid = current_uid();
  2090. return do_send_specific(tgid, pid, sig, &info);
  2091. }
  2092. /**
  2093. * sys_tgkill - send signal to one specific thread
  2094. * @tgid: the thread group ID of the thread
  2095. * @pid: the PID of the thread
  2096. * @sig: signal to be sent
  2097. *
  2098. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2099. * exists but it's not belonging to the target process anymore. This
  2100. * method solves the problem of threads exiting and PIDs getting reused.
  2101. */
  2102. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2103. {
  2104. /* This is only valid for single tasks */
  2105. if (pid <= 0 || tgid <= 0)
  2106. return -EINVAL;
  2107. return do_tkill(tgid, pid, sig);
  2108. }
  2109. /*
  2110. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2111. */
  2112. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2113. {
  2114. /* This is only valid for single tasks */
  2115. if (pid <= 0)
  2116. return -EINVAL;
  2117. return do_tkill(0, pid, sig);
  2118. }
  2119. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2120. siginfo_t __user *, uinfo)
  2121. {
  2122. siginfo_t info;
  2123. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2124. return -EFAULT;
  2125. /* Not even root can pretend to send signals from the kernel.
  2126. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2127. */
  2128. if (info.si_code >= 0 || info.si_code == SI_TKILL) {
  2129. /* We used to allow any < 0 si_code */
  2130. WARN_ON_ONCE(info.si_code < 0);
  2131. return -EPERM;
  2132. }
  2133. info.si_signo = sig;
  2134. /* POSIX.1b doesn't mention process groups. */
  2135. return kill_proc_info(sig, &info, pid);
  2136. }
  2137. long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2138. {
  2139. /* This is only valid for single tasks */
  2140. if (pid <= 0 || tgid <= 0)
  2141. return -EINVAL;
  2142. /* Not even root can pretend to send signals from the kernel.
  2143. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2144. */
  2145. if (info->si_code >= 0 || info->si_code == SI_TKILL) {
  2146. /* We used to allow any < 0 si_code */
  2147. WARN_ON_ONCE(info->si_code < 0);
  2148. return -EPERM;
  2149. }
  2150. info->si_signo = sig;
  2151. return do_send_specific(tgid, pid, sig, info);
  2152. }
  2153. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2154. siginfo_t __user *, uinfo)
  2155. {
  2156. siginfo_t info;
  2157. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2158. return -EFAULT;
  2159. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2160. }
  2161. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2162. {
  2163. struct task_struct *t = current;
  2164. struct k_sigaction *k;
  2165. sigset_t mask;
  2166. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2167. return -EINVAL;
  2168. k = &t->sighand->action[sig-1];
  2169. spin_lock_irq(&current->sighand->siglock);
  2170. if (oact)
  2171. *oact = *k;
  2172. if (act) {
  2173. sigdelsetmask(&act->sa.sa_mask,
  2174. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2175. *k = *act;
  2176. /*
  2177. * POSIX 3.3.1.3:
  2178. * "Setting a signal action to SIG_IGN for a signal that is
  2179. * pending shall cause the pending signal to be discarded,
  2180. * whether or not it is blocked."
  2181. *
  2182. * "Setting a signal action to SIG_DFL for a signal that is
  2183. * pending and whose default action is to ignore the signal
  2184. * (for example, SIGCHLD), shall cause the pending signal to
  2185. * be discarded, whether or not it is blocked"
  2186. */
  2187. if (sig_handler_ignored(sig_handler(t, sig), sig)) {
  2188. sigemptyset(&mask);
  2189. sigaddset(&mask, sig);
  2190. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2191. do {
  2192. rm_from_queue_full(&mask, &t->pending);
  2193. t = next_thread(t);
  2194. } while (t != current);
  2195. }
  2196. }
  2197. spin_unlock_irq(&current->sighand->siglock);
  2198. return 0;
  2199. }
  2200. int
  2201. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2202. {
  2203. stack_t oss;
  2204. int error;
  2205. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2206. oss.ss_size = current->sas_ss_size;
  2207. oss.ss_flags = sas_ss_flags(sp);
  2208. if (uss) {
  2209. void __user *ss_sp;
  2210. size_t ss_size;
  2211. int ss_flags;
  2212. error = -EFAULT;
  2213. if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
  2214. goto out;
  2215. error = __get_user(ss_sp, &uss->ss_sp) |
  2216. __get_user(ss_flags, &uss->ss_flags) |
  2217. __get_user(ss_size, &uss->ss_size);
  2218. if (error)
  2219. goto out;
  2220. error = -EPERM;
  2221. if (on_sig_stack(sp))
  2222. goto out;
  2223. error = -EINVAL;
  2224. /*
  2225. * Note - this code used to test ss_flags incorrectly:
  2226. * old code may have been written using ss_flags==0
  2227. * to mean ss_flags==SS_ONSTACK (as this was the only
  2228. * way that worked) - this fix preserves that older
  2229. * mechanism.
  2230. */
  2231. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2232. goto out;
  2233. if (ss_flags == SS_DISABLE) {
  2234. ss_size = 0;
  2235. ss_sp = NULL;
  2236. } else {
  2237. error = -ENOMEM;
  2238. if (ss_size < MINSIGSTKSZ)
  2239. goto out;
  2240. }
  2241. current->sas_ss_sp = (unsigned long) ss_sp;
  2242. current->sas_ss_size = ss_size;
  2243. }
  2244. error = 0;
  2245. if (uoss) {
  2246. error = -EFAULT;
  2247. if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
  2248. goto out;
  2249. error = __put_user(oss.ss_sp, &uoss->ss_sp) |
  2250. __put_user(oss.ss_size, &uoss->ss_size) |
  2251. __put_user(oss.ss_flags, &uoss->ss_flags);
  2252. }
  2253. out:
  2254. return error;
  2255. }
  2256. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2257. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
  2258. {
  2259. return do_sigpending(set, sizeof(*set));
  2260. }
  2261. #endif
  2262. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2263. /*
  2264. * Some platforms have their own version with special arguments;
  2265. * others support only sys_rt_sigprocmask.
  2266. */
  2267. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
  2268. old_sigset_t __user *, oset)
  2269. {
  2270. int error;
  2271. old_sigset_t old_set, new_set;
  2272. if (set) {
  2273. error = -EFAULT;
  2274. if (copy_from_user(&new_set, set, sizeof(*set)))
  2275. goto out;
  2276. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2277. spin_lock_irq(&current->sighand->siglock);
  2278. old_set = current->blocked.sig[0];
  2279. error = 0;
  2280. switch (how) {
  2281. default:
  2282. error = -EINVAL;
  2283. break;
  2284. case SIG_BLOCK:
  2285. sigaddsetmask(&current->blocked, new_set);
  2286. break;
  2287. case SIG_UNBLOCK:
  2288. sigdelsetmask(&current->blocked, new_set);
  2289. break;
  2290. case SIG_SETMASK:
  2291. current->blocked.sig[0] = new_set;
  2292. break;
  2293. }
  2294. recalc_sigpending();
  2295. spin_unlock_irq(&current->sighand->siglock);
  2296. if (error)
  2297. goto out;
  2298. if (oset)
  2299. goto set_old;
  2300. } else if (oset) {
  2301. old_set = current->blocked.sig[0];
  2302. set_old:
  2303. error = -EFAULT;
  2304. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2305. goto out;
  2306. }
  2307. error = 0;
  2308. out:
  2309. return error;
  2310. }
  2311. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2312. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2313. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2314. const struct sigaction __user *, act,
  2315. struct sigaction __user *, oact,
  2316. size_t, sigsetsize)
  2317. {
  2318. struct k_sigaction new_sa, old_sa;
  2319. int ret = -EINVAL;
  2320. /* XXX: Don't preclude handling different sized sigset_t's. */
  2321. if (sigsetsize != sizeof(sigset_t))
  2322. goto out;
  2323. if (act) {
  2324. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2325. return -EFAULT;
  2326. }
  2327. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2328. if (!ret && oact) {
  2329. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2330. return -EFAULT;
  2331. }
  2332. out:
  2333. return ret;
  2334. }
  2335. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2336. #ifdef __ARCH_WANT_SYS_SGETMASK
  2337. /*
  2338. * For backwards compatibility. Functionality superseded by sigprocmask.
  2339. */
  2340. SYSCALL_DEFINE0(sgetmask)
  2341. {
  2342. /* SMP safe */
  2343. return current->blocked.sig[0];
  2344. }
  2345. SYSCALL_DEFINE1(ssetmask, int, newmask)
  2346. {
  2347. int old;
  2348. spin_lock_irq(&current->sighand->siglock);
  2349. old = current->blocked.sig[0];
  2350. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2351. sigmask(SIGSTOP)));
  2352. recalc_sigpending();
  2353. spin_unlock_irq(&current->sighand->siglock);
  2354. return old;
  2355. }
  2356. #endif /* __ARCH_WANT_SGETMASK */
  2357. #ifdef __ARCH_WANT_SYS_SIGNAL
  2358. /*
  2359. * For backwards compatibility. Functionality superseded by sigaction.
  2360. */
  2361. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  2362. {
  2363. struct k_sigaction new_sa, old_sa;
  2364. int ret;
  2365. new_sa.sa.sa_handler = handler;
  2366. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2367. sigemptyset(&new_sa.sa.sa_mask);
  2368. ret = do_sigaction(sig, &new_sa, &old_sa);
  2369. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2370. }
  2371. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2372. #ifdef __ARCH_WANT_SYS_PAUSE
  2373. SYSCALL_DEFINE0(pause)
  2374. {
  2375. current->state = TASK_INTERRUPTIBLE;
  2376. schedule();
  2377. return -ERESTARTNOHAND;
  2378. }
  2379. #endif
  2380. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2381. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  2382. {
  2383. sigset_t newset;
  2384. /* XXX: Don't preclude handling different sized sigset_t's. */
  2385. if (sigsetsize != sizeof(sigset_t))
  2386. return -EINVAL;
  2387. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2388. return -EFAULT;
  2389. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2390. spin_lock_irq(&current->sighand->siglock);
  2391. current->saved_sigmask = current->blocked;
  2392. current->blocked = newset;
  2393. recalc_sigpending();
  2394. spin_unlock_irq(&current->sighand->siglock);
  2395. current->state = TASK_INTERRUPTIBLE;
  2396. schedule();
  2397. set_restore_sigmask();
  2398. return -ERESTARTNOHAND;
  2399. }
  2400. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2401. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2402. {
  2403. return NULL;
  2404. }
  2405. void __init signals_init(void)
  2406. {
  2407. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2408. }
  2409. #ifdef CONFIG_KGDB_KDB
  2410. #include <linux/kdb.h>
  2411. /*
  2412. * kdb_send_sig_info - Allows kdb to send signals without exposing
  2413. * signal internals. This function checks if the required locks are
  2414. * available before calling the main signal code, to avoid kdb
  2415. * deadlocks.
  2416. */
  2417. void
  2418. kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
  2419. {
  2420. static struct task_struct *kdb_prev_t;
  2421. int sig, new_t;
  2422. if (!spin_trylock(&t->sighand->siglock)) {
  2423. kdb_printf("Can't do kill command now.\n"
  2424. "The sigmask lock is held somewhere else in "
  2425. "kernel, try again later\n");
  2426. return;
  2427. }
  2428. spin_unlock(&t->sighand->siglock);
  2429. new_t = kdb_prev_t != t;
  2430. kdb_prev_t = t;
  2431. if (t->state != TASK_RUNNING && new_t) {
  2432. kdb_printf("Process is not RUNNING, sending a signal from "
  2433. "kdb risks deadlock\n"
  2434. "on the run queue locks. "
  2435. "The signal has _not_ been sent.\n"
  2436. "Reissue the kill command if you want to risk "
  2437. "the deadlock.\n");
  2438. return;
  2439. }
  2440. sig = info->si_signo;
  2441. if (send_sig_info(sig, info, t))
  2442. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  2443. sig, t->pid);
  2444. else
  2445. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  2446. }
  2447. #endif /* CONFIG_KGDB_KDB */