signal.c 96 KB

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