signal.c 103 KB

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