signal.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722
  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/export.h>
  14. #include <linux/init.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/sched/user.h>
  17. #include <linux/sched/debug.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/sched/task_stack.h>
  20. #include <linux/sched/cputime.h>
  21. #include <linux/fs.h>
  22. #include <linux/tty.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/coredump.h>
  25. #include <linux/security.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/signal.h>
  29. #include <linux/signalfd.h>
  30. #include <linux/ratelimit.h>
  31. #include <linux/tracehook.h>
  32. #include <linux/capability.h>
  33. #include <linux/freezer.h>
  34. #include <linux/pid_namespace.h>
  35. #include <linux/nsproxy.h>
  36. #include <linux/user_namespace.h>
  37. #include <linux/uprobes.h>
  38. #include <linux/compat.h>
  39. #include <linux/cn_proc.h>
  40. #include <linux/compiler.h>
  41. #include <linux/posix-timers.h>
  42. #define CREATE_TRACE_POINTS
  43. #include <trace/events/signal.h>
  44. #include <asm/param.h>
  45. #include <linux/uaccess.h>
  46. #include <asm/unistd.h>
  47. #include <asm/siginfo.h>
  48. #include <asm/cacheflush.h>
  49. #include "audit.h" /* audit_signal_info() */
  50. /*
  51. * SLAB caches for signal bits.
  52. */
  53. static struct kmem_cache *sigqueue_cachep;
  54. int print_fatal_signals __read_mostly;
  55. static void __user *sig_handler(struct task_struct *t, int sig)
  56. {
  57. return t->sighand->action[sig - 1].sa.sa_handler;
  58. }
  59. static int sig_handler_ignored(void __user *handler, int sig)
  60. {
  61. /* Is it explicitly or implicitly ignored? */
  62. return handler == SIG_IGN ||
  63. (handler == SIG_DFL && sig_kernel_ignore(sig));
  64. }
  65. static int sig_task_ignored(struct task_struct *t, int sig, bool force)
  66. {
  67. void __user *handler;
  68. handler = sig_handler(t, sig);
  69. if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
  70. handler == SIG_DFL && !(force && sig_kernel_only(sig)))
  71. return 1;
  72. return sig_handler_ignored(handler, sig);
  73. }
  74. static int sig_ignored(struct task_struct *t, int sig, bool force)
  75. {
  76. /*
  77. * Blocked signals are never ignored, since the
  78. * signal handler may change by the time it is
  79. * unblocked.
  80. */
  81. if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
  82. return 0;
  83. /*
  84. * Tracers may want to know about even ignored signal unless it
  85. * is SIGKILL which can't be reported anyway but can be ignored
  86. * by SIGNAL_UNKILLABLE task.
  87. */
  88. if (t->ptrace && sig != SIGKILL)
  89. return 0;
  90. return sig_task_ignored(t, sig, force);
  91. }
  92. /*
  93. * Re-calculate pending state from the set of locally pending
  94. * signals, globally pending signals, and blocked signals.
  95. */
  96. static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
  97. {
  98. unsigned long ready;
  99. long i;
  100. switch (_NSIG_WORDS) {
  101. default:
  102. for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
  103. ready |= signal->sig[i] &~ blocked->sig[i];
  104. break;
  105. case 4: ready = signal->sig[3] &~ blocked->sig[3];
  106. ready |= signal->sig[2] &~ blocked->sig[2];
  107. ready |= signal->sig[1] &~ blocked->sig[1];
  108. ready |= signal->sig[0] &~ blocked->sig[0];
  109. break;
  110. case 2: ready = signal->sig[1] &~ blocked->sig[1];
  111. ready |= signal->sig[0] &~ blocked->sig[0];
  112. break;
  113. case 1: ready = signal->sig[0] &~ blocked->sig[0];
  114. }
  115. return ready != 0;
  116. }
  117. #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
  118. static int recalc_sigpending_tsk(struct task_struct *t)
  119. {
  120. if ((t->jobctl & JOBCTL_PENDING_MASK) ||
  121. PENDING(&t->pending, &t->blocked) ||
  122. PENDING(&t->signal->shared_pending, &t->blocked)) {
  123. set_tsk_thread_flag(t, TIF_SIGPENDING);
  124. return 1;
  125. }
  126. /*
  127. * We must never clear the flag in another thread, or in current
  128. * when it's possible the current syscall is returning -ERESTART*.
  129. * So we don't clear it here, and only callers who know they should do.
  130. */
  131. return 0;
  132. }
  133. /*
  134. * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
  135. * This is superfluous when called on current, the wakeup is a harmless no-op.
  136. */
  137. void recalc_sigpending_and_wake(struct task_struct *t)
  138. {
  139. if (recalc_sigpending_tsk(t))
  140. signal_wake_up(t, 0);
  141. }
  142. void recalc_sigpending(void)
  143. {
  144. if (!recalc_sigpending_tsk(current) && !freezing(current))
  145. clear_thread_flag(TIF_SIGPENDING);
  146. }
  147. /* Given the mask, find the first available signal that should be serviced. */
  148. #define SYNCHRONOUS_MASK \
  149. (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
  150. sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
  151. int next_signal(struct sigpending *pending, sigset_t *mask)
  152. {
  153. unsigned long i, *s, *m, x;
  154. int sig = 0;
  155. s = pending->signal.sig;
  156. m = mask->sig;
  157. /*
  158. * Handle the first word specially: it contains the
  159. * synchronous signals that need to be dequeued first.
  160. */
  161. x = *s &~ *m;
  162. if (x) {
  163. if (x & SYNCHRONOUS_MASK)
  164. x &= SYNCHRONOUS_MASK;
  165. sig = ffz(~x) + 1;
  166. return sig;
  167. }
  168. switch (_NSIG_WORDS) {
  169. default:
  170. for (i = 1; i < _NSIG_WORDS; ++i) {
  171. x = *++s &~ *++m;
  172. if (!x)
  173. continue;
  174. sig = ffz(~x) + i*_NSIG_BPW + 1;
  175. break;
  176. }
  177. break;
  178. case 2:
  179. x = s[1] &~ m[1];
  180. if (!x)
  181. break;
  182. sig = ffz(~x) + _NSIG_BPW + 1;
  183. break;
  184. case 1:
  185. /* Nothing to do */
  186. break;
  187. }
  188. return sig;
  189. }
  190. static inline void print_dropped_signal(int sig)
  191. {
  192. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  193. if (!print_fatal_signals)
  194. return;
  195. if (!__ratelimit(&ratelimit_state))
  196. return;
  197. pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
  198. current->comm, current->pid, sig);
  199. }
  200. /**
  201. * task_set_jobctl_pending - set jobctl pending bits
  202. * @task: target task
  203. * @mask: pending bits to set
  204. *
  205. * Clear @mask from @task->jobctl. @mask must be subset of
  206. * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
  207. * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
  208. * cleared. If @task is already being killed or exiting, this function
  209. * becomes noop.
  210. *
  211. * CONTEXT:
  212. * Must be called with @task->sighand->siglock held.
  213. *
  214. * RETURNS:
  215. * %true if @mask is set, %false if made noop because @task was dying.
  216. */
  217. bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
  218. {
  219. BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
  220. JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
  221. BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
  222. if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
  223. return false;
  224. if (mask & JOBCTL_STOP_SIGMASK)
  225. task->jobctl &= ~JOBCTL_STOP_SIGMASK;
  226. task->jobctl |= mask;
  227. return true;
  228. }
  229. /**
  230. * task_clear_jobctl_trapping - clear jobctl trapping bit
  231. * @task: target task
  232. *
  233. * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
  234. * Clear it and wake up the ptracer. Note that we don't need any further
  235. * locking. @task->siglock guarantees that @task->parent points to the
  236. * ptracer.
  237. *
  238. * CONTEXT:
  239. * Must be called with @task->sighand->siglock held.
  240. */
  241. void task_clear_jobctl_trapping(struct task_struct *task)
  242. {
  243. if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
  244. task->jobctl &= ~JOBCTL_TRAPPING;
  245. smp_mb(); /* advised by wake_up_bit() */
  246. wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
  247. }
  248. }
  249. /**
  250. * task_clear_jobctl_pending - clear jobctl pending bits
  251. * @task: target task
  252. * @mask: pending bits to clear
  253. *
  254. * Clear @mask from @task->jobctl. @mask must be subset of
  255. * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
  256. * STOP bits are cleared together.
  257. *
  258. * If clearing of @mask leaves no stop or trap pending, this function calls
  259. * task_clear_jobctl_trapping().
  260. *
  261. * CONTEXT:
  262. * Must be called with @task->sighand->siglock held.
  263. */
  264. void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
  265. {
  266. BUG_ON(mask & ~JOBCTL_PENDING_MASK);
  267. if (mask & JOBCTL_STOP_PENDING)
  268. mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
  269. task->jobctl &= ~mask;
  270. if (!(task->jobctl & JOBCTL_PENDING_MASK))
  271. task_clear_jobctl_trapping(task);
  272. }
  273. /**
  274. * task_participate_group_stop - participate in a group stop
  275. * @task: task participating in a group stop
  276. *
  277. * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
  278. * Group stop states are cleared and the group stop count is consumed if
  279. * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
  280. * stop, the appropriate %SIGNAL_* flags are set.
  281. *
  282. * CONTEXT:
  283. * Must be called with @task->sighand->siglock held.
  284. *
  285. * RETURNS:
  286. * %true if group stop completion should be notified to the parent, %false
  287. * otherwise.
  288. */
  289. static bool task_participate_group_stop(struct task_struct *task)
  290. {
  291. struct signal_struct *sig = task->signal;
  292. bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
  293. WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
  294. task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
  295. if (!consume)
  296. return false;
  297. if (!WARN_ON_ONCE(sig->group_stop_count == 0))
  298. sig->group_stop_count--;
  299. /*
  300. * Tell the caller to notify completion iff we are entering into a
  301. * fresh group stop. Read comment in do_signal_stop() for details.
  302. */
  303. if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
  304. signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
  305. return true;
  306. }
  307. return false;
  308. }
  309. /*
  310. * allocate a new signal queue record
  311. * - this may be called without locks if and only if t == current, otherwise an
  312. * appropriate lock must be held to stop the target task from exiting
  313. */
  314. static struct sigqueue *
  315. __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  316. {
  317. struct sigqueue *q = NULL;
  318. struct user_struct *user;
  319. /*
  320. * Protect access to @t credentials. This can go away when all
  321. * callers hold rcu read lock.
  322. */
  323. rcu_read_lock();
  324. user = get_uid(__task_cred(t)->user);
  325. atomic_inc(&user->sigpending);
  326. rcu_read_unlock();
  327. if (override_rlimit ||
  328. atomic_read(&user->sigpending) <=
  329. task_rlimit(t, RLIMIT_SIGPENDING)) {
  330. q = kmem_cache_alloc(sigqueue_cachep, flags);
  331. } else {
  332. print_dropped_signal(sig);
  333. }
  334. if (unlikely(q == NULL)) {
  335. atomic_dec(&user->sigpending);
  336. free_uid(user);
  337. } else {
  338. INIT_LIST_HEAD(&q->list);
  339. q->flags = 0;
  340. q->user = user;
  341. }
  342. return q;
  343. }
  344. static void __sigqueue_free(struct sigqueue *q)
  345. {
  346. if (q->flags & SIGQUEUE_PREALLOC)
  347. return;
  348. atomic_dec(&q->user->sigpending);
  349. free_uid(q->user);
  350. kmem_cache_free(sigqueue_cachep, q);
  351. }
  352. void flush_sigqueue(struct sigpending *queue)
  353. {
  354. struct sigqueue *q;
  355. sigemptyset(&queue->signal);
  356. while (!list_empty(&queue->list)) {
  357. q = list_entry(queue->list.next, struct sigqueue , list);
  358. list_del_init(&q->list);
  359. __sigqueue_free(q);
  360. }
  361. }
  362. /*
  363. * Flush all pending signals for this kthread.
  364. */
  365. void flush_signals(struct task_struct *t)
  366. {
  367. unsigned long flags;
  368. spin_lock_irqsave(&t->sighand->siglock, flags);
  369. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  370. flush_sigqueue(&t->pending);
  371. flush_sigqueue(&t->signal->shared_pending);
  372. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  373. }
  374. #ifdef CONFIG_POSIX_TIMERS
  375. static void __flush_itimer_signals(struct sigpending *pending)
  376. {
  377. sigset_t signal, retain;
  378. struct sigqueue *q, *n;
  379. signal = pending->signal;
  380. sigemptyset(&retain);
  381. list_for_each_entry_safe(q, n, &pending->list, list) {
  382. int sig = q->info.si_signo;
  383. if (likely(q->info.si_code != SI_TIMER)) {
  384. sigaddset(&retain, sig);
  385. } else {
  386. sigdelset(&signal, sig);
  387. list_del_init(&q->list);
  388. __sigqueue_free(q);
  389. }
  390. }
  391. sigorsets(&pending->signal, &signal, &retain);
  392. }
  393. void flush_itimer_signals(void)
  394. {
  395. struct task_struct *tsk = current;
  396. unsigned long flags;
  397. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  398. __flush_itimer_signals(&tsk->pending);
  399. __flush_itimer_signals(&tsk->signal->shared_pending);
  400. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  401. }
  402. #endif
  403. void ignore_signals(struct task_struct *t)
  404. {
  405. int i;
  406. for (i = 0; i < _NSIG; ++i)
  407. t->sighand->action[i].sa.sa_handler = SIG_IGN;
  408. flush_signals(t);
  409. }
  410. /*
  411. * Flush all handlers for a task.
  412. */
  413. void
  414. flush_signal_handlers(struct task_struct *t, int force_default)
  415. {
  416. int i;
  417. struct k_sigaction *ka = &t->sighand->action[0];
  418. for (i = _NSIG ; i != 0 ; i--) {
  419. if (force_default || ka->sa.sa_handler != SIG_IGN)
  420. ka->sa.sa_handler = SIG_DFL;
  421. ka->sa.sa_flags = 0;
  422. #ifdef __ARCH_HAS_SA_RESTORER
  423. ka->sa.sa_restorer = NULL;
  424. #endif
  425. sigemptyset(&ka->sa.sa_mask);
  426. ka++;
  427. }
  428. }
  429. int unhandled_signal(struct task_struct *tsk, int sig)
  430. {
  431. void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
  432. if (is_global_init(tsk))
  433. return 1;
  434. if (handler != SIG_IGN && handler != SIG_DFL)
  435. return 0;
  436. /* if ptraced, let the tracer determine */
  437. return !tsk->ptrace;
  438. }
  439. static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
  440. bool *resched_timer)
  441. {
  442. struct sigqueue *q, *first = NULL;
  443. /*
  444. * Collect the siginfo appropriate to this signal. Check if
  445. * there is another siginfo for the same signal.
  446. */
  447. list_for_each_entry(q, &list->list, list) {
  448. if (q->info.si_signo == sig) {
  449. if (first)
  450. goto still_pending;
  451. first = q;
  452. }
  453. }
  454. sigdelset(&list->signal, sig);
  455. if (first) {
  456. still_pending:
  457. list_del_init(&first->list);
  458. copy_siginfo(info, &first->info);
  459. *resched_timer =
  460. (first->flags & SIGQUEUE_PREALLOC) &&
  461. (info->si_code == SI_TIMER) &&
  462. (info->si_sys_private);
  463. __sigqueue_free(first);
  464. } else {
  465. /*
  466. * Ok, it wasn't in the queue. This must be
  467. * a fast-pathed signal or we must have been
  468. * out of queue space. So zero out the info.
  469. */
  470. info->si_signo = sig;
  471. info->si_errno = 0;
  472. info->si_code = SI_USER;
  473. info->si_pid = 0;
  474. info->si_uid = 0;
  475. }
  476. }
  477. static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  478. siginfo_t *info, bool *resched_timer)
  479. {
  480. int sig = next_signal(pending, mask);
  481. if (sig)
  482. collect_signal(sig, pending, info, resched_timer);
  483. return sig;
  484. }
  485. /*
  486. * Dequeue a signal and return the element to the caller, which is
  487. * expected to free it.
  488. *
  489. * All callers have to hold the siglock.
  490. */
  491. int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  492. {
  493. bool resched_timer = false;
  494. int signr;
  495. /* We only dequeue private signals from ourselves, we don't let
  496. * signalfd steal them
  497. */
  498. signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
  499. if (!signr) {
  500. signr = __dequeue_signal(&tsk->signal->shared_pending,
  501. mask, info, &resched_timer);
  502. #ifdef CONFIG_POSIX_TIMERS
  503. /*
  504. * itimer signal ?
  505. *
  506. * itimers are process shared and we restart periodic
  507. * itimers in the signal delivery path to prevent DoS
  508. * attacks in the high resolution timer case. This is
  509. * compliant with the old way of self-restarting
  510. * itimers, as the SIGALRM is a legacy signal and only
  511. * queued once. Changing the restart behaviour to
  512. * restart the timer in the signal dequeue path is
  513. * reducing the timer noise on heavy loaded !highres
  514. * systems too.
  515. */
  516. if (unlikely(signr == SIGALRM)) {
  517. struct hrtimer *tmr = &tsk->signal->real_timer;
  518. if (!hrtimer_is_queued(tmr) &&
  519. tsk->signal->it_real_incr != 0) {
  520. hrtimer_forward(tmr, tmr->base->get_time(),
  521. tsk->signal->it_real_incr);
  522. hrtimer_restart(tmr);
  523. }
  524. }
  525. #endif
  526. }
  527. recalc_sigpending();
  528. if (!signr)
  529. return 0;
  530. if (unlikely(sig_kernel_stop(signr))) {
  531. /*
  532. * Set a marker that we have dequeued a stop signal. Our
  533. * caller might release the siglock and then the pending
  534. * stop signal it is about to process is no longer in the
  535. * pending bitmasks, but must still be cleared by a SIGCONT
  536. * (and overruled by a SIGKILL). So those cases clear this
  537. * shared flag after we've set it. Note that this flag may
  538. * remain set after the signal we return is ignored or
  539. * handled. That doesn't matter because its only purpose
  540. * is to alert stop-signal processing code when another
  541. * processor has come along and cleared the flag.
  542. */
  543. current->jobctl |= JOBCTL_STOP_DEQUEUED;
  544. }
  545. #ifdef CONFIG_POSIX_TIMERS
  546. if (resched_timer) {
  547. /*
  548. * Release the siglock to ensure proper locking order
  549. * of timer locks outside of siglocks. Note, we leave
  550. * irqs disabled here, since the posix-timers code is
  551. * about to disable them again anyway.
  552. */
  553. spin_unlock(&tsk->sighand->siglock);
  554. posixtimer_rearm(info);
  555. spin_lock(&tsk->sighand->siglock);
  556. }
  557. #endif
  558. return signr;
  559. }
  560. /*
  561. * Tell a process that it has a new active signal..
  562. *
  563. * NOTE! we rely on the previous spin_lock to
  564. * lock interrupts for us! We can only be called with
  565. * "siglock" held, and the local interrupt must
  566. * have been disabled when that got acquired!
  567. *
  568. * No need to set need_resched since signal event passing
  569. * goes through ->blocked
  570. */
  571. void signal_wake_up_state(struct task_struct *t, unsigned int state)
  572. {
  573. set_tsk_thread_flag(t, TIF_SIGPENDING);
  574. /*
  575. * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
  576. * case. We don't check t->state here because there is a race with it
  577. * executing another processor and just now entering stopped state.
  578. * By using wake_up_state, we ensure the process will wake up and
  579. * handle its death signal.
  580. */
  581. if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
  582. kick_process(t);
  583. }
  584. /*
  585. * Remove signals in mask from the pending set and queue.
  586. * Returns 1 if any signals were found.
  587. *
  588. * All callers must be holding the siglock.
  589. */
  590. static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
  591. {
  592. struct sigqueue *q, *n;
  593. sigset_t m;
  594. sigandsets(&m, mask, &s->signal);
  595. if (sigisemptyset(&m))
  596. return 0;
  597. sigandnsets(&s->signal, &s->signal, mask);
  598. list_for_each_entry_safe(q, n, &s->list, list) {
  599. if (sigismember(mask, q->info.si_signo)) {
  600. list_del_init(&q->list);
  601. __sigqueue_free(q);
  602. }
  603. }
  604. return 1;
  605. }
  606. static inline int is_si_special(const struct siginfo *info)
  607. {
  608. return info <= SEND_SIG_FORCED;
  609. }
  610. static inline bool si_fromuser(const struct siginfo *info)
  611. {
  612. return info == SEND_SIG_NOINFO ||
  613. (!is_si_special(info) && SI_FROMUSER(info));
  614. }
  615. /*
  616. * called with RCU read lock from check_kill_permission()
  617. */
  618. static int kill_ok_by_cred(struct task_struct *t)
  619. {
  620. const struct cred *cred = current_cred();
  621. const struct cred *tcred = __task_cred(t);
  622. if (uid_eq(cred->euid, tcred->suid) ||
  623. uid_eq(cred->euid, tcred->uid) ||
  624. uid_eq(cred->uid, tcred->suid) ||
  625. uid_eq(cred->uid, tcred->uid))
  626. return 1;
  627. if (ns_capable(tcred->user_ns, CAP_KILL))
  628. return 1;
  629. return 0;
  630. }
  631. /*
  632. * Bad permissions for sending the signal
  633. * - the caller must hold the RCU read lock
  634. */
  635. static int check_kill_permission(int sig, struct siginfo *info,
  636. struct task_struct *t)
  637. {
  638. struct pid *sid;
  639. int error;
  640. if (!valid_signal(sig))
  641. return -EINVAL;
  642. if (!si_fromuser(info))
  643. return 0;
  644. error = audit_signal_info(sig, t); /* Let audit system see the signal */
  645. if (error)
  646. return error;
  647. if (!same_thread_group(current, t) &&
  648. !kill_ok_by_cred(t)) {
  649. switch (sig) {
  650. case SIGCONT:
  651. sid = task_session(t);
  652. /*
  653. * We don't return the error if sid == NULL. The
  654. * task was unhashed, the caller must notice this.
  655. */
  656. if (!sid || sid == task_session(current))
  657. break;
  658. default:
  659. return -EPERM;
  660. }
  661. }
  662. return security_task_kill(t, info, sig, 0);
  663. }
  664. /**
  665. * ptrace_trap_notify - schedule trap to notify ptracer
  666. * @t: tracee wanting to notify tracer
  667. *
  668. * This function schedules sticky ptrace trap which is cleared on the next
  669. * TRAP_STOP to notify ptracer of an event. @t must have been seized by
  670. * ptracer.
  671. *
  672. * If @t is running, STOP trap will be taken. If trapped for STOP and
  673. * ptracer is listening for events, tracee is woken up so that it can
  674. * re-trap for the new event. If trapped otherwise, STOP trap will be
  675. * eventually taken without returning to userland after the existing traps
  676. * are finished by PTRACE_CONT.
  677. *
  678. * CONTEXT:
  679. * Must be called with @task->sighand->siglock held.
  680. */
  681. static void ptrace_trap_notify(struct task_struct *t)
  682. {
  683. WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
  684. assert_spin_locked(&t->sighand->siglock);
  685. task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
  686. ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
  687. }
  688. /*
  689. * Handle magic process-wide effects of stop/continue signals. Unlike
  690. * the signal actions, these happen immediately at signal-generation
  691. * time regardless of blocking, ignoring, or handling. This does the
  692. * actual continuing for SIGCONT, but not the actual stopping for stop
  693. * signals. The process stop is done as a signal action for SIG_DFL.
  694. *
  695. * Returns true if the signal should be actually delivered, otherwise
  696. * it should be dropped.
  697. */
  698. static bool prepare_signal(int sig, struct task_struct *p, bool force)
  699. {
  700. struct signal_struct *signal = p->signal;
  701. struct task_struct *t;
  702. sigset_t flush;
  703. if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
  704. if (!(signal->flags & SIGNAL_GROUP_EXIT))
  705. return sig == SIGKILL;
  706. /*
  707. * The process is in the middle of dying, nothing to do.
  708. */
  709. } else if (sig_kernel_stop(sig)) {
  710. /*
  711. * This is a stop signal. Remove SIGCONT from all queues.
  712. */
  713. siginitset(&flush, sigmask(SIGCONT));
  714. flush_sigqueue_mask(&flush, &signal->shared_pending);
  715. for_each_thread(p, t)
  716. flush_sigqueue_mask(&flush, &t->pending);
  717. } else if (sig == SIGCONT) {
  718. unsigned int why;
  719. /*
  720. * Remove all stop signals from all queues, wake all threads.
  721. */
  722. siginitset(&flush, SIG_KERNEL_STOP_MASK);
  723. flush_sigqueue_mask(&flush, &signal->shared_pending);
  724. for_each_thread(p, t) {
  725. flush_sigqueue_mask(&flush, &t->pending);
  726. task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
  727. if (likely(!(t->ptrace & PT_SEIZED)))
  728. wake_up_state(t, __TASK_STOPPED);
  729. else
  730. ptrace_trap_notify(t);
  731. }
  732. /*
  733. * Notify the parent with CLD_CONTINUED if we were stopped.
  734. *
  735. * If we were in the middle of a group stop, we pretend it
  736. * was already finished, and then continued. Since SIGCHLD
  737. * doesn't queue we report only CLD_STOPPED, as if the next
  738. * CLD_CONTINUED was dropped.
  739. */
  740. why = 0;
  741. if (signal->flags & SIGNAL_STOP_STOPPED)
  742. why |= SIGNAL_CLD_CONTINUED;
  743. else if (signal->group_stop_count)
  744. why |= SIGNAL_CLD_STOPPED;
  745. if (why) {
  746. /*
  747. * The first thread which returns from do_signal_stop()
  748. * will take ->siglock, notice SIGNAL_CLD_MASK, and
  749. * notify its parent. See get_signal_to_deliver().
  750. */
  751. signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
  752. signal->group_stop_count = 0;
  753. signal->group_exit_code = 0;
  754. }
  755. }
  756. return !sig_ignored(p, sig, force);
  757. }
  758. /*
  759. * Test if P wants to take SIG. After we've checked all threads with this,
  760. * it's equivalent to finding no threads not blocking SIG. Any threads not
  761. * blocking SIG were ruled out because they are not running and already
  762. * have pending signals. Such threads will dequeue from the shared queue
  763. * as soon as they're available, so putting the signal on the shared queue
  764. * will be equivalent to sending it to one such thread.
  765. */
  766. static inline int wants_signal(int sig, struct task_struct *p)
  767. {
  768. if (sigismember(&p->blocked, sig))
  769. return 0;
  770. if (p->flags & PF_EXITING)
  771. return 0;
  772. if (sig == SIGKILL)
  773. return 1;
  774. if (task_is_stopped_or_traced(p))
  775. return 0;
  776. return task_curr(p) || !signal_pending(p);
  777. }
  778. static void complete_signal(int sig, struct task_struct *p, int group)
  779. {
  780. struct signal_struct *signal = p->signal;
  781. struct task_struct *t;
  782. /*
  783. * Now find a thread we can wake up to take the signal off the queue.
  784. *
  785. * If the main thread wants the signal, it gets first crack.
  786. * Probably the least surprising to the average bear.
  787. */
  788. if (wants_signal(sig, p))
  789. t = p;
  790. else if (!group || thread_group_empty(p))
  791. /*
  792. * There is just one thread and it does not need to be woken.
  793. * It will dequeue unblocked signals before it runs again.
  794. */
  795. return;
  796. else {
  797. /*
  798. * Otherwise try to find a suitable thread.
  799. */
  800. t = signal->curr_target;
  801. while (!wants_signal(sig, t)) {
  802. t = next_thread(t);
  803. if (t == signal->curr_target)
  804. /*
  805. * No thread needs to be woken.
  806. * Any eligible threads will see
  807. * the signal in the queue soon.
  808. */
  809. return;
  810. }
  811. signal->curr_target = t;
  812. }
  813. /*
  814. * Found a killable thread. If the signal will be fatal,
  815. * then start taking the whole group down immediately.
  816. */
  817. if (sig_fatal(p, sig) &&
  818. !(signal->flags & SIGNAL_GROUP_EXIT) &&
  819. !sigismember(&t->real_blocked, sig) &&
  820. (sig == SIGKILL || !p->ptrace)) {
  821. /*
  822. * This signal will be fatal to the whole group.
  823. */
  824. if (!sig_kernel_coredump(sig)) {
  825. /*
  826. * Start a group exit and wake everybody up.
  827. * This way we don't have other threads
  828. * running and doing things after a slower
  829. * thread has the fatal signal pending.
  830. */
  831. signal->flags = SIGNAL_GROUP_EXIT;
  832. signal->group_exit_code = sig;
  833. signal->group_stop_count = 0;
  834. t = p;
  835. do {
  836. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  837. sigaddset(&t->pending.signal, SIGKILL);
  838. signal_wake_up(t, 1);
  839. } while_each_thread(p, t);
  840. return;
  841. }
  842. }
  843. /*
  844. * The signal is already in the shared-pending queue.
  845. * Tell the chosen thread to wake up and dequeue it.
  846. */
  847. signal_wake_up(t, sig == SIGKILL);
  848. return;
  849. }
  850. static inline int legacy_queue(struct sigpending *signals, int sig)
  851. {
  852. return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
  853. }
  854. #ifdef CONFIG_USER_NS
  855. static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
  856. {
  857. if (current_user_ns() == task_cred_xxx(t, user_ns))
  858. return;
  859. if (SI_FROMKERNEL(info))
  860. return;
  861. rcu_read_lock();
  862. info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
  863. make_kuid(current_user_ns(), info->si_uid));
  864. rcu_read_unlock();
  865. }
  866. #else
  867. static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
  868. {
  869. return;
  870. }
  871. #endif
  872. static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
  873. int group, int from_ancestor_ns)
  874. {
  875. struct sigpending *pending;
  876. struct sigqueue *q;
  877. int override_rlimit;
  878. int ret = 0, result;
  879. assert_spin_locked(&t->sighand->siglock);
  880. result = TRACE_SIGNAL_IGNORED;
  881. if (!prepare_signal(sig, t,
  882. from_ancestor_ns || (info == SEND_SIG_FORCED)))
  883. goto ret;
  884. pending = group ? &t->signal->shared_pending : &t->pending;
  885. /*
  886. * Short-circuit ignored signals and support queuing
  887. * exactly one non-rt signal, so that we can get more
  888. * detailed information about the cause of the signal.
  889. */
  890. result = TRACE_SIGNAL_ALREADY_PENDING;
  891. if (legacy_queue(pending, sig))
  892. goto ret;
  893. result = TRACE_SIGNAL_DELIVERED;
  894. /*
  895. * fast-pathed signals for kernel-internal things like SIGSTOP
  896. * or SIGKILL.
  897. */
  898. if (info == SEND_SIG_FORCED)
  899. goto out_set;
  900. /*
  901. * Real-time signals must be queued if sent by sigqueue, or
  902. * some other real-time mechanism. It is implementation
  903. * defined whether kill() does so. We attempt to do so, on
  904. * the principle of least surprise, but since kill is not
  905. * allowed to fail with EAGAIN when low on memory we just
  906. * make sure at least one signal gets delivered and don't
  907. * pass on the info struct.
  908. */
  909. if (sig < SIGRTMIN)
  910. override_rlimit = (is_si_special(info) || info->si_code >= 0);
  911. else
  912. override_rlimit = 0;
  913. q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
  914. if (q) {
  915. list_add_tail(&q->list, &pending->list);
  916. switch ((unsigned long) info) {
  917. case (unsigned long) SEND_SIG_NOINFO:
  918. q->info.si_signo = sig;
  919. q->info.si_errno = 0;
  920. q->info.si_code = SI_USER;
  921. q->info.si_pid = task_tgid_nr_ns(current,
  922. task_active_pid_ns(t));
  923. q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  924. break;
  925. case (unsigned long) SEND_SIG_PRIV:
  926. q->info.si_signo = sig;
  927. q->info.si_errno = 0;
  928. q->info.si_code = SI_KERNEL;
  929. q->info.si_pid = 0;
  930. q->info.si_uid = 0;
  931. break;
  932. default:
  933. copy_siginfo(&q->info, info);
  934. if (from_ancestor_ns)
  935. q->info.si_pid = 0;
  936. break;
  937. }
  938. userns_fixup_signal_uid(&q->info, t);
  939. } else if (!is_si_special(info)) {
  940. if (sig >= SIGRTMIN && info->si_code != SI_USER) {
  941. /*
  942. * Queue overflow, abort. We may abort if the
  943. * signal was rt and sent by user using something
  944. * other than kill().
  945. */
  946. result = TRACE_SIGNAL_OVERFLOW_FAIL;
  947. ret = -EAGAIN;
  948. goto ret;
  949. } else {
  950. /*
  951. * This is a silent loss of information. We still
  952. * send the signal, but the *info bits are lost.
  953. */
  954. result = TRACE_SIGNAL_LOSE_INFO;
  955. }
  956. }
  957. out_set:
  958. signalfd_notify(t, sig);
  959. sigaddset(&pending->signal, sig);
  960. complete_signal(sig, t, group);
  961. ret:
  962. trace_signal_generate(sig, info, t, group, result);
  963. return ret;
  964. }
  965. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  966. int group)
  967. {
  968. int from_ancestor_ns = 0;
  969. #ifdef CONFIG_PID_NS
  970. from_ancestor_ns = si_fromuser(info) &&
  971. !task_pid_nr_ns(current, task_active_pid_ns(t));
  972. #endif
  973. return __send_signal(sig, info, t, group, from_ancestor_ns);
  974. }
  975. static void print_fatal_signal(int signr)
  976. {
  977. struct pt_regs *regs = signal_pt_regs();
  978. pr_info("potentially unexpected fatal signal %d.\n", signr);
  979. #if defined(__i386__) && !defined(__arch_um__)
  980. pr_info("code at %08lx: ", regs->ip);
  981. {
  982. int i;
  983. for (i = 0; i < 16; i++) {
  984. unsigned char insn;
  985. if (get_user(insn, (unsigned char *)(regs->ip + i)))
  986. break;
  987. pr_cont("%02x ", insn);
  988. }
  989. }
  990. pr_cont("\n");
  991. #endif
  992. preempt_disable();
  993. show_regs(regs);
  994. preempt_enable();
  995. }
  996. static int __init setup_print_fatal_signals(char *str)
  997. {
  998. get_option (&str, &print_fatal_signals);
  999. return 1;
  1000. }
  1001. __setup("print-fatal-signals=", setup_print_fatal_signals);
  1002. int
  1003. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1004. {
  1005. return send_signal(sig, info, p, 1);
  1006. }
  1007. static int
  1008. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1009. {
  1010. return send_signal(sig, info, t, 0);
  1011. }
  1012. int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  1013. bool group)
  1014. {
  1015. unsigned long flags;
  1016. int ret = -ESRCH;
  1017. if (lock_task_sighand(p, &flags)) {
  1018. ret = send_signal(sig, info, p, group);
  1019. unlock_task_sighand(p, &flags);
  1020. }
  1021. return ret;
  1022. }
  1023. /*
  1024. * Force a signal that the process can't ignore: if necessary
  1025. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  1026. *
  1027. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  1028. * since we do not want to have a signal handler that was blocked
  1029. * be invoked when user space had explicitly blocked it.
  1030. *
  1031. * We don't want to have recursive SIGSEGV's etc, for example,
  1032. * that is why we also clear SIGNAL_UNKILLABLE.
  1033. */
  1034. int
  1035. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1036. {
  1037. unsigned long int flags;
  1038. int ret, blocked, ignored;
  1039. struct k_sigaction *action;
  1040. spin_lock_irqsave(&t->sighand->siglock, flags);
  1041. action = &t->sighand->action[sig-1];
  1042. ignored = action->sa.sa_handler == SIG_IGN;
  1043. blocked = sigismember(&t->blocked, sig);
  1044. if (blocked || ignored) {
  1045. action->sa.sa_handler = SIG_DFL;
  1046. if (blocked) {
  1047. sigdelset(&t->blocked, sig);
  1048. recalc_sigpending_and_wake(t);
  1049. }
  1050. }
  1051. /*
  1052. * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
  1053. * debugging to leave init killable.
  1054. */
  1055. if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
  1056. t->signal->flags &= ~SIGNAL_UNKILLABLE;
  1057. ret = specific_send_sig_info(sig, info, t);
  1058. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  1059. return ret;
  1060. }
  1061. /*
  1062. * Nuke all other threads in the group.
  1063. */
  1064. int zap_other_threads(struct task_struct *p)
  1065. {
  1066. struct task_struct *t = p;
  1067. int count = 0;
  1068. p->signal->group_stop_count = 0;
  1069. while_each_thread(p, t) {
  1070. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1071. count++;
  1072. /* Don't bother with already dead threads */
  1073. if (t->exit_state)
  1074. continue;
  1075. sigaddset(&t->pending.signal, SIGKILL);
  1076. signal_wake_up(t, 1);
  1077. }
  1078. return count;
  1079. }
  1080. struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  1081. unsigned long *flags)
  1082. {
  1083. struct sighand_struct *sighand;
  1084. for (;;) {
  1085. /*
  1086. * Disable interrupts early to avoid deadlocks.
  1087. * See rcu_read_unlock() comment header for details.
  1088. */
  1089. local_irq_save(*flags);
  1090. rcu_read_lock();
  1091. sighand = rcu_dereference(tsk->sighand);
  1092. if (unlikely(sighand == NULL)) {
  1093. rcu_read_unlock();
  1094. local_irq_restore(*flags);
  1095. break;
  1096. }
  1097. /*
  1098. * This sighand can be already freed and even reused, but
  1099. * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
  1100. * initializes ->siglock: this slab can't go away, it has
  1101. * the same object type, ->siglock can't be reinitialized.
  1102. *
  1103. * We need to ensure that tsk->sighand is still the same
  1104. * after we take the lock, we can race with de_thread() or
  1105. * __exit_signal(). In the latter case the next iteration
  1106. * must see ->sighand == NULL.
  1107. */
  1108. spin_lock(&sighand->siglock);
  1109. if (likely(sighand == tsk->sighand)) {
  1110. rcu_read_unlock();
  1111. break;
  1112. }
  1113. spin_unlock(&sighand->siglock);
  1114. rcu_read_unlock();
  1115. local_irq_restore(*flags);
  1116. }
  1117. return sighand;
  1118. }
  1119. /*
  1120. * send signal info to all the members of a group
  1121. */
  1122. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1123. {
  1124. int ret;
  1125. rcu_read_lock();
  1126. ret = check_kill_permission(sig, info, p);
  1127. rcu_read_unlock();
  1128. if (!ret && sig)
  1129. ret = do_send_sig_info(sig, info, p, true);
  1130. return ret;
  1131. }
  1132. /*
  1133. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1134. * control characters do (^C, ^Z etc)
  1135. * - the caller must hold at least a readlock on tasklist_lock
  1136. */
  1137. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1138. {
  1139. struct task_struct *p = NULL;
  1140. int retval, success;
  1141. success = 0;
  1142. retval = -ESRCH;
  1143. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1144. int err = group_send_sig_info(sig, info, p);
  1145. success |= !err;
  1146. retval = err;
  1147. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1148. return success ? 0 : retval;
  1149. }
  1150. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1151. {
  1152. int error = -ESRCH;
  1153. struct task_struct *p;
  1154. for (;;) {
  1155. rcu_read_lock();
  1156. p = pid_task(pid, PIDTYPE_PID);
  1157. if (p)
  1158. error = group_send_sig_info(sig, info, p);
  1159. rcu_read_unlock();
  1160. if (likely(!p || error != -ESRCH))
  1161. return error;
  1162. /*
  1163. * The task was unhashed in between, try again. If it
  1164. * is dead, pid_task() will return NULL, if we race with
  1165. * de_thread() it will find the new leader.
  1166. */
  1167. }
  1168. }
  1169. static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1170. {
  1171. int error;
  1172. rcu_read_lock();
  1173. error = kill_pid_info(sig, info, find_vpid(pid));
  1174. rcu_read_unlock();
  1175. return error;
  1176. }
  1177. static int kill_as_cred_perm(const struct cred *cred,
  1178. struct task_struct *target)
  1179. {
  1180. const struct cred *pcred = __task_cred(target);
  1181. if (!uid_eq(cred->euid, pcred->suid) && !uid_eq(cred->euid, pcred->uid) &&
  1182. !uid_eq(cred->uid, pcred->suid) && !uid_eq(cred->uid, pcred->uid))
  1183. return 0;
  1184. return 1;
  1185. }
  1186. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1187. int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
  1188. const struct cred *cred, u32 secid)
  1189. {
  1190. int ret = -EINVAL;
  1191. struct task_struct *p;
  1192. unsigned long flags;
  1193. if (!valid_signal(sig))
  1194. return ret;
  1195. rcu_read_lock();
  1196. p = pid_task(pid, PIDTYPE_PID);
  1197. if (!p) {
  1198. ret = -ESRCH;
  1199. goto out_unlock;
  1200. }
  1201. if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
  1202. ret = -EPERM;
  1203. goto out_unlock;
  1204. }
  1205. ret = security_task_kill(p, info, sig, secid);
  1206. if (ret)
  1207. goto out_unlock;
  1208. if (sig) {
  1209. if (lock_task_sighand(p, &flags)) {
  1210. ret = __send_signal(sig, info, p, 1, 0);
  1211. unlock_task_sighand(p, &flags);
  1212. } else
  1213. ret = -ESRCH;
  1214. }
  1215. out_unlock:
  1216. rcu_read_unlock();
  1217. return ret;
  1218. }
  1219. EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
  1220. /*
  1221. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1222. *
  1223. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1224. * is probably wrong. Should make it like BSD or SYSV.
  1225. */
  1226. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1227. {
  1228. int ret;
  1229. if (pid > 0) {
  1230. rcu_read_lock();
  1231. ret = kill_pid_info(sig, info, find_vpid(pid));
  1232. rcu_read_unlock();
  1233. return ret;
  1234. }
  1235. /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
  1236. if (pid == INT_MIN)
  1237. return -ESRCH;
  1238. read_lock(&tasklist_lock);
  1239. if (pid != -1) {
  1240. ret = __kill_pgrp_info(sig, info,
  1241. pid ? find_vpid(-pid) : task_pgrp(current));
  1242. } else {
  1243. int retval = 0, count = 0;
  1244. struct task_struct * p;
  1245. for_each_process(p) {
  1246. if (task_pid_vnr(p) > 1 &&
  1247. !same_thread_group(p, current)) {
  1248. int err = group_send_sig_info(sig, info, p);
  1249. ++count;
  1250. if (err != -EPERM)
  1251. retval = err;
  1252. }
  1253. }
  1254. ret = count ? retval : -ESRCH;
  1255. }
  1256. read_unlock(&tasklist_lock);
  1257. return ret;
  1258. }
  1259. /*
  1260. * These are for backward compatibility with the rest of the kernel source.
  1261. */
  1262. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1263. {
  1264. /*
  1265. * Make sure legacy kernel users don't send in bad values
  1266. * (normal paths check this in check_kill_permission).
  1267. */
  1268. if (!valid_signal(sig))
  1269. return -EINVAL;
  1270. return do_send_sig_info(sig, info, p, false);
  1271. }
  1272. #define __si_special(priv) \
  1273. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1274. int
  1275. send_sig(int sig, struct task_struct *p, int priv)
  1276. {
  1277. return send_sig_info(sig, __si_special(priv), p);
  1278. }
  1279. void
  1280. force_sig(int sig, struct task_struct *p)
  1281. {
  1282. force_sig_info(sig, SEND_SIG_PRIV, p);
  1283. }
  1284. /*
  1285. * When things go south during signal handling, we
  1286. * will force a SIGSEGV. And if the signal that caused
  1287. * the problem was already a SIGSEGV, we'll want to
  1288. * make sure we don't even try to deliver the signal..
  1289. */
  1290. int
  1291. force_sigsegv(int sig, struct task_struct *p)
  1292. {
  1293. if (sig == SIGSEGV) {
  1294. unsigned long flags;
  1295. spin_lock_irqsave(&p->sighand->siglock, flags);
  1296. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1297. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1298. }
  1299. force_sig(SIGSEGV, p);
  1300. return 0;
  1301. }
  1302. int kill_pgrp(struct pid *pid, int sig, int priv)
  1303. {
  1304. int ret;
  1305. read_lock(&tasklist_lock);
  1306. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1307. read_unlock(&tasklist_lock);
  1308. return ret;
  1309. }
  1310. EXPORT_SYMBOL(kill_pgrp);
  1311. int kill_pid(struct pid *pid, int sig, int priv)
  1312. {
  1313. return kill_pid_info(sig, __si_special(priv), pid);
  1314. }
  1315. EXPORT_SYMBOL(kill_pid);
  1316. /*
  1317. * These functions support sending signals using preallocated sigqueue
  1318. * structures. This is needed "because realtime applications cannot
  1319. * afford to lose notifications of asynchronous events, like timer
  1320. * expirations or I/O completions". In the case of POSIX Timers
  1321. * we allocate the sigqueue structure from the timer_create. If this
  1322. * allocation fails we are able to report the failure to the application
  1323. * with an EAGAIN error.
  1324. */
  1325. struct sigqueue *sigqueue_alloc(void)
  1326. {
  1327. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1328. if (q)
  1329. q->flags |= SIGQUEUE_PREALLOC;
  1330. return q;
  1331. }
  1332. void sigqueue_free(struct sigqueue *q)
  1333. {
  1334. unsigned long flags;
  1335. spinlock_t *lock = &current->sighand->siglock;
  1336. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1337. /*
  1338. * We must hold ->siglock while testing q->list
  1339. * to serialize with collect_signal() or with
  1340. * __exit_signal()->flush_sigqueue().
  1341. */
  1342. spin_lock_irqsave(lock, flags);
  1343. q->flags &= ~SIGQUEUE_PREALLOC;
  1344. /*
  1345. * If it is queued it will be freed when dequeued,
  1346. * like the "regular" sigqueue.
  1347. */
  1348. if (!list_empty(&q->list))
  1349. q = NULL;
  1350. spin_unlock_irqrestore(lock, flags);
  1351. if (q)
  1352. __sigqueue_free(q);
  1353. }
  1354. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  1355. {
  1356. int sig = q->info.si_signo;
  1357. struct sigpending *pending;
  1358. unsigned long flags;
  1359. int ret, result;
  1360. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1361. ret = -1;
  1362. if (!likely(lock_task_sighand(t, &flags)))
  1363. goto ret;
  1364. ret = 1; /* the signal is ignored */
  1365. result = TRACE_SIGNAL_IGNORED;
  1366. if (!prepare_signal(sig, t, false))
  1367. goto out;
  1368. ret = 0;
  1369. if (unlikely(!list_empty(&q->list))) {
  1370. /*
  1371. * If an SI_TIMER entry is already queue just increment
  1372. * the overrun count.
  1373. */
  1374. BUG_ON(q->info.si_code != SI_TIMER);
  1375. q->info.si_overrun++;
  1376. result = TRACE_SIGNAL_ALREADY_PENDING;
  1377. goto out;
  1378. }
  1379. q->info.si_overrun = 0;
  1380. signalfd_notify(t, sig);
  1381. pending = group ? &t->signal->shared_pending : &t->pending;
  1382. list_add_tail(&q->list, &pending->list);
  1383. sigaddset(&pending->signal, sig);
  1384. complete_signal(sig, t, group);
  1385. result = TRACE_SIGNAL_DELIVERED;
  1386. out:
  1387. trace_signal_generate(sig, &q->info, t, group, result);
  1388. unlock_task_sighand(t, &flags);
  1389. ret:
  1390. return ret;
  1391. }
  1392. /*
  1393. * Let a parent know about the death of a child.
  1394. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1395. *
  1396. * Returns true if our parent ignored us and so we've switched to
  1397. * self-reaping.
  1398. */
  1399. bool do_notify_parent(struct task_struct *tsk, int sig)
  1400. {
  1401. struct siginfo info;
  1402. unsigned long flags;
  1403. struct sighand_struct *psig;
  1404. bool autoreap = false;
  1405. u64 utime, stime;
  1406. BUG_ON(sig == -1);
  1407. /* do_notify_parent_cldstop should have been called instead. */
  1408. BUG_ON(task_is_stopped_or_traced(tsk));
  1409. BUG_ON(!tsk->ptrace &&
  1410. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1411. if (sig != SIGCHLD) {
  1412. /*
  1413. * This is only possible if parent == real_parent.
  1414. * Check if it has changed security domain.
  1415. */
  1416. if (tsk->parent_exec_id != tsk->parent->self_exec_id)
  1417. sig = SIGCHLD;
  1418. }
  1419. info.si_signo = sig;
  1420. info.si_errno = 0;
  1421. /*
  1422. * We are under tasklist_lock here so our parent is tied to
  1423. * us and cannot change.
  1424. *
  1425. * task_active_pid_ns will always return the same pid namespace
  1426. * until a task passes through release_task.
  1427. *
  1428. * write_lock() currently calls preempt_disable() which is the
  1429. * same as rcu_read_lock(), but according to Oleg, this is not
  1430. * correct to rely on this
  1431. */
  1432. rcu_read_lock();
  1433. info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
  1434. info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
  1435. task_uid(tsk));
  1436. rcu_read_unlock();
  1437. task_cputime(tsk, &utime, &stime);
  1438. info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
  1439. info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
  1440. info.si_status = tsk->exit_code & 0x7f;
  1441. if (tsk->exit_code & 0x80)
  1442. info.si_code = CLD_DUMPED;
  1443. else if (tsk->exit_code & 0x7f)
  1444. info.si_code = CLD_KILLED;
  1445. else {
  1446. info.si_code = CLD_EXITED;
  1447. info.si_status = tsk->exit_code >> 8;
  1448. }
  1449. psig = tsk->parent->sighand;
  1450. spin_lock_irqsave(&psig->siglock, flags);
  1451. if (!tsk->ptrace && sig == SIGCHLD &&
  1452. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1453. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1454. /*
  1455. * We are exiting and our parent doesn't care. POSIX.1
  1456. * defines special semantics for setting SIGCHLD to SIG_IGN
  1457. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1458. * automatically and not left for our parent's wait4 call.
  1459. * Rather than having the parent do it as a magic kind of
  1460. * signal handler, we just set this to tell do_exit that we
  1461. * can be cleaned up without becoming a zombie. Note that
  1462. * we still call __wake_up_parent in this case, because a
  1463. * blocked sys_wait4 might now return -ECHILD.
  1464. *
  1465. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1466. * is implementation-defined: we do (if you don't want
  1467. * it, just use SIG_IGN instead).
  1468. */
  1469. autoreap = true;
  1470. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1471. sig = 0;
  1472. }
  1473. if (valid_signal(sig) && sig)
  1474. __group_send_sig_info(sig, &info, tsk->parent);
  1475. __wake_up_parent(tsk, tsk->parent);
  1476. spin_unlock_irqrestore(&psig->siglock, flags);
  1477. return autoreap;
  1478. }
  1479. /**
  1480. * do_notify_parent_cldstop - notify parent of stopped/continued state change
  1481. * @tsk: task reporting the state change
  1482. * @for_ptracer: the notification is for ptracer
  1483. * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
  1484. *
  1485. * Notify @tsk's parent that the stopped/continued state has changed. If
  1486. * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
  1487. * If %true, @tsk reports to @tsk->parent which should be the ptracer.
  1488. *
  1489. * CONTEXT:
  1490. * Must be called with tasklist_lock at least read locked.
  1491. */
  1492. static void do_notify_parent_cldstop(struct task_struct *tsk,
  1493. bool for_ptracer, int why)
  1494. {
  1495. struct siginfo info;
  1496. unsigned long flags;
  1497. struct task_struct *parent;
  1498. struct sighand_struct *sighand;
  1499. u64 utime, stime;
  1500. if (for_ptracer) {
  1501. parent = tsk->parent;
  1502. } else {
  1503. tsk = tsk->group_leader;
  1504. parent = tsk->real_parent;
  1505. }
  1506. info.si_signo = SIGCHLD;
  1507. info.si_errno = 0;
  1508. /*
  1509. * see comment in do_notify_parent() about the following 4 lines
  1510. */
  1511. rcu_read_lock();
  1512. info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
  1513. info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
  1514. rcu_read_unlock();
  1515. task_cputime(tsk, &utime, &stime);
  1516. info.si_utime = nsec_to_clock_t(utime);
  1517. info.si_stime = nsec_to_clock_t(stime);
  1518. info.si_code = why;
  1519. switch (why) {
  1520. case CLD_CONTINUED:
  1521. info.si_status = SIGCONT;
  1522. break;
  1523. case CLD_STOPPED:
  1524. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1525. break;
  1526. case CLD_TRAPPED:
  1527. info.si_status = tsk->exit_code & 0x7f;
  1528. break;
  1529. default:
  1530. BUG();
  1531. }
  1532. sighand = parent->sighand;
  1533. spin_lock_irqsave(&sighand->siglock, flags);
  1534. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1535. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1536. __group_send_sig_info(SIGCHLD, &info, parent);
  1537. /*
  1538. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1539. */
  1540. __wake_up_parent(tsk, parent);
  1541. spin_unlock_irqrestore(&sighand->siglock, flags);
  1542. }
  1543. static inline int may_ptrace_stop(void)
  1544. {
  1545. if (!likely(current->ptrace))
  1546. return 0;
  1547. /*
  1548. * Are we in the middle of do_coredump?
  1549. * If so and our tracer is also part of the coredump stopping
  1550. * is a deadlock situation, and pointless because our tracer
  1551. * is dead so don't allow us to stop.
  1552. * If SIGKILL was already sent before the caller unlocked
  1553. * ->siglock we must see ->core_state != NULL. Otherwise it
  1554. * is safe to enter schedule().
  1555. *
  1556. * This is almost outdated, a task with the pending SIGKILL can't
  1557. * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
  1558. * after SIGKILL was already dequeued.
  1559. */
  1560. if (unlikely(current->mm->core_state) &&
  1561. unlikely(current->mm == current->parent->mm))
  1562. return 0;
  1563. return 1;
  1564. }
  1565. /*
  1566. * Return non-zero if there is a SIGKILL that should be waking us up.
  1567. * Called with the siglock held.
  1568. */
  1569. static int sigkill_pending(struct task_struct *tsk)
  1570. {
  1571. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1572. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1573. }
  1574. /*
  1575. * This must be called with current->sighand->siglock held.
  1576. *
  1577. * This should be the path for all ptrace stops.
  1578. * We always set current->last_siginfo while stopped here.
  1579. * That makes it a way to test a stopped process for
  1580. * being ptrace-stopped vs being job-control-stopped.
  1581. *
  1582. * If we actually decide not to stop at all because the tracer
  1583. * is gone, we keep current->exit_code unless clear_code.
  1584. */
  1585. static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  1586. __releases(&current->sighand->siglock)
  1587. __acquires(&current->sighand->siglock)
  1588. {
  1589. bool gstop_done = false;
  1590. if (arch_ptrace_stop_needed(exit_code, info)) {
  1591. /*
  1592. * The arch code has something special to do before a
  1593. * ptrace stop. This is allowed to block, e.g. for faults
  1594. * on user stack pages. We can't keep the siglock while
  1595. * calling arch_ptrace_stop, so we must release it now.
  1596. * To preserve proper semantics, we must do this before
  1597. * any signal bookkeeping like checking group_stop_count.
  1598. * Meanwhile, a SIGKILL could come in before we retake the
  1599. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1600. * So after regaining the lock, we must check for SIGKILL.
  1601. */
  1602. spin_unlock_irq(&current->sighand->siglock);
  1603. arch_ptrace_stop(exit_code, info);
  1604. spin_lock_irq(&current->sighand->siglock);
  1605. if (sigkill_pending(current))
  1606. return;
  1607. }
  1608. /*
  1609. * We're committing to trapping. TRACED should be visible before
  1610. * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
  1611. * Also, transition to TRACED and updates to ->jobctl should be
  1612. * atomic with respect to siglock and should be done after the arch
  1613. * hook as siglock is released and regrabbed across it.
  1614. */
  1615. set_current_state(TASK_TRACED);
  1616. current->last_siginfo = info;
  1617. current->exit_code = exit_code;
  1618. /*
  1619. * If @why is CLD_STOPPED, we're trapping to participate in a group
  1620. * stop. Do the bookkeeping. Note that if SIGCONT was delievered
  1621. * across siglock relocks since INTERRUPT was scheduled, PENDING
  1622. * could be clear now. We act as if SIGCONT is received after
  1623. * TASK_TRACED is entered - ignore it.
  1624. */
  1625. if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
  1626. gstop_done = task_participate_group_stop(current);
  1627. /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
  1628. task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1629. if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
  1630. task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
  1631. /* entering a trap, clear TRAPPING */
  1632. task_clear_jobctl_trapping(current);
  1633. spin_unlock_irq(&current->sighand->siglock);
  1634. read_lock(&tasklist_lock);
  1635. if (may_ptrace_stop()) {
  1636. /*
  1637. * Notify parents of the stop.
  1638. *
  1639. * While ptraced, there are two parents - the ptracer and
  1640. * the real_parent of the group_leader. The ptracer should
  1641. * know about every stop while the real parent is only
  1642. * interested in the completion of group stop. The states
  1643. * for the two don't interact with each other. Notify
  1644. * separately unless they're gonna be duplicates.
  1645. */
  1646. do_notify_parent_cldstop(current, true, why);
  1647. if (gstop_done && ptrace_reparented(current))
  1648. do_notify_parent_cldstop(current, false, why);
  1649. /*
  1650. * Don't want to allow preemption here, because
  1651. * sys_ptrace() needs this task to be inactive.
  1652. *
  1653. * XXX: implement read_unlock_no_resched().
  1654. */
  1655. preempt_disable();
  1656. read_unlock(&tasklist_lock);
  1657. preempt_enable_no_resched();
  1658. freezable_schedule();
  1659. } else {
  1660. /*
  1661. * By the time we got the lock, our tracer went away.
  1662. * Don't drop the lock yet, another tracer may come.
  1663. *
  1664. * If @gstop_done, the ptracer went away between group stop
  1665. * completion and here. During detach, it would have set
  1666. * JOBCTL_STOP_PENDING on us and we'll re-enter
  1667. * TASK_STOPPED in do_signal_stop() on return, so notifying
  1668. * the real parent of the group stop completion is enough.
  1669. */
  1670. if (gstop_done)
  1671. do_notify_parent_cldstop(current, false, why);
  1672. /* tasklist protects us from ptrace_freeze_traced() */
  1673. __set_current_state(TASK_RUNNING);
  1674. if (clear_code)
  1675. current->exit_code = 0;
  1676. read_unlock(&tasklist_lock);
  1677. }
  1678. /*
  1679. * We are back. Now reacquire the siglock before touching
  1680. * last_siginfo, so that we are sure to have synchronized with
  1681. * any signal-sending on another CPU that wants to examine it.
  1682. */
  1683. spin_lock_irq(&current->sighand->siglock);
  1684. current->last_siginfo = NULL;
  1685. /* LISTENING can be set only during STOP traps, clear it */
  1686. current->jobctl &= ~JOBCTL_LISTENING;
  1687. /*
  1688. * Queued signals ignored us while we were stopped for tracing.
  1689. * So check for any that we should take before resuming user mode.
  1690. * This sets TIF_SIGPENDING, but never clears it.
  1691. */
  1692. recalc_sigpending_tsk(current);
  1693. }
  1694. static void ptrace_do_notify(int signr, int exit_code, int why)
  1695. {
  1696. siginfo_t info;
  1697. memset(&info, 0, sizeof info);
  1698. info.si_signo = signr;
  1699. info.si_code = exit_code;
  1700. info.si_pid = task_pid_vnr(current);
  1701. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  1702. /* Let the debugger run. */
  1703. ptrace_stop(exit_code, why, 1, &info);
  1704. }
  1705. void ptrace_notify(int exit_code)
  1706. {
  1707. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1708. if (unlikely(current->task_works))
  1709. task_work_run();
  1710. spin_lock_irq(&current->sighand->siglock);
  1711. ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
  1712. spin_unlock_irq(&current->sighand->siglock);
  1713. }
  1714. /**
  1715. * do_signal_stop - handle group stop for SIGSTOP and other stop signals
  1716. * @signr: signr causing group stop if initiating
  1717. *
  1718. * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
  1719. * and participate in it. If already set, participate in the existing
  1720. * group stop. If participated in a group stop (and thus slept), %true is
  1721. * returned with siglock released.
  1722. *
  1723. * If ptraced, this function doesn't handle stop itself. Instead,
  1724. * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
  1725. * untouched. The caller must ensure that INTERRUPT trap handling takes
  1726. * places afterwards.
  1727. *
  1728. * CONTEXT:
  1729. * Must be called with @current->sighand->siglock held, which is released
  1730. * on %true return.
  1731. *
  1732. * RETURNS:
  1733. * %false if group stop is already cancelled or ptrace trap is scheduled.
  1734. * %true if participated in group stop.
  1735. */
  1736. static bool do_signal_stop(int signr)
  1737. __releases(&current->sighand->siglock)
  1738. {
  1739. struct signal_struct *sig = current->signal;
  1740. if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
  1741. unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
  1742. struct task_struct *t;
  1743. /* signr will be recorded in task->jobctl for retries */
  1744. WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
  1745. if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
  1746. unlikely(signal_group_exit(sig)))
  1747. return false;
  1748. /*
  1749. * There is no group stop already in progress. We must
  1750. * initiate one now.
  1751. *
  1752. * While ptraced, a task may be resumed while group stop is
  1753. * still in effect and then receive a stop signal and
  1754. * initiate another group stop. This deviates from the
  1755. * usual behavior as two consecutive stop signals can't
  1756. * cause two group stops when !ptraced. That is why we
  1757. * also check !task_is_stopped(t) below.
  1758. *
  1759. * The condition can be distinguished by testing whether
  1760. * SIGNAL_STOP_STOPPED is already set. Don't generate
  1761. * group_exit_code in such case.
  1762. *
  1763. * This is not necessary for SIGNAL_STOP_CONTINUED because
  1764. * an intervening stop signal is required to cause two
  1765. * continued events regardless of ptrace.
  1766. */
  1767. if (!(sig->flags & SIGNAL_STOP_STOPPED))
  1768. sig->group_exit_code = signr;
  1769. sig->group_stop_count = 0;
  1770. if (task_set_jobctl_pending(current, signr | gstop))
  1771. sig->group_stop_count++;
  1772. t = current;
  1773. while_each_thread(current, t) {
  1774. /*
  1775. * Setting state to TASK_STOPPED for a group
  1776. * stop is always done with the siglock held,
  1777. * so this check has no races.
  1778. */
  1779. if (!task_is_stopped(t) &&
  1780. task_set_jobctl_pending(t, signr | gstop)) {
  1781. sig->group_stop_count++;
  1782. if (likely(!(t->ptrace & PT_SEIZED)))
  1783. signal_wake_up(t, 0);
  1784. else
  1785. ptrace_trap_notify(t);
  1786. }
  1787. }
  1788. }
  1789. if (likely(!current->ptrace)) {
  1790. int notify = 0;
  1791. /*
  1792. * If there are no other threads in the group, or if there
  1793. * is a group stop in progress and we are the last to stop,
  1794. * report to the parent.
  1795. */
  1796. if (task_participate_group_stop(current))
  1797. notify = CLD_STOPPED;
  1798. __set_current_state(TASK_STOPPED);
  1799. spin_unlock_irq(&current->sighand->siglock);
  1800. /*
  1801. * Notify the parent of the group stop completion. Because
  1802. * we're not holding either the siglock or tasklist_lock
  1803. * here, ptracer may attach inbetween; however, this is for
  1804. * group stop and should always be delivered to the real
  1805. * parent of the group leader. The new ptracer will get
  1806. * its notification when this task transitions into
  1807. * TASK_TRACED.
  1808. */
  1809. if (notify) {
  1810. read_lock(&tasklist_lock);
  1811. do_notify_parent_cldstop(current, false, notify);
  1812. read_unlock(&tasklist_lock);
  1813. }
  1814. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1815. freezable_schedule();
  1816. return true;
  1817. } else {
  1818. /*
  1819. * While ptraced, group stop is handled by STOP trap.
  1820. * Schedule it and let the caller deal with it.
  1821. */
  1822. task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1823. return false;
  1824. }
  1825. }
  1826. /**
  1827. * do_jobctl_trap - take care of ptrace jobctl traps
  1828. *
  1829. * When PT_SEIZED, it's used for both group stop and explicit
  1830. * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
  1831. * accompanying siginfo. If stopped, lower eight bits of exit_code contain
  1832. * the stop signal; otherwise, %SIGTRAP.
  1833. *
  1834. * When !PT_SEIZED, it's used only for group stop trap with stop signal
  1835. * number as exit_code and no siginfo.
  1836. *
  1837. * CONTEXT:
  1838. * Must be called with @current->sighand->siglock held, which may be
  1839. * released and re-acquired before returning with intervening sleep.
  1840. */
  1841. static void do_jobctl_trap(void)
  1842. {
  1843. struct signal_struct *signal = current->signal;
  1844. int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
  1845. if (current->ptrace & PT_SEIZED) {
  1846. if (!signal->group_stop_count &&
  1847. !(signal->flags & SIGNAL_STOP_STOPPED))
  1848. signr = SIGTRAP;
  1849. WARN_ON_ONCE(!signr);
  1850. ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
  1851. CLD_STOPPED);
  1852. } else {
  1853. WARN_ON_ONCE(!signr);
  1854. ptrace_stop(signr, CLD_STOPPED, 0, NULL);
  1855. current->exit_code = 0;
  1856. }
  1857. }
  1858. static int ptrace_signal(int signr, siginfo_t *info)
  1859. {
  1860. /*
  1861. * We do not check sig_kernel_stop(signr) but set this marker
  1862. * unconditionally because we do not know whether debugger will
  1863. * change signr. This flag has no meaning unless we are going
  1864. * to stop after return from ptrace_stop(). In this case it will
  1865. * be checked in do_signal_stop(), we should only stop if it was
  1866. * not cleared by SIGCONT while we were sleeping. See also the
  1867. * comment in dequeue_signal().
  1868. */
  1869. current->jobctl |= JOBCTL_STOP_DEQUEUED;
  1870. ptrace_stop(signr, CLD_TRAPPED, 0, info);
  1871. /* We're back. Did the debugger cancel the sig? */
  1872. signr = current->exit_code;
  1873. if (signr == 0)
  1874. return signr;
  1875. current->exit_code = 0;
  1876. /*
  1877. * Update the siginfo structure if the signal has
  1878. * changed. If the debugger wanted something
  1879. * specific in the siginfo structure then it should
  1880. * have updated *info via PTRACE_SETSIGINFO.
  1881. */
  1882. if (signr != info->si_signo) {
  1883. info->si_signo = signr;
  1884. info->si_errno = 0;
  1885. info->si_code = SI_USER;
  1886. rcu_read_lock();
  1887. info->si_pid = task_pid_vnr(current->parent);
  1888. info->si_uid = from_kuid_munged(current_user_ns(),
  1889. task_uid(current->parent));
  1890. rcu_read_unlock();
  1891. }
  1892. /* If the (new) signal is now blocked, requeue it. */
  1893. if (sigismember(&current->blocked, signr)) {
  1894. specific_send_sig_info(signr, info, current);
  1895. signr = 0;
  1896. }
  1897. return signr;
  1898. }
  1899. int get_signal(struct ksignal *ksig)
  1900. {
  1901. struct sighand_struct *sighand = current->sighand;
  1902. struct signal_struct *signal = current->signal;
  1903. int signr;
  1904. if (unlikely(current->task_works))
  1905. task_work_run();
  1906. if (unlikely(uprobe_deny_signal()))
  1907. return 0;
  1908. /*
  1909. * Do this once, we can't return to user-mode if freezing() == T.
  1910. * do_signal_stop() and ptrace_stop() do freezable_schedule() and
  1911. * thus do not need another check after return.
  1912. */
  1913. try_to_freeze();
  1914. relock:
  1915. spin_lock_irq(&sighand->siglock);
  1916. /*
  1917. * Every stopped thread goes here after wakeup. Check to see if
  1918. * we should notify the parent, prepare_signal(SIGCONT) encodes
  1919. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  1920. */
  1921. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1922. int why;
  1923. if (signal->flags & SIGNAL_CLD_CONTINUED)
  1924. why = CLD_CONTINUED;
  1925. else
  1926. why = CLD_STOPPED;
  1927. signal->flags &= ~SIGNAL_CLD_MASK;
  1928. spin_unlock_irq(&sighand->siglock);
  1929. /*
  1930. * Notify the parent that we're continuing. This event is
  1931. * always per-process and doesn't make whole lot of sense
  1932. * for ptracers, who shouldn't consume the state via
  1933. * wait(2) either, but, for backward compatibility, notify
  1934. * the ptracer of the group leader too unless it's gonna be
  1935. * a duplicate.
  1936. */
  1937. read_lock(&tasklist_lock);
  1938. do_notify_parent_cldstop(current, false, why);
  1939. if (ptrace_reparented(current->group_leader))
  1940. do_notify_parent_cldstop(current->group_leader,
  1941. true, why);
  1942. read_unlock(&tasklist_lock);
  1943. goto relock;
  1944. }
  1945. for (;;) {
  1946. struct k_sigaction *ka;
  1947. if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
  1948. do_signal_stop(0))
  1949. goto relock;
  1950. if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
  1951. do_jobctl_trap();
  1952. spin_unlock_irq(&sighand->siglock);
  1953. goto relock;
  1954. }
  1955. signr = dequeue_signal(current, &current->blocked, &ksig->info);
  1956. if (!signr)
  1957. break; /* will return 0 */
  1958. if (unlikely(current->ptrace) && signr != SIGKILL) {
  1959. signr = ptrace_signal(signr, &ksig->info);
  1960. if (!signr)
  1961. continue;
  1962. }
  1963. ka = &sighand->action[signr-1];
  1964. /* Trace actually delivered signals. */
  1965. trace_signal_deliver(signr, &ksig->info, ka);
  1966. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1967. continue;
  1968. if (ka->sa.sa_handler != SIG_DFL) {
  1969. /* Run the handler. */
  1970. ksig->ka = *ka;
  1971. if (ka->sa.sa_flags & SA_ONESHOT)
  1972. ka->sa.sa_handler = SIG_DFL;
  1973. break; /* will return non-zero "signr" value */
  1974. }
  1975. /*
  1976. * Now we are doing the default action for this signal.
  1977. */
  1978. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1979. continue;
  1980. /*
  1981. * Global init gets no signals it doesn't want.
  1982. * Container-init gets no signals it doesn't want from same
  1983. * container.
  1984. *
  1985. * Note that if global/container-init sees a sig_kernel_only()
  1986. * signal here, the signal must have been generated internally
  1987. * or must have come from an ancestor namespace. In either
  1988. * case, the signal cannot be dropped.
  1989. */
  1990. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  1991. !sig_kernel_only(signr))
  1992. continue;
  1993. if (sig_kernel_stop(signr)) {
  1994. /*
  1995. * The default action is to stop all threads in
  1996. * the thread group. The job control signals
  1997. * do nothing in an orphaned pgrp, but SIGSTOP
  1998. * always works. Note that siglock needs to be
  1999. * dropped during the call to is_orphaned_pgrp()
  2000. * because of lock ordering with tasklist_lock.
  2001. * This allows an intervening SIGCONT to be posted.
  2002. * We need to check for that and bail out if necessary.
  2003. */
  2004. if (signr != SIGSTOP) {
  2005. spin_unlock_irq(&sighand->siglock);
  2006. /* signals can be posted during this window */
  2007. if (is_current_pgrp_orphaned())
  2008. goto relock;
  2009. spin_lock_irq(&sighand->siglock);
  2010. }
  2011. if (likely(do_signal_stop(ksig->info.si_signo))) {
  2012. /* It released the siglock. */
  2013. goto relock;
  2014. }
  2015. /*
  2016. * We didn't actually stop, due to a race
  2017. * with SIGCONT or something like that.
  2018. */
  2019. continue;
  2020. }
  2021. spin_unlock_irq(&sighand->siglock);
  2022. /*
  2023. * Anything else is fatal, maybe with a core dump.
  2024. */
  2025. current->flags |= PF_SIGNALED;
  2026. if (sig_kernel_coredump(signr)) {
  2027. if (print_fatal_signals)
  2028. print_fatal_signal(ksig->info.si_signo);
  2029. proc_coredump_connector(current);
  2030. /*
  2031. * If it was able to dump core, this kills all
  2032. * other threads in the group and synchronizes with
  2033. * their demise. If we lost the race with another
  2034. * thread getting here, it set group_exit_code
  2035. * first and our do_group_exit call below will use
  2036. * that value and ignore the one we pass it.
  2037. */
  2038. do_coredump(&ksig->info);
  2039. }
  2040. /*
  2041. * Death signals, no core dump.
  2042. */
  2043. do_group_exit(ksig->info.si_signo);
  2044. /* NOTREACHED */
  2045. }
  2046. spin_unlock_irq(&sighand->siglock);
  2047. ksig->sig = signr;
  2048. return ksig->sig > 0;
  2049. }
  2050. /**
  2051. * signal_delivered -
  2052. * @ksig: kernel signal struct
  2053. * @stepping: nonzero if debugger single-step or block-step in use
  2054. *
  2055. * This function should be called when a signal has successfully been
  2056. * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
  2057. * is always blocked, and the signal itself is blocked unless %SA_NODEFER
  2058. * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
  2059. */
  2060. static void signal_delivered(struct ksignal *ksig, int stepping)
  2061. {
  2062. sigset_t blocked;
  2063. /* A signal was successfully delivered, and the
  2064. saved sigmask was stored on the signal frame,
  2065. and will be restored by sigreturn. So we can
  2066. simply clear the restore sigmask flag. */
  2067. clear_restore_sigmask();
  2068. sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
  2069. if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
  2070. sigaddset(&blocked, ksig->sig);
  2071. set_current_blocked(&blocked);
  2072. tracehook_signal_handler(stepping);
  2073. }
  2074. void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
  2075. {
  2076. if (failed)
  2077. force_sigsegv(ksig->sig, current);
  2078. else
  2079. signal_delivered(ksig, stepping);
  2080. }
  2081. /*
  2082. * It could be that complete_signal() picked us to notify about the
  2083. * group-wide signal. Other threads should be notified now to take
  2084. * the shared signals in @which since we will not.
  2085. */
  2086. static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
  2087. {
  2088. sigset_t retarget;
  2089. struct task_struct *t;
  2090. sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
  2091. if (sigisemptyset(&retarget))
  2092. return;
  2093. t = tsk;
  2094. while_each_thread(tsk, t) {
  2095. if (t->flags & PF_EXITING)
  2096. continue;
  2097. if (!has_pending_signals(&retarget, &t->blocked))
  2098. continue;
  2099. /* Remove the signals this thread can handle. */
  2100. sigandsets(&retarget, &retarget, &t->blocked);
  2101. if (!signal_pending(t))
  2102. signal_wake_up(t, 0);
  2103. if (sigisemptyset(&retarget))
  2104. break;
  2105. }
  2106. }
  2107. void exit_signals(struct task_struct *tsk)
  2108. {
  2109. int group_stop = 0;
  2110. sigset_t unblocked;
  2111. /*
  2112. * @tsk is about to have PF_EXITING set - lock out users which
  2113. * expect stable threadgroup.
  2114. */
  2115. cgroup_threadgroup_change_begin(tsk);
  2116. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  2117. tsk->flags |= PF_EXITING;
  2118. cgroup_threadgroup_change_end(tsk);
  2119. return;
  2120. }
  2121. spin_lock_irq(&tsk->sighand->siglock);
  2122. /*
  2123. * From now this task is not visible for group-wide signals,
  2124. * see wants_signal(), do_signal_stop().
  2125. */
  2126. tsk->flags |= PF_EXITING;
  2127. cgroup_threadgroup_change_end(tsk);
  2128. if (!signal_pending(tsk))
  2129. goto out;
  2130. unblocked = tsk->blocked;
  2131. signotset(&unblocked);
  2132. retarget_shared_pending(tsk, &unblocked);
  2133. if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
  2134. task_participate_group_stop(tsk))
  2135. group_stop = CLD_STOPPED;
  2136. out:
  2137. spin_unlock_irq(&tsk->sighand->siglock);
  2138. /*
  2139. * If group stop has completed, deliver the notification. This
  2140. * should always go to the real parent of the group leader.
  2141. */
  2142. if (unlikely(group_stop)) {
  2143. read_lock(&tasklist_lock);
  2144. do_notify_parent_cldstop(tsk, false, group_stop);
  2145. read_unlock(&tasklist_lock);
  2146. }
  2147. }
  2148. EXPORT_SYMBOL(recalc_sigpending);
  2149. EXPORT_SYMBOL_GPL(dequeue_signal);
  2150. EXPORT_SYMBOL(flush_signals);
  2151. EXPORT_SYMBOL(force_sig);
  2152. EXPORT_SYMBOL(send_sig);
  2153. EXPORT_SYMBOL(send_sig_info);
  2154. EXPORT_SYMBOL(sigprocmask);
  2155. /*
  2156. * System call entry points.
  2157. */
  2158. /**
  2159. * sys_restart_syscall - restart a system call
  2160. */
  2161. SYSCALL_DEFINE0(restart_syscall)
  2162. {
  2163. struct restart_block *restart = &current->restart_block;
  2164. return restart->fn(restart);
  2165. }
  2166. long do_no_restart_syscall(struct restart_block *param)
  2167. {
  2168. return -EINTR;
  2169. }
  2170. static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
  2171. {
  2172. if (signal_pending(tsk) && !thread_group_empty(tsk)) {
  2173. sigset_t newblocked;
  2174. /* A set of now blocked but previously unblocked signals. */
  2175. sigandnsets(&newblocked, newset, &current->blocked);
  2176. retarget_shared_pending(tsk, &newblocked);
  2177. }
  2178. tsk->blocked = *newset;
  2179. recalc_sigpending();
  2180. }
  2181. /**
  2182. * set_current_blocked - change current->blocked mask
  2183. * @newset: new mask
  2184. *
  2185. * It is wrong to change ->blocked directly, this helper should be used
  2186. * to ensure the process can't miss a shared signal we are going to block.
  2187. */
  2188. void set_current_blocked(sigset_t *newset)
  2189. {
  2190. sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2191. __set_current_blocked(newset);
  2192. }
  2193. void __set_current_blocked(const sigset_t *newset)
  2194. {
  2195. struct task_struct *tsk = current;
  2196. /*
  2197. * In case the signal mask hasn't changed, there is nothing we need
  2198. * to do. The current->blocked shouldn't be modified by other task.
  2199. */
  2200. if (sigequalsets(&tsk->blocked, newset))
  2201. return;
  2202. spin_lock_irq(&tsk->sighand->siglock);
  2203. __set_task_blocked(tsk, newset);
  2204. spin_unlock_irq(&tsk->sighand->siglock);
  2205. }
  2206. /*
  2207. * This is also useful for kernel threads that want to temporarily
  2208. * (or permanently) block certain signals.
  2209. *
  2210. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  2211. * interface happily blocks "unblockable" signals like SIGKILL
  2212. * and friends.
  2213. */
  2214. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  2215. {
  2216. struct task_struct *tsk = current;
  2217. sigset_t newset;
  2218. /* Lockless, only current can change ->blocked, never from irq */
  2219. if (oldset)
  2220. *oldset = tsk->blocked;
  2221. switch (how) {
  2222. case SIG_BLOCK:
  2223. sigorsets(&newset, &tsk->blocked, set);
  2224. break;
  2225. case SIG_UNBLOCK:
  2226. sigandnsets(&newset, &tsk->blocked, set);
  2227. break;
  2228. case SIG_SETMASK:
  2229. newset = *set;
  2230. break;
  2231. default:
  2232. return -EINVAL;
  2233. }
  2234. __set_current_blocked(&newset);
  2235. return 0;
  2236. }
  2237. /**
  2238. * sys_rt_sigprocmask - change the list of currently blocked signals
  2239. * @how: whether to add, remove, or set signals
  2240. * @nset: stores pending signals
  2241. * @oset: previous value of signal mask if non-null
  2242. * @sigsetsize: size of sigset_t type
  2243. */
  2244. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
  2245. sigset_t __user *, oset, size_t, sigsetsize)
  2246. {
  2247. sigset_t old_set, new_set;
  2248. int error;
  2249. /* XXX: Don't preclude handling different sized sigset_t's. */
  2250. if (sigsetsize != sizeof(sigset_t))
  2251. return -EINVAL;
  2252. old_set = current->blocked;
  2253. if (nset) {
  2254. if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
  2255. return -EFAULT;
  2256. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2257. error = sigprocmask(how, &new_set, NULL);
  2258. if (error)
  2259. return error;
  2260. }
  2261. if (oset) {
  2262. if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
  2263. return -EFAULT;
  2264. }
  2265. return 0;
  2266. }
  2267. #ifdef CONFIG_COMPAT
  2268. COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
  2269. compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
  2270. {
  2271. sigset_t old_set = current->blocked;
  2272. /* XXX: Don't preclude handling different sized sigset_t's. */
  2273. if (sigsetsize != sizeof(sigset_t))
  2274. return -EINVAL;
  2275. if (nset) {
  2276. sigset_t new_set;
  2277. int error;
  2278. if (get_compat_sigset(&new_set, nset))
  2279. return -EFAULT;
  2280. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2281. error = sigprocmask(how, &new_set, NULL);
  2282. if (error)
  2283. return error;
  2284. }
  2285. return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
  2286. }
  2287. #endif
  2288. static int do_sigpending(sigset_t *set)
  2289. {
  2290. spin_lock_irq(&current->sighand->siglock);
  2291. sigorsets(set, &current->pending.signal,
  2292. &current->signal->shared_pending.signal);
  2293. spin_unlock_irq(&current->sighand->siglock);
  2294. /* Outside the lock because only this thread touches it. */
  2295. sigandsets(set, &current->blocked, set);
  2296. return 0;
  2297. }
  2298. /**
  2299. * sys_rt_sigpending - examine a pending signal that has been raised
  2300. * while blocked
  2301. * @uset: stores pending signals
  2302. * @sigsetsize: size of sigset_t type or larger
  2303. */
  2304. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
  2305. {
  2306. sigset_t set;
  2307. int err;
  2308. if (sigsetsize > sizeof(*uset))
  2309. return -EINVAL;
  2310. err = do_sigpending(&set);
  2311. if (!err && copy_to_user(uset, &set, sigsetsize))
  2312. err = -EFAULT;
  2313. return err;
  2314. }
  2315. #ifdef CONFIG_COMPAT
  2316. COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
  2317. compat_size_t, sigsetsize)
  2318. {
  2319. sigset_t set;
  2320. int err;
  2321. if (sigsetsize > sizeof(*uset))
  2322. return -EINVAL;
  2323. err = do_sigpending(&set);
  2324. if (!err)
  2325. err = put_compat_sigset(uset, &set, sigsetsize);
  2326. return err;
  2327. }
  2328. #endif
  2329. enum siginfo_layout siginfo_layout(int sig, int si_code)
  2330. {
  2331. enum siginfo_layout layout = SIL_KILL;
  2332. if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
  2333. static const struct {
  2334. unsigned char limit, layout;
  2335. } filter[] = {
  2336. [SIGILL] = { NSIGILL, SIL_FAULT },
  2337. [SIGFPE] = { NSIGFPE, SIL_FAULT },
  2338. [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
  2339. [SIGBUS] = { NSIGBUS, SIL_FAULT },
  2340. [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
  2341. #if defined(SIGEMT) && defined(NSIGEMT)
  2342. [SIGEMT] = { NSIGEMT, SIL_FAULT },
  2343. #endif
  2344. [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
  2345. [SIGPOLL] = { NSIGPOLL, SIL_POLL },
  2346. #ifdef __ARCH_SIGSYS
  2347. [SIGSYS] = { NSIGSYS, SIL_SYS },
  2348. #endif
  2349. };
  2350. if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit))
  2351. layout = filter[sig].layout;
  2352. else if (si_code <= NSIGPOLL)
  2353. layout = SIL_POLL;
  2354. } else {
  2355. if (si_code == SI_TIMER)
  2356. layout = SIL_TIMER;
  2357. else if (si_code == SI_SIGIO)
  2358. layout = SIL_POLL;
  2359. else if (si_code < 0)
  2360. layout = SIL_RT;
  2361. /* Tests to support buggy kernel ABIs */
  2362. #ifdef TRAP_FIXME
  2363. if ((sig == SIGTRAP) && (si_code == TRAP_FIXME))
  2364. layout = SIL_FAULT;
  2365. #endif
  2366. #ifdef FPE_FIXME
  2367. if ((sig == SIGFPE) && (si_code == FPE_FIXME))
  2368. layout = SIL_FAULT;
  2369. #endif
  2370. }
  2371. return layout;
  2372. }
  2373. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  2374. int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
  2375. {
  2376. int err;
  2377. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  2378. return -EFAULT;
  2379. if (from->si_code < 0)
  2380. return __copy_to_user(to, from, sizeof(siginfo_t))
  2381. ? -EFAULT : 0;
  2382. /*
  2383. * If you change siginfo_t structure, please be sure
  2384. * this code is fixed accordingly.
  2385. * Please remember to update the signalfd_copyinfo() function
  2386. * inside fs/signalfd.c too, in case siginfo_t changes.
  2387. * It should never copy any pad contained in the structure
  2388. * to avoid security leaks, but must copy the generic
  2389. * 3 ints plus the relevant union member.
  2390. */
  2391. err = __put_user(from->si_signo, &to->si_signo);
  2392. err |= __put_user(from->si_errno, &to->si_errno);
  2393. err |= __put_user(from->si_code, &to->si_code);
  2394. switch (siginfo_layout(from->si_signo, from->si_code)) {
  2395. case SIL_KILL:
  2396. err |= __put_user(from->si_pid, &to->si_pid);
  2397. err |= __put_user(from->si_uid, &to->si_uid);
  2398. break;
  2399. case SIL_TIMER:
  2400. /* Unreached SI_TIMER is negative */
  2401. break;
  2402. case SIL_POLL:
  2403. err |= __put_user(from->si_band, &to->si_band);
  2404. err |= __put_user(from->si_fd, &to->si_fd);
  2405. break;
  2406. case SIL_FAULT:
  2407. err |= __put_user(from->si_addr, &to->si_addr);
  2408. #ifdef __ARCH_SI_TRAPNO
  2409. err |= __put_user(from->si_trapno, &to->si_trapno);
  2410. #endif
  2411. #ifdef BUS_MCEERR_AO
  2412. /*
  2413. * Other callers might not initialize the si_lsb field,
  2414. * so check explicitly for the right codes here.
  2415. */
  2416. if (from->si_signo == SIGBUS &&
  2417. (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
  2418. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  2419. #endif
  2420. #ifdef SEGV_BNDERR
  2421. if (from->si_signo == SIGSEGV && from->si_code == SEGV_BNDERR) {
  2422. err |= __put_user(from->si_lower, &to->si_lower);
  2423. err |= __put_user(from->si_upper, &to->si_upper);
  2424. }
  2425. #endif
  2426. #ifdef SEGV_PKUERR
  2427. if (from->si_signo == SIGSEGV && from->si_code == SEGV_PKUERR)
  2428. err |= __put_user(from->si_pkey, &to->si_pkey);
  2429. #endif
  2430. break;
  2431. case SIL_CHLD:
  2432. err |= __put_user(from->si_pid, &to->si_pid);
  2433. err |= __put_user(from->si_uid, &to->si_uid);
  2434. err |= __put_user(from->si_status, &to->si_status);
  2435. err |= __put_user(from->si_utime, &to->si_utime);
  2436. err |= __put_user(from->si_stime, &to->si_stime);
  2437. break;
  2438. case SIL_RT:
  2439. err |= __put_user(from->si_pid, &to->si_pid);
  2440. err |= __put_user(from->si_uid, &to->si_uid);
  2441. err |= __put_user(from->si_ptr, &to->si_ptr);
  2442. break;
  2443. #ifdef __ARCH_SIGSYS
  2444. case SIL_SYS:
  2445. err |= __put_user(from->si_call_addr, &to->si_call_addr);
  2446. err |= __put_user(from->si_syscall, &to->si_syscall);
  2447. err |= __put_user(from->si_arch, &to->si_arch);
  2448. break;
  2449. #endif
  2450. }
  2451. return err;
  2452. }
  2453. #endif
  2454. /**
  2455. * do_sigtimedwait - wait for queued signals specified in @which
  2456. * @which: queued signals to wait for
  2457. * @info: if non-null, the signal's siginfo is returned here
  2458. * @ts: upper bound on process time suspension
  2459. */
  2460. static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
  2461. const struct timespec *ts)
  2462. {
  2463. ktime_t *to = NULL, timeout = KTIME_MAX;
  2464. struct task_struct *tsk = current;
  2465. sigset_t mask = *which;
  2466. int sig, ret = 0;
  2467. if (ts) {
  2468. if (!timespec_valid(ts))
  2469. return -EINVAL;
  2470. timeout = timespec_to_ktime(*ts);
  2471. to = &timeout;
  2472. }
  2473. /*
  2474. * Invert the set of allowed signals to get those we want to block.
  2475. */
  2476. sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2477. signotset(&mask);
  2478. spin_lock_irq(&tsk->sighand->siglock);
  2479. sig = dequeue_signal(tsk, &mask, info);
  2480. if (!sig && timeout) {
  2481. /*
  2482. * None ready, temporarily unblock those we're interested
  2483. * while we are sleeping in so that we'll be awakened when
  2484. * they arrive. Unblocking is always fine, we can avoid
  2485. * set_current_blocked().
  2486. */
  2487. tsk->real_blocked = tsk->blocked;
  2488. sigandsets(&tsk->blocked, &tsk->blocked, &mask);
  2489. recalc_sigpending();
  2490. spin_unlock_irq(&tsk->sighand->siglock);
  2491. __set_current_state(TASK_INTERRUPTIBLE);
  2492. ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
  2493. HRTIMER_MODE_REL);
  2494. spin_lock_irq(&tsk->sighand->siglock);
  2495. __set_task_blocked(tsk, &tsk->real_blocked);
  2496. sigemptyset(&tsk->real_blocked);
  2497. sig = dequeue_signal(tsk, &mask, info);
  2498. }
  2499. spin_unlock_irq(&tsk->sighand->siglock);
  2500. if (sig)
  2501. return sig;
  2502. return ret ? -EINTR : -EAGAIN;
  2503. }
  2504. /**
  2505. * sys_rt_sigtimedwait - synchronously wait for queued signals specified
  2506. * in @uthese
  2507. * @uthese: queued signals to wait for
  2508. * @uinfo: if non-null, the signal's siginfo is returned here
  2509. * @uts: upper bound on process time suspension
  2510. * @sigsetsize: size of sigset_t type
  2511. */
  2512. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  2513. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  2514. size_t, sigsetsize)
  2515. {
  2516. sigset_t these;
  2517. struct timespec ts;
  2518. siginfo_t info;
  2519. int ret;
  2520. /* XXX: Don't preclude handling different sized sigset_t's. */
  2521. if (sigsetsize != sizeof(sigset_t))
  2522. return -EINVAL;
  2523. if (copy_from_user(&these, uthese, sizeof(these)))
  2524. return -EFAULT;
  2525. if (uts) {
  2526. if (copy_from_user(&ts, uts, sizeof(ts)))
  2527. return -EFAULT;
  2528. }
  2529. ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
  2530. if (ret > 0 && uinfo) {
  2531. if (copy_siginfo_to_user(uinfo, &info))
  2532. ret = -EFAULT;
  2533. }
  2534. return ret;
  2535. }
  2536. #ifdef CONFIG_COMPAT
  2537. COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
  2538. struct compat_siginfo __user *, uinfo,
  2539. struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
  2540. {
  2541. sigset_t s;
  2542. struct timespec t;
  2543. siginfo_t info;
  2544. long ret;
  2545. if (sigsetsize != sizeof(sigset_t))
  2546. return -EINVAL;
  2547. if (get_compat_sigset(&s, uthese))
  2548. return -EFAULT;
  2549. if (uts) {
  2550. if (compat_get_timespec(&t, uts))
  2551. return -EFAULT;
  2552. }
  2553. ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
  2554. if (ret > 0 && uinfo) {
  2555. if (copy_siginfo_to_user32(uinfo, &info))
  2556. ret = -EFAULT;
  2557. }
  2558. return ret;
  2559. }
  2560. #endif
  2561. /**
  2562. * sys_kill - send a signal to a process
  2563. * @pid: the PID of the process
  2564. * @sig: signal to be sent
  2565. */
  2566. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2567. {
  2568. struct siginfo info;
  2569. info.si_signo = sig;
  2570. info.si_errno = 0;
  2571. info.si_code = SI_USER;
  2572. info.si_pid = task_tgid_vnr(current);
  2573. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2574. return kill_something_info(sig, &info, pid);
  2575. }
  2576. static int
  2577. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2578. {
  2579. struct task_struct *p;
  2580. int error = -ESRCH;
  2581. rcu_read_lock();
  2582. p = find_task_by_vpid(pid);
  2583. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2584. error = check_kill_permission(sig, info, p);
  2585. /*
  2586. * The null signal is a permissions and process existence
  2587. * probe. No signal is actually delivered.
  2588. */
  2589. if (!error && sig) {
  2590. error = do_send_sig_info(sig, info, p, false);
  2591. /*
  2592. * If lock_task_sighand() failed we pretend the task
  2593. * dies after receiving the signal. The window is tiny,
  2594. * and the signal is private anyway.
  2595. */
  2596. if (unlikely(error == -ESRCH))
  2597. error = 0;
  2598. }
  2599. }
  2600. rcu_read_unlock();
  2601. return error;
  2602. }
  2603. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2604. {
  2605. struct siginfo info = {};
  2606. info.si_signo = sig;
  2607. info.si_errno = 0;
  2608. info.si_code = SI_TKILL;
  2609. info.si_pid = task_tgid_vnr(current);
  2610. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2611. return do_send_specific(tgid, pid, sig, &info);
  2612. }
  2613. /**
  2614. * sys_tgkill - send signal to one specific thread
  2615. * @tgid: the thread group ID of the thread
  2616. * @pid: the PID of the thread
  2617. * @sig: signal to be sent
  2618. *
  2619. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2620. * exists but it's not belonging to the target process anymore. This
  2621. * method solves the problem of threads exiting and PIDs getting reused.
  2622. */
  2623. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2624. {
  2625. /* This is only valid for single tasks */
  2626. if (pid <= 0 || tgid <= 0)
  2627. return -EINVAL;
  2628. return do_tkill(tgid, pid, sig);
  2629. }
  2630. /**
  2631. * sys_tkill - send signal to one specific task
  2632. * @pid: the PID of the task
  2633. * @sig: signal to be sent
  2634. *
  2635. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2636. */
  2637. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2638. {
  2639. /* This is only valid for single tasks */
  2640. if (pid <= 0)
  2641. return -EINVAL;
  2642. return do_tkill(0, pid, sig);
  2643. }
  2644. static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
  2645. {
  2646. /* Not even root can pretend to send signals from the kernel.
  2647. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2648. */
  2649. if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
  2650. (task_pid_vnr(current) != pid))
  2651. return -EPERM;
  2652. info->si_signo = sig;
  2653. /* POSIX.1b doesn't mention process groups. */
  2654. return kill_proc_info(sig, info, pid);
  2655. }
  2656. /**
  2657. * sys_rt_sigqueueinfo - send signal information to a signal
  2658. * @pid: the PID of the thread
  2659. * @sig: signal to be sent
  2660. * @uinfo: signal info to be sent
  2661. */
  2662. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2663. siginfo_t __user *, uinfo)
  2664. {
  2665. siginfo_t info;
  2666. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2667. return -EFAULT;
  2668. return do_rt_sigqueueinfo(pid, sig, &info);
  2669. }
  2670. #ifdef CONFIG_COMPAT
  2671. COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
  2672. compat_pid_t, pid,
  2673. int, sig,
  2674. struct compat_siginfo __user *, uinfo)
  2675. {
  2676. siginfo_t info = {};
  2677. int ret = copy_siginfo_from_user32(&info, uinfo);
  2678. if (unlikely(ret))
  2679. return ret;
  2680. return do_rt_sigqueueinfo(pid, sig, &info);
  2681. }
  2682. #endif
  2683. static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2684. {
  2685. /* This is only valid for single tasks */
  2686. if (pid <= 0 || tgid <= 0)
  2687. return -EINVAL;
  2688. /* Not even root can pretend to send signals from the kernel.
  2689. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2690. */
  2691. if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
  2692. (task_pid_vnr(current) != pid))
  2693. return -EPERM;
  2694. info->si_signo = sig;
  2695. return do_send_specific(tgid, pid, sig, info);
  2696. }
  2697. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2698. siginfo_t __user *, uinfo)
  2699. {
  2700. siginfo_t info;
  2701. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2702. return -EFAULT;
  2703. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2704. }
  2705. #ifdef CONFIG_COMPAT
  2706. COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
  2707. compat_pid_t, tgid,
  2708. compat_pid_t, pid,
  2709. int, sig,
  2710. struct compat_siginfo __user *, uinfo)
  2711. {
  2712. siginfo_t info = {};
  2713. if (copy_siginfo_from_user32(&info, uinfo))
  2714. return -EFAULT;
  2715. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2716. }
  2717. #endif
  2718. /*
  2719. * For kthreads only, must not be used if cloned with CLONE_SIGHAND
  2720. */
  2721. void kernel_sigaction(int sig, __sighandler_t action)
  2722. {
  2723. spin_lock_irq(&current->sighand->siglock);
  2724. current->sighand->action[sig - 1].sa.sa_handler = action;
  2725. if (action == SIG_IGN) {
  2726. sigset_t mask;
  2727. sigemptyset(&mask);
  2728. sigaddset(&mask, sig);
  2729. flush_sigqueue_mask(&mask, &current->signal->shared_pending);
  2730. flush_sigqueue_mask(&mask, &current->pending);
  2731. recalc_sigpending();
  2732. }
  2733. spin_unlock_irq(&current->sighand->siglock);
  2734. }
  2735. EXPORT_SYMBOL(kernel_sigaction);
  2736. void __weak sigaction_compat_abi(struct k_sigaction *act,
  2737. struct k_sigaction *oact)
  2738. {
  2739. }
  2740. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2741. {
  2742. struct task_struct *p = current, *t;
  2743. struct k_sigaction *k;
  2744. sigset_t mask;
  2745. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2746. return -EINVAL;
  2747. k = &p->sighand->action[sig-1];
  2748. spin_lock_irq(&p->sighand->siglock);
  2749. if (oact)
  2750. *oact = *k;
  2751. sigaction_compat_abi(act, oact);
  2752. if (act) {
  2753. sigdelsetmask(&act->sa.sa_mask,
  2754. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2755. *k = *act;
  2756. /*
  2757. * POSIX 3.3.1.3:
  2758. * "Setting a signal action to SIG_IGN for a signal that is
  2759. * pending shall cause the pending signal to be discarded,
  2760. * whether or not it is blocked."
  2761. *
  2762. * "Setting a signal action to SIG_DFL for a signal that is
  2763. * pending and whose default action is to ignore the signal
  2764. * (for example, SIGCHLD), shall cause the pending signal to
  2765. * be discarded, whether or not it is blocked"
  2766. */
  2767. if (sig_handler_ignored(sig_handler(p, sig), sig)) {
  2768. sigemptyset(&mask);
  2769. sigaddset(&mask, sig);
  2770. flush_sigqueue_mask(&mask, &p->signal->shared_pending);
  2771. for_each_thread(p, t)
  2772. flush_sigqueue_mask(&mask, &t->pending);
  2773. }
  2774. }
  2775. spin_unlock_irq(&p->sighand->siglock);
  2776. return 0;
  2777. }
  2778. static int
  2779. do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp)
  2780. {
  2781. struct task_struct *t = current;
  2782. if (oss) {
  2783. memset(oss, 0, sizeof(stack_t));
  2784. oss->ss_sp = (void __user *) t->sas_ss_sp;
  2785. oss->ss_size = t->sas_ss_size;
  2786. oss->ss_flags = sas_ss_flags(sp) |
  2787. (current->sas_ss_flags & SS_FLAG_BITS);
  2788. }
  2789. if (ss) {
  2790. void __user *ss_sp = ss->ss_sp;
  2791. size_t ss_size = ss->ss_size;
  2792. unsigned ss_flags = ss->ss_flags;
  2793. int ss_mode;
  2794. if (unlikely(on_sig_stack(sp)))
  2795. return -EPERM;
  2796. ss_mode = ss_flags & ~SS_FLAG_BITS;
  2797. if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
  2798. ss_mode != 0))
  2799. return -EINVAL;
  2800. if (ss_mode == SS_DISABLE) {
  2801. ss_size = 0;
  2802. ss_sp = NULL;
  2803. } else {
  2804. if (unlikely(ss_size < MINSIGSTKSZ))
  2805. return -ENOMEM;
  2806. }
  2807. t->sas_ss_sp = (unsigned long) ss_sp;
  2808. t->sas_ss_size = ss_size;
  2809. t->sas_ss_flags = ss_flags;
  2810. }
  2811. return 0;
  2812. }
  2813. SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
  2814. {
  2815. stack_t new, old;
  2816. int err;
  2817. if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
  2818. return -EFAULT;
  2819. err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
  2820. current_user_stack_pointer());
  2821. if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
  2822. err = -EFAULT;
  2823. return err;
  2824. }
  2825. int restore_altstack(const stack_t __user *uss)
  2826. {
  2827. stack_t new;
  2828. if (copy_from_user(&new, uss, sizeof(stack_t)))
  2829. return -EFAULT;
  2830. (void)do_sigaltstack(&new, NULL, current_user_stack_pointer());
  2831. /* squash all but EFAULT for now */
  2832. return 0;
  2833. }
  2834. int __save_altstack(stack_t __user *uss, unsigned long sp)
  2835. {
  2836. struct task_struct *t = current;
  2837. int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
  2838. __put_user(t->sas_ss_flags, &uss->ss_flags) |
  2839. __put_user(t->sas_ss_size, &uss->ss_size);
  2840. if (err)
  2841. return err;
  2842. if (t->sas_ss_flags & SS_AUTODISARM)
  2843. sas_ss_reset(t);
  2844. return 0;
  2845. }
  2846. #ifdef CONFIG_COMPAT
  2847. COMPAT_SYSCALL_DEFINE2(sigaltstack,
  2848. const compat_stack_t __user *, uss_ptr,
  2849. compat_stack_t __user *, uoss_ptr)
  2850. {
  2851. stack_t uss, uoss;
  2852. int ret;
  2853. if (uss_ptr) {
  2854. compat_stack_t uss32;
  2855. if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
  2856. return -EFAULT;
  2857. uss.ss_sp = compat_ptr(uss32.ss_sp);
  2858. uss.ss_flags = uss32.ss_flags;
  2859. uss.ss_size = uss32.ss_size;
  2860. }
  2861. ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
  2862. compat_user_stack_pointer());
  2863. if (ret >= 0 && uoss_ptr) {
  2864. compat_stack_t old;
  2865. memset(&old, 0, sizeof(old));
  2866. old.ss_sp = ptr_to_compat(uoss.ss_sp);
  2867. old.ss_flags = uoss.ss_flags;
  2868. old.ss_size = uoss.ss_size;
  2869. if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
  2870. ret = -EFAULT;
  2871. }
  2872. return ret;
  2873. }
  2874. int compat_restore_altstack(const compat_stack_t __user *uss)
  2875. {
  2876. int err = compat_sys_sigaltstack(uss, NULL);
  2877. /* squash all but -EFAULT for now */
  2878. return err == -EFAULT ? err : 0;
  2879. }
  2880. int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
  2881. {
  2882. int err;
  2883. struct task_struct *t = current;
  2884. err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
  2885. &uss->ss_sp) |
  2886. __put_user(t->sas_ss_flags, &uss->ss_flags) |
  2887. __put_user(t->sas_ss_size, &uss->ss_size);
  2888. if (err)
  2889. return err;
  2890. if (t->sas_ss_flags & SS_AUTODISARM)
  2891. sas_ss_reset(t);
  2892. return 0;
  2893. }
  2894. #endif
  2895. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2896. /**
  2897. * sys_sigpending - examine pending signals
  2898. * @set: where mask of pending signal is returned
  2899. */
  2900. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
  2901. {
  2902. return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
  2903. }
  2904. #ifdef CONFIG_COMPAT
  2905. COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
  2906. {
  2907. sigset_t set;
  2908. int err = do_sigpending(&set);
  2909. if (!err)
  2910. err = put_user(set.sig[0], set32);
  2911. return err;
  2912. }
  2913. #endif
  2914. #endif
  2915. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2916. /**
  2917. * sys_sigprocmask - examine and change blocked signals
  2918. * @how: whether to add, remove, or set signals
  2919. * @nset: signals to add or remove (if non-null)
  2920. * @oset: previous value of signal mask if non-null
  2921. *
  2922. * Some platforms have their own version with special arguments;
  2923. * others support only sys_rt_sigprocmask.
  2924. */
  2925. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
  2926. old_sigset_t __user *, oset)
  2927. {
  2928. old_sigset_t old_set, new_set;
  2929. sigset_t new_blocked;
  2930. old_set = current->blocked.sig[0];
  2931. if (nset) {
  2932. if (copy_from_user(&new_set, nset, sizeof(*nset)))
  2933. return -EFAULT;
  2934. new_blocked = current->blocked;
  2935. switch (how) {
  2936. case SIG_BLOCK:
  2937. sigaddsetmask(&new_blocked, new_set);
  2938. break;
  2939. case SIG_UNBLOCK:
  2940. sigdelsetmask(&new_blocked, new_set);
  2941. break;
  2942. case SIG_SETMASK:
  2943. new_blocked.sig[0] = new_set;
  2944. break;
  2945. default:
  2946. return -EINVAL;
  2947. }
  2948. set_current_blocked(&new_blocked);
  2949. }
  2950. if (oset) {
  2951. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2952. return -EFAULT;
  2953. }
  2954. return 0;
  2955. }
  2956. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2957. #ifndef CONFIG_ODD_RT_SIGACTION
  2958. /**
  2959. * sys_rt_sigaction - alter an action taken by a process
  2960. * @sig: signal to be sent
  2961. * @act: new sigaction
  2962. * @oact: used to save the previous sigaction
  2963. * @sigsetsize: size of sigset_t type
  2964. */
  2965. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2966. const struct sigaction __user *, act,
  2967. struct sigaction __user *, oact,
  2968. size_t, sigsetsize)
  2969. {
  2970. struct k_sigaction new_sa, old_sa;
  2971. int ret = -EINVAL;
  2972. /* XXX: Don't preclude handling different sized sigset_t's. */
  2973. if (sigsetsize != sizeof(sigset_t))
  2974. goto out;
  2975. if (act) {
  2976. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2977. return -EFAULT;
  2978. }
  2979. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2980. if (!ret && oact) {
  2981. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2982. return -EFAULT;
  2983. }
  2984. out:
  2985. return ret;
  2986. }
  2987. #ifdef CONFIG_COMPAT
  2988. COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2989. const struct compat_sigaction __user *, act,
  2990. struct compat_sigaction __user *, oact,
  2991. compat_size_t, sigsetsize)
  2992. {
  2993. struct k_sigaction new_ka, old_ka;
  2994. #ifdef __ARCH_HAS_SA_RESTORER
  2995. compat_uptr_t restorer;
  2996. #endif
  2997. int ret;
  2998. /* XXX: Don't preclude handling different sized sigset_t's. */
  2999. if (sigsetsize != sizeof(compat_sigset_t))
  3000. return -EINVAL;
  3001. if (act) {
  3002. compat_uptr_t handler;
  3003. ret = get_user(handler, &act->sa_handler);
  3004. new_ka.sa.sa_handler = compat_ptr(handler);
  3005. #ifdef __ARCH_HAS_SA_RESTORER
  3006. ret |= get_user(restorer, &act->sa_restorer);
  3007. new_ka.sa.sa_restorer = compat_ptr(restorer);
  3008. #endif
  3009. ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
  3010. ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
  3011. if (ret)
  3012. return -EFAULT;
  3013. }
  3014. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3015. if (!ret && oact) {
  3016. ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
  3017. &oact->sa_handler);
  3018. ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
  3019. sizeof(oact->sa_mask));
  3020. ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  3021. #ifdef __ARCH_HAS_SA_RESTORER
  3022. ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  3023. &oact->sa_restorer);
  3024. #endif
  3025. }
  3026. return ret;
  3027. }
  3028. #endif
  3029. #endif /* !CONFIG_ODD_RT_SIGACTION */
  3030. #ifdef CONFIG_OLD_SIGACTION
  3031. SYSCALL_DEFINE3(sigaction, int, sig,
  3032. const struct old_sigaction __user *, act,
  3033. struct old_sigaction __user *, oact)
  3034. {
  3035. struct k_sigaction new_ka, old_ka;
  3036. int ret;
  3037. if (act) {
  3038. old_sigset_t mask;
  3039. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  3040. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  3041. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  3042. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  3043. __get_user(mask, &act->sa_mask))
  3044. return -EFAULT;
  3045. #ifdef __ARCH_HAS_KA_RESTORER
  3046. new_ka.ka_restorer = NULL;
  3047. #endif
  3048. siginitset(&new_ka.sa.sa_mask, mask);
  3049. }
  3050. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3051. if (!ret && oact) {
  3052. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  3053. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  3054. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  3055. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  3056. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  3057. return -EFAULT;
  3058. }
  3059. return ret;
  3060. }
  3061. #endif
  3062. #ifdef CONFIG_COMPAT_OLD_SIGACTION
  3063. COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
  3064. const struct compat_old_sigaction __user *, act,
  3065. struct compat_old_sigaction __user *, oact)
  3066. {
  3067. struct k_sigaction new_ka, old_ka;
  3068. int ret;
  3069. compat_old_sigset_t mask;
  3070. compat_uptr_t handler, restorer;
  3071. if (act) {
  3072. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  3073. __get_user(handler, &act->sa_handler) ||
  3074. __get_user(restorer, &act->sa_restorer) ||
  3075. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  3076. __get_user(mask, &act->sa_mask))
  3077. return -EFAULT;
  3078. #ifdef __ARCH_HAS_KA_RESTORER
  3079. new_ka.ka_restorer = NULL;
  3080. #endif
  3081. new_ka.sa.sa_handler = compat_ptr(handler);
  3082. new_ka.sa.sa_restorer = compat_ptr(restorer);
  3083. siginitset(&new_ka.sa.sa_mask, mask);
  3084. }
  3085. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3086. if (!ret && oact) {
  3087. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  3088. __put_user(ptr_to_compat(old_ka.sa.sa_handler),
  3089. &oact->sa_handler) ||
  3090. __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  3091. &oact->sa_restorer) ||
  3092. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  3093. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  3094. return -EFAULT;
  3095. }
  3096. return ret;
  3097. }
  3098. #endif
  3099. #ifdef CONFIG_SGETMASK_SYSCALL
  3100. /*
  3101. * For backwards compatibility. Functionality superseded by sigprocmask.
  3102. */
  3103. SYSCALL_DEFINE0(sgetmask)
  3104. {
  3105. /* SMP safe */
  3106. return current->blocked.sig[0];
  3107. }
  3108. SYSCALL_DEFINE1(ssetmask, int, newmask)
  3109. {
  3110. int old = current->blocked.sig[0];
  3111. sigset_t newset;
  3112. siginitset(&newset, newmask);
  3113. set_current_blocked(&newset);
  3114. return old;
  3115. }
  3116. #endif /* CONFIG_SGETMASK_SYSCALL */
  3117. #ifdef __ARCH_WANT_SYS_SIGNAL
  3118. /*
  3119. * For backwards compatibility. Functionality superseded by sigaction.
  3120. */
  3121. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  3122. {
  3123. struct k_sigaction new_sa, old_sa;
  3124. int ret;
  3125. new_sa.sa.sa_handler = handler;
  3126. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  3127. sigemptyset(&new_sa.sa.sa_mask);
  3128. ret = do_sigaction(sig, &new_sa, &old_sa);
  3129. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  3130. }
  3131. #endif /* __ARCH_WANT_SYS_SIGNAL */
  3132. #ifdef __ARCH_WANT_SYS_PAUSE
  3133. SYSCALL_DEFINE0(pause)
  3134. {
  3135. while (!signal_pending(current)) {
  3136. __set_current_state(TASK_INTERRUPTIBLE);
  3137. schedule();
  3138. }
  3139. return -ERESTARTNOHAND;
  3140. }
  3141. #endif
  3142. static int sigsuspend(sigset_t *set)
  3143. {
  3144. current->saved_sigmask = current->blocked;
  3145. set_current_blocked(set);
  3146. while (!signal_pending(current)) {
  3147. __set_current_state(TASK_INTERRUPTIBLE);
  3148. schedule();
  3149. }
  3150. set_restore_sigmask();
  3151. return -ERESTARTNOHAND;
  3152. }
  3153. /**
  3154. * sys_rt_sigsuspend - replace the signal mask for a value with the
  3155. * @unewset value until a signal is received
  3156. * @unewset: new signal mask value
  3157. * @sigsetsize: size of sigset_t type
  3158. */
  3159. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  3160. {
  3161. sigset_t newset;
  3162. /* XXX: Don't preclude handling different sized sigset_t's. */
  3163. if (sigsetsize != sizeof(sigset_t))
  3164. return -EINVAL;
  3165. if (copy_from_user(&newset, unewset, sizeof(newset)))
  3166. return -EFAULT;
  3167. return sigsuspend(&newset);
  3168. }
  3169. #ifdef CONFIG_COMPAT
  3170. COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
  3171. {
  3172. sigset_t newset;
  3173. /* XXX: Don't preclude handling different sized sigset_t's. */
  3174. if (sigsetsize != sizeof(sigset_t))
  3175. return -EINVAL;
  3176. if (get_compat_sigset(&newset, unewset))
  3177. return -EFAULT;
  3178. return sigsuspend(&newset);
  3179. }
  3180. #endif
  3181. #ifdef CONFIG_OLD_SIGSUSPEND
  3182. SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
  3183. {
  3184. sigset_t blocked;
  3185. siginitset(&blocked, mask);
  3186. return sigsuspend(&blocked);
  3187. }
  3188. #endif
  3189. #ifdef CONFIG_OLD_SIGSUSPEND3
  3190. SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
  3191. {
  3192. sigset_t blocked;
  3193. siginitset(&blocked, mask);
  3194. return sigsuspend(&blocked);
  3195. }
  3196. #endif
  3197. __weak const char *arch_vma_name(struct vm_area_struct *vma)
  3198. {
  3199. return NULL;
  3200. }
  3201. void __init signals_init(void)
  3202. {
  3203. /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
  3204. BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
  3205. != offsetof(struct siginfo, _sifields._pad));
  3206. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  3207. }
  3208. #ifdef CONFIG_KGDB_KDB
  3209. #include <linux/kdb.h>
  3210. /*
  3211. * kdb_send_sig_info - Allows kdb to send signals without exposing
  3212. * signal internals. This function checks if the required locks are
  3213. * available before calling the main signal code, to avoid kdb
  3214. * deadlocks.
  3215. */
  3216. void
  3217. kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
  3218. {
  3219. static struct task_struct *kdb_prev_t;
  3220. int sig, new_t;
  3221. if (!spin_trylock(&t->sighand->siglock)) {
  3222. kdb_printf("Can't do kill command now.\n"
  3223. "The sigmask lock is held somewhere else in "
  3224. "kernel, try again later\n");
  3225. return;
  3226. }
  3227. spin_unlock(&t->sighand->siglock);
  3228. new_t = kdb_prev_t != t;
  3229. kdb_prev_t = t;
  3230. if (t->state != TASK_RUNNING && new_t) {
  3231. kdb_printf("Process is not RUNNING, sending a signal from "
  3232. "kdb risks deadlock\n"
  3233. "on the run queue locks. "
  3234. "The signal has _not_ been sent.\n"
  3235. "Reissue the kill command if you want to risk "
  3236. "the deadlock.\n");
  3237. return;
  3238. }
  3239. sig = info->si_signo;
  3240. if (send_sig_info(sig, info, t))
  3241. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  3242. sig, t->pid);
  3243. else
  3244. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  3245. }
  3246. #endif /* CONFIG_KGDB_KDB */