signal.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002
  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. complete_signal(sig, t, type);
  991. ret:
  992. trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
  993. return ret;
  994. }
  995. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  996. enum pid_type type)
  997. {
  998. int from_ancestor_ns = 0;
  999. #ifdef CONFIG_PID_NS
  1000. from_ancestor_ns = si_fromuser(info) &&
  1001. !task_pid_nr_ns(current, task_active_pid_ns(t));
  1002. #endif
  1003. return __send_signal(sig, info, t, type, from_ancestor_ns);
  1004. }
  1005. static void print_fatal_signal(int signr)
  1006. {
  1007. struct pt_regs *regs = signal_pt_regs();
  1008. pr_info("potentially unexpected fatal signal %d.\n", signr);
  1009. #if defined(__i386__) && !defined(__arch_um__)
  1010. pr_info("code at %08lx: ", regs->ip);
  1011. {
  1012. int i;
  1013. for (i = 0; i < 16; i++) {
  1014. unsigned char insn;
  1015. if (get_user(insn, (unsigned char *)(regs->ip + i)))
  1016. break;
  1017. pr_cont("%02x ", insn);
  1018. }
  1019. }
  1020. pr_cont("\n");
  1021. #endif
  1022. preempt_disable();
  1023. show_regs(regs);
  1024. preempt_enable();
  1025. }
  1026. static int __init setup_print_fatal_signals(char *str)
  1027. {
  1028. get_option (&str, &print_fatal_signals);
  1029. return 1;
  1030. }
  1031. __setup("print-fatal-signals=", setup_print_fatal_signals);
  1032. int
  1033. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1034. {
  1035. return send_signal(sig, info, p, PIDTYPE_TGID);
  1036. }
  1037. static int
  1038. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1039. {
  1040. return send_signal(sig, info, t, PIDTYPE_PID);
  1041. }
  1042. int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  1043. enum pid_type type)
  1044. {
  1045. unsigned long flags;
  1046. int ret = -ESRCH;
  1047. if (lock_task_sighand(p, &flags)) {
  1048. ret = send_signal(sig, info, p, type);
  1049. unlock_task_sighand(p, &flags);
  1050. }
  1051. return ret;
  1052. }
  1053. /*
  1054. * Force a signal that the process can't ignore: if necessary
  1055. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  1056. *
  1057. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  1058. * since we do not want to have a signal handler that was blocked
  1059. * be invoked when user space had explicitly blocked it.
  1060. *
  1061. * We don't want to have recursive SIGSEGV's etc, for example,
  1062. * that is why we also clear SIGNAL_UNKILLABLE.
  1063. */
  1064. int
  1065. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1066. {
  1067. unsigned long int flags;
  1068. int ret, blocked, ignored;
  1069. struct k_sigaction *action;
  1070. spin_lock_irqsave(&t->sighand->siglock, flags);
  1071. action = &t->sighand->action[sig-1];
  1072. ignored = action->sa.sa_handler == SIG_IGN;
  1073. blocked = sigismember(&t->blocked, sig);
  1074. if (blocked || ignored) {
  1075. action->sa.sa_handler = SIG_DFL;
  1076. if (blocked) {
  1077. sigdelset(&t->blocked, sig);
  1078. recalc_sigpending_and_wake(t);
  1079. }
  1080. }
  1081. /*
  1082. * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
  1083. * debugging to leave init killable.
  1084. */
  1085. if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
  1086. t->signal->flags &= ~SIGNAL_UNKILLABLE;
  1087. ret = specific_send_sig_info(sig, info, t);
  1088. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  1089. return ret;
  1090. }
  1091. /*
  1092. * Nuke all other threads in the group.
  1093. */
  1094. int zap_other_threads(struct task_struct *p)
  1095. {
  1096. struct task_struct *t = p;
  1097. int count = 0;
  1098. p->signal->group_stop_count = 0;
  1099. while_each_thread(p, t) {
  1100. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1101. count++;
  1102. /* Don't bother with already dead threads */
  1103. if (t->exit_state)
  1104. continue;
  1105. sigaddset(&t->pending.signal, SIGKILL);
  1106. signal_wake_up(t, 1);
  1107. }
  1108. return count;
  1109. }
  1110. struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  1111. unsigned long *flags)
  1112. {
  1113. struct sighand_struct *sighand;
  1114. rcu_read_lock();
  1115. for (;;) {
  1116. sighand = rcu_dereference(tsk->sighand);
  1117. if (unlikely(sighand == NULL))
  1118. break;
  1119. /*
  1120. * This sighand can be already freed and even reused, but
  1121. * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
  1122. * initializes ->siglock: this slab can't go away, it has
  1123. * the same object type, ->siglock can't be reinitialized.
  1124. *
  1125. * We need to ensure that tsk->sighand is still the same
  1126. * after we take the lock, we can race with de_thread() or
  1127. * __exit_signal(). In the latter case the next iteration
  1128. * must see ->sighand == NULL.
  1129. */
  1130. spin_lock_irqsave(&sighand->siglock, *flags);
  1131. if (likely(sighand == tsk->sighand))
  1132. break;
  1133. spin_unlock_irqrestore(&sighand->siglock, *flags);
  1134. }
  1135. rcu_read_unlock();
  1136. return sighand;
  1137. }
  1138. /*
  1139. * send signal info to all the members of a group
  1140. */
  1141. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  1142. enum pid_type type)
  1143. {
  1144. int ret;
  1145. rcu_read_lock();
  1146. ret = check_kill_permission(sig, info, p);
  1147. rcu_read_unlock();
  1148. if (!ret && sig)
  1149. ret = do_send_sig_info(sig, info, p, type);
  1150. return ret;
  1151. }
  1152. /*
  1153. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1154. * control characters do (^C, ^Z etc)
  1155. * - the caller must hold at least a readlock on tasklist_lock
  1156. */
  1157. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1158. {
  1159. struct task_struct *p = NULL;
  1160. int retval, success;
  1161. success = 0;
  1162. retval = -ESRCH;
  1163. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1164. int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
  1165. success |= !err;
  1166. retval = err;
  1167. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1168. return success ? 0 : retval;
  1169. }
  1170. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1171. {
  1172. int error = -ESRCH;
  1173. struct task_struct *p;
  1174. for (;;) {
  1175. rcu_read_lock();
  1176. p = pid_task(pid, PIDTYPE_PID);
  1177. if (p)
  1178. error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
  1179. rcu_read_unlock();
  1180. if (likely(!p || error != -ESRCH))
  1181. return error;
  1182. /*
  1183. * The task was unhashed in between, try again. If it
  1184. * is dead, pid_task() will return NULL, if we race with
  1185. * de_thread() it will find the new leader.
  1186. */
  1187. }
  1188. }
  1189. static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1190. {
  1191. int error;
  1192. rcu_read_lock();
  1193. error = kill_pid_info(sig, info, find_vpid(pid));
  1194. rcu_read_unlock();
  1195. return error;
  1196. }
  1197. static int kill_as_cred_perm(const struct cred *cred,
  1198. struct task_struct *target)
  1199. {
  1200. const struct cred *pcred = __task_cred(target);
  1201. if (!uid_eq(cred->euid, pcred->suid) && !uid_eq(cred->euid, pcred->uid) &&
  1202. !uid_eq(cred->uid, pcred->suid) && !uid_eq(cred->uid, pcred->uid))
  1203. return 0;
  1204. return 1;
  1205. }
  1206. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1207. int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
  1208. const struct cred *cred)
  1209. {
  1210. int ret = -EINVAL;
  1211. struct task_struct *p;
  1212. unsigned long flags;
  1213. if (!valid_signal(sig))
  1214. return ret;
  1215. rcu_read_lock();
  1216. p = pid_task(pid, PIDTYPE_PID);
  1217. if (!p) {
  1218. ret = -ESRCH;
  1219. goto out_unlock;
  1220. }
  1221. if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
  1222. ret = -EPERM;
  1223. goto out_unlock;
  1224. }
  1225. ret = security_task_kill(p, info, sig, cred);
  1226. if (ret)
  1227. goto out_unlock;
  1228. if (sig) {
  1229. if (lock_task_sighand(p, &flags)) {
  1230. ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
  1231. unlock_task_sighand(p, &flags);
  1232. } else
  1233. ret = -ESRCH;
  1234. }
  1235. out_unlock:
  1236. rcu_read_unlock();
  1237. return ret;
  1238. }
  1239. EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
  1240. /*
  1241. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1242. *
  1243. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1244. * is probably wrong. Should make it like BSD or SYSV.
  1245. */
  1246. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1247. {
  1248. int ret;
  1249. if (pid > 0) {
  1250. rcu_read_lock();
  1251. ret = kill_pid_info(sig, info, find_vpid(pid));
  1252. rcu_read_unlock();
  1253. return ret;
  1254. }
  1255. /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
  1256. if (pid == INT_MIN)
  1257. return -ESRCH;
  1258. read_lock(&tasklist_lock);
  1259. if (pid != -1) {
  1260. ret = __kill_pgrp_info(sig, info,
  1261. pid ? find_vpid(-pid) : task_pgrp(current));
  1262. } else {
  1263. int retval = 0, count = 0;
  1264. struct task_struct * p;
  1265. for_each_process(p) {
  1266. if (task_pid_vnr(p) > 1 &&
  1267. !same_thread_group(p, current)) {
  1268. int err = group_send_sig_info(sig, info, p,
  1269. PIDTYPE_MAX);
  1270. ++count;
  1271. if (err != -EPERM)
  1272. retval = err;
  1273. }
  1274. }
  1275. ret = count ? retval : -ESRCH;
  1276. }
  1277. read_unlock(&tasklist_lock);
  1278. return ret;
  1279. }
  1280. /*
  1281. * These are for backward compatibility with the rest of the kernel source.
  1282. */
  1283. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1284. {
  1285. /*
  1286. * Make sure legacy kernel users don't send in bad values
  1287. * (normal paths check this in check_kill_permission).
  1288. */
  1289. if (!valid_signal(sig))
  1290. return -EINVAL;
  1291. return do_send_sig_info(sig, info, p, PIDTYPE_PID);
  1292. }
  1293. #define __si_special(priv) \
  1294. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1295. int
  1296. send_sig(int sig, struct task_struct *p, int priv)
  1297. {
  1298. return send_sig_info(sig, __si_special(priv), p);
  1299. }
  1300. void
  1301. force_sig(int sig, struct task_struct *p)
  1302. {
  1303. force_sig_info(sig, SEND_SIG_PRIV, p);
  1304. }
  1305. /*
  1306. * When things go south during signal handling, we
  1307. * will force a SIGSEGV. And if the signal that caused
  1308. * the problem was already a SIGSEGV, we'll want to
  1309. * make sure we don't even try to deliver the signal..
  1310. */
  1311. int
  1312. force_sigsegv(int sig, struct task_struct *p)
  1313. {
  1314. if (sig == SIGSEGV) {
  1315. unsigned long flags;
  1316. spin_lock_irqsave(&p->sighand->siglock, flags);
  1317. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1318. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1319. }
  1320. force_sig(SIGSEGV, p);
  1321. return 0;
  1322. }
  1323. int force_sig_fault(int sig, int code, void __user *addr
  1324. ___ARCH_SI_TRAPNO(int trapno)
  1325. ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
  1326. , struct task_struct *t)
  1327. {
  1328. struct siginfo info;
  1329. clear_siginfo(&info);
  1330. info.si_signo = sig;
  1331. info.si_errno = 0;
  1332. info.si_code = code;
  1333. info.si_addr = addr;
  1334. #ifdef __ARCH_SI_TRAPNO
  1335. info.si_trapno = trapno;
  1336. #endif
  1337. #ifdef __ia64__
  1338. info.si_imm = imm;
  1339. info.si_flags = flags;
  1340. info.si_isr = isr;
  1341. #endif
  1342. return force_sig_info(info.si_signo, &info, t);
  1343. }
  1344. int send_sig_fault(int sig, int code, void __user *addr
  1345. ___ARCH_SI_TRAPNO(int trapno)
  1346. ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
  1347. , struct task_struct *t)
  1348. {
  1349. struct siginfo info;
  1350. clear_siginfo(&info);
  1351. info.si_signo = sig;
  1352. info.si_errno = 0;
  1353. info.si_code = code;
  1354. info.si_addr = addr;
  1355. #ifdef __ARCH_SI_TRAPNO
  1356. info.si_trapno = trapno;
  1357. #endif
  1358. #ifdef __ia64__
  1359. info.si_imm = imm;
  1360. info.si_flags = flags;
  1361. info.si_isr = isr;
  1362. #endif
  1363. return send_sig_info(info.si_signo, &info, t);
  1364. }
  1365. int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
  1366. {
  1367. struct siginfo info;
  1368. WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
  1369. clear_siginfo(&info);
  1370. info.si_signo = SIGBUS;
  1371. info.si_errno = 0;
  1372. info.si_code = code;
  1373. info.si_addr = addr;
  1374. info.si_addr_lsb = lsb;
  1375. return force_sig_info(info.si_signo, &info, t);
  1376. }
  1377. int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
  1378. {
  1379. struct siginfo info;
  1380. WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
  1381. clear_siginfo(&info);
  1382. info.si_signo = SIGBUS;
  1383. info.si_errno = 0;
  1384. info.si_code = code;
  1385. info.si_addr = addr;
  1386. info.si_addr_lsb = lsb;
  1387. return send_sig_info(info.si_signo, &info, t);
  1388. }
  1389. EXPORT_SYMBOL(send_sig_mceerr);
  1390. int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
  1391. {
  1392. struct siginfo info;
  1393. clear_siginfo(&info);
  1394. info.si_signo = SIGSEGV;
  1395. info.si_errno = 0;
  1396. info.si_code = SEGV_BNDERR;
  1397. info.si_addr = addr;
  1398. info.si_lower = lower;
  1399. info.si_upper = upper;
  1400. return force_sig_info(info.si_signo, &info, current);
  1401. }
  1402. #ifdef SEGV_PKUERR
  1403. int force_sig_pkuerr(void __user *addr, u32 pkey)
  1404. {
  1405. struct siginfo info;
  1406. clear_siginfo(&info);
  1407. info.si_signo = SIGSEGV;
  1408. info.si_errno = 0;
  1409. info.si_code = SEGV_PKUERR;
  1410. info.si_addr = addr;
  1411. info.si_pkey = pkey;
  1412. return force_sig_info(info.si_signo, &info, current);
  1413. }
  1414. #endif
  1415. /* For the crazy architectures that include trap information in
  1416. * the errno field, instead of an actual errno value.
  1417. */
  1418. int force_sig_ptrace_errno_trap(int errno, void __user *addr)
  1419. {
  1420. struct siginfo info;
  1421. clear_siginfo(&info);
  1422. info.si_signo = SIGTRAP;
  1423. info.si_errno = errno;
  1424. info.si_code = TRAP_HWBKPT;
  1425. info.si_addr = addr;
  1426. return force_sig_info(info.si_signo, &info, current);
  1427. }
  1428. int kill_pgrp(struct pid *pid, int sig, int priv)
  1429. {
  1430. int ret;
  1431. read_lock(&tasklist_lock);
  1432. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1433. read_unlock(&tasklist_lock);
  1434. return ret;
  1435. }
  1436. EXPORT_SYMBOL(kill_pgrp);
  1437. int kill_pid(struct pid *pid, int sig, int priv)
  1438. {
  1439. return kill_pid_info(sig, __si_special(priv), pid);
  1440. }
  1441. EXPORT_SYMBOL(kill_pid);
  1442. /*
  1443. * These functions support sending signals using preallocated sigqueue
  1444. * structures. This is needed "because realtime applications cannot
  1445. * afford to lose notifications of asynchronous events, like timer
  1446. * expirations or I/O completions". In the case of POSIX Timers
  1447. * we allocate the sigqueue structure from the timer_create. If this
  1448. * allocation fails we are able to report the failure to the application
  1449. * with an EAGAIN error.
  1450. */
  1451. struct sigqueue *sigqueue_alloc(void)
  1452. {
  1453. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1454. if (q)
  1455. q->flags |= SIGQUEUE_PREALLOC;
  1456. return q;
  1457. }
  1458. void sigqueue_free(struct sigqueue *q)
  1459. {
  1460. unsigned long flags;
  1461. spinlock_t *lock = &current->sighand->siglock;
  1462. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1463. /*
  1464. * We must hold ->siglock while testing q->list
  1465. * to serialize with collect_signal() or with
  1466. * __exit_signal()->flush_sigqueue().
  1467. */
  1468. spin_lock_irqsave(lock, flags);
  1469. q->flags &= ~SIGQUEUE_PREALLOC;
  1470. /*
  1471. * If it is queued it will be freed when dequeued,
  1472. * like the "regular" sigqueue.
  1473. */
  1474. if (!list_empty(&q->list))
  1475. q = NULL;
  1476. spin_unlock_irqrestore(lock, flags);
  1477. if (q)
  1478. __sigqueue_free(q);
  1479. }
  1480. int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
  1481. {
  1482. int sig = q->info.si_signo;
  1483. struct sigpending *pending;
  1484. struct task_struct *t;
  1485. unsigned long flags;
  1486. int ret, result;
  1487. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1488. ret = -1;
  1489. rcu_read_lock();
  1490. t = pid_task(pid, type);
  1491. if (!t || !likely(lock_task_sighand(t, &flags)))
  1492. goto ret;
  1493. ret = 1; /* the signal is ignored */
  1494. result = TRACE_SIGNAL_IGNORED;
  1495. if (!prepare_signal(sig, t, false))
  1496. goto out;
  1497. ret = 0;
  1498. if (unlikely(!list_empty(&q->list))) {
  1499. /*
  1500. * If an SI_TIMER entry is already queue just increment
  1501. * the overrun count.
  1502. */
  1503. BUG_ON(q->info.si_code != SI_TIMER);
  1504. q->info.si_overrun++;
  1505. result = TRACE_SIGNAL_ALREADY_PENDING;
  1506. goto out;
  1507. }
  1508. q->info.si_overrun = 0;
  1509. signalfd_notify(t, sig);
  1510. pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
  1511. list_add_tail(&q->list, &pending->list);
  1512. sigaddset(&pending->signal, sig);
  1513. complete_signal(sig, t, type);
  1514. result = TRACE_SIGNAL_DELIVERED;
  1515. out:
  1516. trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
  1517. unlock_task_sighand(t, &flags);
  1518. ret:
  1519. rcu_read_unlock();
  1520. return ret;
  1521. }
  1522. /*
  1523. * Let a parent know about the death of a child.
  1524. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1525. *
  1526. * Returns true if our parent ignored us and so we've switched to
  1527. * self-reaping.
  1528. */
  1529. bool do_notify_parent(struct task_struct *tsk, int sig)
  1530. {
  1531. struct siginfo info;
  1532. unsigned long flags;
  1533. struct sighand_struct *psig;
  1534. bool autoreap = false;
  1535. u64 utime, stime;
  1536. BUG_ON(sig == -1);
  1537. /* do_notify_parent_cldstop should have been called instead. */
  1538. BUG_ON(task_is_stopped_or_traced(tsk));
  1539. BUG_ON(!tsk->ptrace &&
  1540. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1541. if (sig != SIGCHLD) {
  1542. /*
  1543. * This is only possible if parent == real_parent.
  1544. * Check if it has changed security domain.
  1545. */
  1546. if (tsk->parent_exec_id != tsk->parent->self_exec_id)
  1547. sig = SIGCHLD;
  1548. }
  1549. clear_siginfo(&info);
  1550. info.si_signo = sig;
  1551. info.si_errno = 0;
  1552. /*
  1553. * We are under tasklist_lock here so our parent is tied to
  1554. * us and cannot change.
  1555. *
  1556. * task_active_pid_ns will always return the same pid namespace
  1557. * until a task passes through release_task.
  1558. *
  1559. * write_lock() currently calls preempt_disable() which is the
  1560. * same as rcu_read_lock(), but according to Oleg, this is not
  1561. * correct to rely on this
  1562. */
  1563. rcu_read_lock();
  1564. info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
  1565. info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
  1566. task_uid(tsk));
  1567. rcu_read_unlock();
  1568. task_cputime(tsk, &utime, &stime);
  1569. info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
  1570. info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
  1571. info.si_status = tsk->exit_code & 0x7f;
  1572. if (tsk->exit_code & 0x80)
  1573. info.si_code = CLD_DUMPED;
  1574. else if (tsk->exit_code & 0x7f)
  1575. info.si_code = CLD_KILLED;
  1576. else {
  1577. info.si_code = CLD_EXITED;
  1578. info.si_status = tsk->exit_code >> 8;
  1579. }
  1580. psig = tsk->parent->sighand;
  1581. spin_lock_irqsave(&psig->siglock, flags);
  1582. if (!tsk->ptrace && sig == SIGCHLD &&
  1583. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1584. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1585. /*
  1586. * We are exiting and our parent doesn't care. POSIX.1
  1587. * defines special semantics for setting SIGCHLD to SIG_IGN
  1588. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1589. * automatically and not left for our parent's wait4 call.
  1590. * Rather than having the parent do it as a magic kind of
  1591. * signal handler, we just set this to tell do_exit that we
  1592. * can be cleaned up without becoming a zombie. Note that
  1593. * we still call __wake_up_parent in this case, because a
  1594. * blocked sys_wait4 might now return -ECHILD.
  1595. *
  1596. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1597. * is implementation-defined: we do (if you don't want
  1598. * it, just use SIG_IGN instead).
  1599. */
  1600. autoreap = true;
  1601. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1602. sig = 0;
  1603. }
  1604. if (valid_signal(sig) && sig)
  1605. __group_send_sig_info(sig, &info, tsk->parent);
  1606. __wake_up_parent(tsk, tsk->parent);
  1607. spin_unlock_irqrestore(&psig->siglock, flags);
  1608. return autoreap;
  1609. }
  1610. /**
  1611. * do_notify_parent_cldstop - notify parent of stopped/continued state change
  1612. * @tsk: task reporting the state change
  1613. * @for_ptracer: the notification is for ptracer
  1614. * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
  1615. *
  1616. * Notify @tsk's parent that the stopped/continued state has changed. If
  1617. * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
  1618. * If %true, @tsk reports to @tsk->parent which should be the ptracer.
  1619. *
  1620. * CONTEXT:
  1621. * Must be called with tasklist_lock at least read locked.
  1622. */
  1623. static void do_notify_parent_cldstop(struct task_struct *tsk,
  1624. bool for_ptracer, int why)
  1625. {
  1626. struct siginfo info;
  1627. unsigned long flags;
  1628. struct task_struct *parent;
  1629. struct sighand_struct *sighand;
  1630. u64 utime, stime;
  1631. if (for_ptracer) {
  1632. parent = tsk->parent;
  1633. } else {
  1634. tsk = tsk->group_leader;
  1635. parent = tsk->real_parent;
  1636. }
  1637. clear_siginfo(&info);
  1638. info.si_signo = SIGCHLD;
  1639. info.si_errno = 0;
  1640. /*
  1641. * see comment in do_notify_parent() about the following 4 lines
  1642. */
  1643. rcu_read_lock();
  1644. info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
  1645. info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
  1646. rcu_read_unlock();
  1647. task_cputime(tsk, &utime, &stime);
  1648. info.si_utime = nsec_to_clock_t(utime);
  1649. info.si_stime = nsec_to_clock_t(stime);
  1650. info.si_code = why;
  1651. switch (why) {
  1652. case CLD_CONTINUED:
  1653. info.si_status = SIGCONT;
  1654. break;
  1655. case CLD_STOPPED:
  1656. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1657. break;
  1658. case CLD_TRAPPED:
  1659. info.si_status = tsk->exit_code & 0x7f;
  1660. break;
  1661. default:
  1662. BUG();
  1663. }
  1664. sighand = parent->sighand;
  1665. spin_lock_irqsave(&sighand->siglock, flags);
  1666. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1667. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1668. __group_send_sig_info(SIGCHLD, &info, parent);
  1669. /*
  1670. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1671. */
  1672. __wake_up_parent(tsk, parent);
  1673. spin_unlock_irqrestore(&sighand->siglock, flags);
  1674. }
  1675. static inline int may_ptrace_stop(void)
  1676. {
  1677. if (!likely(current->ptrace))
  1678. return 0;
  1679. /*
  1680. * Are we in the middle of do_coredump?
  1681. * If so and our tracer is also part of the coredump stopping
  1682. * is a deadlock situation, and pointless because our tracer
  1683. * is dead so don't allow us to stop.
  1684. * If SIGKILL was already sent before the caller unlocked
  1685. * ->siglock we must see ->core_state != NULL. Otherwise it
  1686. * is safe to enter schedule().
  1687. *
  1688. * This is almost outdated, a task with the pending SIGKILL can't
  1689. * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
  1690. * after SIGKILL was already dequeued.
  1691. */
  1692. if (unlikely(current->mm->core_state) &&
  1693. unlikely(current->mm == current->parent->mm))
  1694. return 0;
  1695. return 1;
  1696. }
  1697. /*
  1698. * Return non-zero if there is a SIGKILL that should be waking us up.
  1699. * Called with the siglock held.
  1700. */
  1701. static int sigkill_pending(struct task_struct *tsk)
  1702. {
  1703. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1704. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1705. }
  1706. /*
  1707. * This must be called with current->sighand->siglock held.
  1708. *
  1709. * This should be the path for all ptrace stops.
  1710. * We always set current->last_siginfo while stopped here.
  1711. * That makes it a way to test a stopped process for
  1712. * being ptrace-stopped vs being job-control-stopped.
  1713. *
  1714. * If we actually decide not to stop at all because the tracer
  1715. * is gone, we keep current->exit_code unless clear_code.
  1716. */
  1717. static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  1718. __releases(&current->sighand->siglock)
  1719. __acquires(&current->sighand->siglock)
  1720. {
  1721. bool gstop_done = false;
  1722. if (arch_ptrace_stop_needed(exit_code, info)) {
  1723. /*
  1724. * The arch code has something special to do before a
  1725. * ptrace stop. This is allowed to block, e.g. for faults
  1726. * on user stack pages. We can't keep the siglock while
  1727. * calling arch_ptrace_stop, so we must release it now.
  1728. * To preserve proper semantics, we must do this before
  1729. * any signal bookkeeping like checking group_stop_count.
  1730. * Meanwhile, a SIGKILL could come in before we retake the
  1731. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1732. * So after regaining the lock, we must check for SIGKILL.
  1733. */
  1734. spin_unlock_irq(&current->sighand->siglock);
  1735. arch_ptrace_stop(exit_code, info);
  1736. spin_lock_irq(&current->sighand->siglock);
  1737. if (sigkill_pending(current))
  1738. return;
  1739. }
  1740. set_special_state(TASK_TRACED);
  1741. /*
  1742. * We're committing to trapping. TRACED should be visible before
  1743. * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
  1744. * Also, transition to TRACED and updates to ->jobctl should be
  1745. * atomic with respect to siglock and should be done after the arch
  1746. * hook as siglock is released and regrabbed across it.
  1747. *
  1748. * TRACER TRACEE
  1749. *
  1750. * ptrace_attach()
  1751. * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
  1752. * do_wait()
  1753. * set_current_state() smp_wmb();
  1754. * ptrace_do_wait()
  1755. * wait_task_stopped()
  1756. * task_stopped_code()
  1757. * [L] task_is_traced() [S] task_clear_jobctl_trapping();
  1758. */
  1759. smp_wmb();
  1760. current->last_siginfo = info;
  1761. current->exit_code = exit_code;
  1762. /*
  1763. * If @why is CLD_STOPPED, we're trapping to participate in a group
  1764. * stop. Do the bookkeeping. Note that if SIGCONT was delievered
  1765. * across siglock relocks since INTERRUPT was scheduled, PENDING
  1766. * could be clear now. We act as if SIGCONT is received after
  1767. * TASK_TRACED is entered - ignore it.
  1768. */
  1769. if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
  1770. gstop_done = task_participate_group_stop(current);
  1771. /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
  1772. task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1773. if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
  1774. task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
  1775. /* entering a trap, clear TRAPPING */
  1776. task_clear_jobctl_trapping(current);
  1777. spin_unlock_irq(&current->sighand->siglock);
  1778. read_lock(&tasklist_lock);
  1779. if (may_ptrace_stop()) {
  1780. /*
  1781. * Notify parents of the stop.
  1782. *
  1783. * While ptraced, there are two parents - the ptracer and
  1784. * the real_parent of the group_leader. The ptracer should
  1785. * know about every stop while the real parent is only
  1786. * interested in the completion of group stop. The states
  1787. * for the two don't interact with each other. Notify
  1788. * separately unless they're gonna be duplicates.
  1789. */
  1790. do_notify_parent_cldstop(current, true, why);
  1791. if (gstop_done && ptrace_reparented(current))
  1792. do_notify_parent_cldstop(current, false, why);
  1793. /*
  1794. * Don't want to allow preemption here, because
  1795. * sys_ptrace() needs this task to be inactive.
  1796. *
  1797. * XXX: implement read_unlock_no_resched().
  1798. */
  1799. preempt_disable();
  1800. read_unlock(&tasklist_lock);
  1801. preempt_enable_no_resched();
  1802. freezable_schedule();
  1803. } else {
  1804. /*
  1805. * By the time we got the lock, our tracer went away.
  1806. * Don't drop the lock yet, another tracer may come.
  1807. *
  1808. * If @gstop_done, the ptracer went away between group stop
  1809. * completion and here. During detach, it would have set
  1810. * JOBCTL_STOP_PENDING on us and we'll re-enter
  1811. * TASK_STOPPED in do_signal_stop() on return, so notifying
  1812. * the real parent of the group stop completion is enough.
  1813. */
  1814. if (gstop_done)
  1815. do_notify_parent_cldstop(current, false, why);
  1816. /* tasklist protects us from ptrace_freeze_traced() */
  1817. __set_current_state(TASK_RUNNING);
  1818. if (clear_code)
  1819. current->exit_code = 0;
  1820. read_unlock(&tasklist_lock);
  1821. }
  1822. /*
  1823. * We are back. Now reacquire the siglock before touching
  1824. * last_siginfo, so that we are sure to have synchronized with
  1825. * any signal-sending on another CPU that wants to examine it.
  1826. */
  1827. spin_lock_irq(&current->sighand->siglock);
  1828. current->last_siginfo = NULL;
  1829. /* LISTENING can be set only during STOP traps, clear it */
  1830. current->jobctl &= ~JOBCTL_LISTENING;
  1831. /*
  1832. * Queued signals ignored us while we were stopped for tracing.
  1833. * So check for any that we should take before resuming user mode.
  1834. * This sets TIF_SIGPENDING, but never clears it.
  1835. */
  1836. recalc_sigpending_tsk(current);
  1837. }
  1838. static void ptrace_do_notify(int signr, int exit_code, int why)
  1839. {
  1840. siginfo_t info;
  1841. clear_siginfo(&info);
  1842. info.si_signo = signr;
  1843. info.si_code = exit_code;
  1844. info.si_pid = task_pid_vnr(current);
  1845. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  1846. /* Let the debugger run. */
  1847. ptrace_stop(exit_code, why, 1, &info);
  1848. }
  1849. void ptrace_notify(int exit_code)
  1850. {
  1851. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1852. if (unlikely(current->task_works))
  1853. task_work_run();
  1854. spin_lock_irq(&current->sighand->siglock);
  1855. ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
  1856. spin_unlock_irq(&current->sighand->siglock);
  1857. }
  1858. /**
  1859. * do_signal_stop - handle group stop for SIGSTOP and other stop signals
  1860. * @signr: signr causing group stop if initiating
  1861. *
  1862. * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
  1863. * and participate in it. If already set, participate in the existing
  1864. * group stop. If participated in a group stop (and thus slept), %true is
  1865. * returned with siglock released.
  1866. *
  1867. * If ptraced, this function doesn't handle stop itself. Instead,
  1868. * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
  1869. * untouched. The caller must ensure that INTERRUPT trap handling takes
  1870. * places afterwards.
  1871. *
  1872. * CONTEXT:
  1873. * Must be called with @current->sighand->siglock held, which is released
  1874. * on %true return.
  1875. *
  1876. * RETURNS:
  1877. * %false if group stop is already cancelled or ptrace trap is scheduled.
  1878. * %true if participated in group stop.
  1879. */
  1880. static bool do_signal_stop(int signr)
  1881. __releases(&current->sighand->siglock)
  1882. {
  1883. struct signal_struct *sig = current->signal;
  1884. if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
  1885. unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
  1886. struct task_struct *t;
  1887. /* signr will be recorded in task->jobctl for retries */
  1888. WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
  1889. if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
  1890. unlikely(signal_group_exit(sig)))
  1891. return false;
  1892. /*
  1893. * There is no group stop already in progress. We must
  1894. * initiate one now.
  1895. *
  1896. * While ptraced, a task may be resumed while group stop is
  1897. * still in effect and then receive a stop signal and
  1898. * initiate another group stop. This deviates from the
  1899. * usual behavior as two consecutive stop signals can't
  1900. * cause two group stops when !ptraced. That is why we
  1901. * also check !task_is_stopped(t) below.
  1902. *
  1903. * The condition can be distinguished by testing whether
  1904. * SIGNAL_STOP_STOPPED is already set. Don't generate
  1905. * group_exit_code in such case.
  1906. *
  1907. * This is not necessary for SIGNAL_STOP_CONTINUED because
  1908. * an intervening stop signal is required to cause two
  1909. * continued events regardless of ptrace.
  1910. */
  1911. if (!(sig->flags & SIGNAL_STOP_STOPPED))
  1912. sig->group_exit_code = signr;
  1913. sig->group_stop_count = 0;
  1914. if (task_set_jobctl_pending(current, signr | gstop))
  1915. sig->group_stop_count++;
  1916. t = current;
  1917. while_each_thread(current, t) {
  1918. /*
  1919. * Setting state to TASK_STOPPED for a group
  1920. * stop is always done with the siglock held,
  1921. * so this check has no races.
  1922. */
  1923. if (!task_is_stopped(t) &&
  1924. task_set_jobctl_pending(t, signr | gstop)) {
  1925. sig->group_stop_count++;
  1926. if (likely(!(t->ptrace & PT_SEIZED)))
  1927. signal_wake_up(t, 0);
  1928. else
  1929. ptrace_trap_notify(t);
  1930. }
  1931. }
  1932. }
  1933. if (likely(!current->ptrace)) {
  1934. int notify = 0;
  1935. /*
  1936. * If there are no other threads in the group, or if there
  1937. * is a group stop in progress and we are the last to stop,
  1938. * report to the parent.
  1939. */
  1940. if (task_participate_group_stop(current))
  1941. notify = CLD_STOPPED;
  1942. set_special_state(TASK_STOPPED);
  1943. spin_unlock_irq(&current->sighand->siglock);
  1944. /*
  1945. * Notify the parent of the group stop completion. Because
  1946. * we're not holding either the siglock or tasklist_lock
  1947. * here, ptracer may attach inbetween; however, this is for
  1948. * group stop and should always be delivered to the real
  1949. * parent of the group leader. The new ptracer will get
  1950. * its notification when this task transitions into
  1951. * TASK_TRACED.
  1952. */
  1953. if (notify) {
  1954. read_lock(&tasklist_lock);
  1955. do_notify_parent_cldstop(current, false, notify);
  1956. read_unlock(&tasklist_lock);
  1957. }
  1958. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1959. freezable_schedule();
  1960. return true;
  1961. } else {
  1962. /*
  1963. * While ptraced, group stop is handled by STOP trap.
  1964. * Schedule it and let the caller deal with it.
  1965. */
  1966. task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1967. return false;
  1968. }
  1969. }
  1970. /**
  1971. * do_jobctl_trap - take care of ptrace jobctl traps
  1972. *
  1973. * When PT_SEIZED, it's used for both group stop and explicit
  1974. * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
  1975. * accompanying siginfo. If stopped, lower eight bits of exit_code contain
  1976. * the stop signal; otherwise, %SIGTRAP.
  1977. *
  1978. * When !PT_SEIZED, it's used only for group stop trap with stop signal
  1979. * number as exit_code and no siginfo.
  1980. *
  1981. * CONTEXT:
  1982. * Must be called with @current->sighand->siglock held, which may be
  1983. * released and re-acquired before returning with intervening sleep.
  1984. */
  1985. static void do_jobctl_trap(void)
  1986. {
  1987. struct signal_struct *signal = current->signal;
  1988. int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
  1989. if (current->ptrace & PT_SEIZED) {
  1990. if (!signal->group_stop_count &&
  1991. !(signal->flags & SIGNAL_STOP_STOPPED))
  1992. signr = SIGTRAP;
  1993. WARN_ON_ONCE(!signr);
  1994. ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
  1995. CLD_STOPPED);
  1996. } else {
  1997. WARN_ON_ONCE(!signr);
  1998. ptrace_stop(signr, CLD_STOPPED, 0, NULL);
  1999. current->exit_code = 0;
  2000. }
  2001. }
  2002. static int ptrace_signal(int signr, siginfo_t *info)
  2003. {
  2004. /*
  2005. * We do not check sig_kernel_stop(signr) but set this marker
  2006. * unconditionally because we do not know whether debugger will
  2007. * change signr. This flag has no meaning unless we are going
  2008. * to stop after return from ptrace_stop(). In this case it will
  2009. * be checked in do_signal_stop(), we should only stop if it was
  2010. * not cleared by SIGCONT while we were sleeping. See also the
  2011. * comment in dequeue_signal().
  2012. */
  2013. current->jobctl |= JOBCTL_STOP_DEQUEUED;
  2014. ptrace_stop(signr, CLD_TRAPPED, 0, info);
  2015. /* We're back. Did the debugger cancel the sig? */
  2016. signr = current->exit_code;
  2017. if (signr == 0)
  2018. return signr;
  2019. current->exit_code = 0;
  2020. /*
  2021. * Update the siginfo structure if the signal has
  2022. * changed. If the debugger wanted something
  2023. * specific in the siginfo structure then it should
  2024. * have updated *info via PTRACE_SETSIGINFO.
  2025. */
  2026. if (signr != info->si_signo) {
  2027. clear_siginfo(info);
  2028. info->si_signo = signr;
  2029. info->si_errno = 0;
  2030. info->si_code = SI_USER;
  2031. rcu_read_lock();
  2032. info->si_pid = task_pid_vnr(current->parent);
  2033. info->si_uid = from_kuid_munged(current_user_ns(),
  2034. task_uid(current->parent));
  2035. rcu_read_unlock();
  2036. }
  2037. /* If the (new) signal is now blocked, requeue it. */
  2038. if (sigismember(&current->blocked, signr)) {
  2039. specific_send_sig_info(signr, info, current);
  2040. signr = 0;
  2041. }
  2042. return signr;
  2043. }
  2044. int get_signal(struct ksignal *ksig)
  2045. {
  2046. struct sighand_struct *sighand = current->sighand;
  2047. struct signal_struct *signal = current->signal;
  2048. int signr;
  2049. if (unlikely(current->task_works))
  2050. task_work_run();
  2051. if (unlikely(uprobe_deny_signal()))
  2052. return 0;
  2053. /*
  2054. * Do this once, we can't return to user-mode if freezing() == T.
  2055. * do_signal_stop() and ptrace_stop() do freezable_schedule() and
  2056. * thus do not need another check after return.
  2057. */
  2058. try_to_freeze();
  2059. relock:
  2060. spin_lock_irq(&sighand->siglock);
  2061. /*
  2062. * Every stopped thread goes here after wakeup. Check to see if
  2063. * we should notify the parent, prepare_signal(SIGCONT) encodes
  2064. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  2065. */
  2066. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  2067. int why;
  2068. if (signal->flags & SIGNAL_CLD_CONTINUED)
  2069. why = CLD_CONTINUED;
  2070. else
  2071. why = CLD_STOPPED;
  2072. signal->flags &= ~SIGNAL_CLD_MASK;
  2073. spin_unlock_irq(&sighand->siglock);
  2074. /*
  2075. * Notify the parent that we're continuing. This event is
  2076. * always per-process and doesn't make whole lot of sense
  2077. * for ptracers, who shouldn't consume the state via
  2078. * wait(2) either, but, for backward compatibility, notify
  2079. * the ptracer of the group leader too unless it's gonna be
  2080. * a duplicate.
  2081. */
  2082. read_lock(&tasklist_lock);
  2083. do_notify_parent_cldstop(current, false, why);
  2084. if (ptrace_reparented(current->group_leader))
  2085. do_notify_parent_cldstop(current->group_leader,
  2086. true, why);
  2087. read_unlock(&tasklist_lock);
  2088. goto relock;
  2089. }
  2090. for (;;) {
  2091. struct k_sigaction *ka;
  2092. if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
  2093. do_signal_stop(0))
  2094. goto relock;
  2095. if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
  2096. do_jobctl_trap();
  2097. spin_unlock_irq(&sighand->siglock);
  2098. goto relock;
  2099. }
  2100. signr = dequeue_signal(current, &current->blocked, &ksig->info);
  2101. if (!signr)
  2102. break; /* will return 0 */
  2103. if (unlikely(current->ptrace) && signr != SIGKILL) {
  2104. signr = ptrace_signal(signr, &ksig->info);
  2105. if (!signr)
  2106. continue;
  2107. }
  2108. ka = &sighand->action[signr-1];
  2109. /* Trace actually delivered signals. */
  2110. trace_signal_deliver(signr, &ksig->info, ka);
  2111. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  2112. continue;
  2113. if (ka->sa.sa_handler != SIG_DFL) {
  2114. /* Run the handler. */
  2115. ksig->ka = *ka;
  2116. if (ka->sa.sa_flags & SA_ONESHOT)
  2117. ka->sa.sa_handler = SIG_DFL;
  2118. break; /* will return non-zero "signr" value */
  2119. }
  2120. /*
  2121. * Now we are doing the default action for this signal.
  2122. */
  2123. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  2124. continue;
  2125. /*
  2126. * Global init gets no signals it doesn't want.
  2127. * Container-init gets no signals it doesn't want from same
  2128. * container.
  2129. *
  2130. * Note that if global/container-init sees a sig_kernel_only()
  2131. * signal here, the signal must have been generated internally
  2132. * or must have come from an ancestor namespace. In either
  2133. * case, the signal cannot be dropped.
  2134. */
  2135. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  2136. !sig_kernel_only(signr))
  2137. continue;
  2138. if (sig_kernel_stop(signr)) {
  2139. /*
  2140. * The default action is to stop all threads in
  2141. * the thread group. The job control signals
  2142. * do nothing in an orphaned pgrp, but SIGSTOP
  2143. * always works. Note that siglock needs to be
  2144. * dropped during the call to is_orphaned_pgrp()
  2145. * because of lock ordering with tasklist_lock.
  2146. * This allows an intervening SIGCONT to be posted.
  2147. * We need to check for that and bail out if necessary.
  2148. */
  2149. if (signr != SIGSTOP) {
  2150. spin_unlock_irq(&sighand->siglock);
  2151. /* signals can be posted during this window */
  2152. if (is_current_pgrp_orphaned())
  2153. goto relock;
  2154. spin_lock_irq(&sighand->siglock);
  2155. }
  2156. if (likely(do_signal_stop(ksig->info.si_signo))) {
  2157. /* It released the siglock. */
  2158. goto relock;
  2159. }
  2160. /*
  2161. * We didn't actually stop, due to a race
  2162. * with SIGCONT or something like that.
  2163. */
  2164. continue;
  2165. }
  2166. spin_unlock_irq(&sighand->siglock);
  2167. /*
  2168. * Anything else is fatal, maybe with a core dump.
  2169. */
  2170. current->flags |= PF_SIGNALED;
  2171. if (sig_kernel_coredump(signr)) {
  2172. if (print_fatal_signals)
  2173. print_fatal_signal(ksig->info.si_signo);
  2174. proc_coredump_connector(current);
  2175. /*
  2176. * If it was able to dump core, this kills all
  2177. * other threads in the group and synchronizes with
  2178. * their demise. If we lost the race with another
  2179. * thread getting here, it set group_exit_code
  2180. * first and our do_group_exit call below will use
  2181. * that value and ignore the one we pass it.
  2182. */
  2183. do_coredump(&ksig->info);
  2184. }
  2185. /*
  2186. * Death signals, no core dump.
  2187. */
  2188. do_group_exit(ksig->info.si_signo);
  2189. /* NOTREACHED */
  2190. }
  2191. spin_unlock_irq(&sighand->siglock);
  2192. ksig->sig = signr;
  2193. return ksig->sig > 0;
  2194. }
  2195. /**
  2196. * signal_delivered -
  2197. * @ksig: kernel signal struct
  2198. * @stepping: nonzero if debugger single-step or block-step in use
  2199. *
  2200. * This function should be called when a signal has successfully been
  2201. * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
  2202. * is always blocked, and the signal itself is blocked unless %SA_NODEFER
  2203. * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
  2204. */
  2205. static void signal_delivered(struct ksignal *ksig, int stepping)
  2206. {
  2207. sigset_t blocked;
  2208. /* A signal was successfully delivered, and the
  2209. saved sigmask was stored on the signal frame,
  2210. and will be restored by sigreturn. So we can
  2211. simply clear the restore sigmask flag. */
  2212. clear_restore_sigmask();
  2213. sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
  2214. if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
  2215. sigaddset(&blocked, ksig->sig);
  2216. set_current_blocked(&blocked);
  2217. tracehook_signal_handler(stepping);
  2218. }
  2219. void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
  2220. {
  2221. if (failed)
  2222. force_sigsegv(ksig->sig, current);
  2223. else
  2224. signal_delivered(ksig, stepping);
  2225. }
  2226. /*
  2227. * It could be that complete_signal() picked us to notify about the
  2228. * group-wide signal. Other threads should be notified now to take
  2229. * the shared signals in @which since we will not.
  2230. */
  2231. static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
  2232. {
  2233. sigset_t retarget;
  2234. struct task_struct *t;
  2235. sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
  2236. if (sigisemptyset(&retarget))
  2237. return;
  2238. t = tsk;
  2239. while_each_thread(tsk, t) {
  2240. if (t->flags & PF_EXITING)
  2241. continue;
  2242. if (!has_pending_signals(&retarget, &t->blocked))
  2243. continue;
  2244. /* Remove the signals this thread can handle. */
  2245. sigandsets(&retarget, &retarget, &t->blocked);
  2246. if (!signal_pending(t))
  2247. signal_wake_up(t, 0);
  2248. if (sigisemptyset(&retarget))
  2249. break;
  2250. }
  2251. }
  2252. void exit_signals(struct task_struct *tsk)
  2253. {
  2254. int group_stop = 0;
  2255. sigset_t unblocked;
  2256. /*
  2257. * @tsk is about to have PF_EXITING set - lock out users which
  2258. * expect stable threadgroup.
  2259. */
  2260. cgroup_threadgroup_change_begin(tsk);
  2261. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  2262. tsk->flags |= PF_EXITING;
  2263. cgroup_threadgroup_change_end(tsk);
  2264. return;
  2265. }
  2266. spin_lock_irq(&tsk->sighand->siglock);
  2267. /*
  2268. * From now this task is not visible for group-wide signals,
  2269. * see wants_signal(), do_signal_stop().
  2270. */
  2271. tsk->flags |= PF_EXITING;
  2272. cgroup_threadgroup_change_end(tsk);
  2273. if (!signal_pending(tsk))
  2274. goto out;
  2275. unblocked = tsk->blocked;
  2276. signotset(&unblocked);
  2277. retarget_shared_pending(tsk, &unblocked);
  2278. if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
  2279. task_participate_group_stop(tsk))
  2280. group_stop = CLD_STOPPED;
  2281. out:
  2282. spin_unlock_irq(&tsk->sighand->siglock);
  2283. /*
  2284. * If group stop has completed, deliver the notification. This
  2285. * should always go to the real parent of the group leader.
  2286. */
  2287. if (unlikely(group_stop)) {
  2288. read_lock(&tasklist_lock);
  2289. do_notify_parent_cldstop(tsk, false, group_stop);
  2290. read_unlock(&tasklist_lock);
  2291. }
  2292. }
  2293. EXPORT_SYMBOL(recalc_sigpending);
  2294. EXPORT_SYMBOL_GPL(dequeue_signal);
  2295. EXPORT_SYMBOL(flush_signals);
  2296. EXPORT_SYMBOL(force_sig);
  2297. EXPORT_SYMBOL(send_sig);
  2298. EXPORT_SYMBOL(send_sig_info);
  2299. EXPORT_SYMBOL(sigprocmask);
  2300. /*
  2301. * System call entry points.
  2302. */
  2303. /**
  2304. * sys_restart_syscall - restart a system call
  2305. */
  2306. SYSCALL_DEFINE0(restart_syscall)
  2307. {
  2308. struct restart_block *restart = &current->restart_block;
  2309. return restart->fn(restart);
  2310. }
  2311. long do_no_restart_syscall(struct restart_block *param)
  2312. {
  2313. return -EINTR;
  2314. }
  2315. static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
  2316. {
  2317. if (signal_pending(tsk) && !thread_group_empty(tsk)) {
  2318. sigset_t newblocked;
  2319. /* A set of now blocked but previously unblocked signals. */
  2320. sigandnsets(&newblocked, newset, &current->blocked);
  2321. retarget_shared_pending(tsk, &newblocked);
  2322. }
  2323. tsk->blocked = *newset;
  2324. recalc_sigpending();
  2325. }
  2326. /**
  2327. * set_current_blocked - change current->blocked mask
  2328. * @newset: new mask
  2329. *
  2330. * It is wrong to change ->blocked directly, this helper should be used
  2331. * to ensure the process can't miss a shared signal we are going to block.
  2332. */
  2333. void set_current_blocked(sigset_t *newset)
  2334. {
  2335. sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2336. __set_current_blocked(newset);
  2337. }
  2338. void __set_current_blocked(const sigset_t *newset)
  2339. {
  2340. struct task_struct *tsk = current;
  2341. /*
  2342. * In case the signal mask hasn't changed, there is nothing we need
  2343. * to do. The current->blocked shouldn't be modified by other task.
  2344. */
  2345. if (sigequalsets(&tsk->blocked, newset))
  2346. return;
  2347. spin_lock_irq(&tsk->sighand->siglock);
  2348. __set_task_blocked(tsk, newset);
  2349. spin_unlock_irq(&tsk->sighand->siglock);
  2350. }
  2351. /*
  2352. * This is also useful for kernel threads that want to temporarily
  2353. * (or permanently) block certain signals.
  2354. *
  2355. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  2356. * interface happily blocks "unblockable" signals like SIGKILL
  2357. * and friends.
  2358. */
  2359. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  2360. {
  2361. struct task_struct *tsk = current;
  2362. sigset_t newset;
  2363. /* Lockless, only current can change ->blocked, never from irq */
  2364. if (oldset)
  2365. *oldset = tsk->blocked;
  2366. switch (how) {
  2367. case SIG_BLOCK:
  2368. sigorsets(&newset, &tsk->blocked, set);
  2369. break;
  2370. case SIG_UNBLOCK:
  2371. sigandnsets(&newset, &tsk->blocked, set);
  2372. break;
  2373. case SIG_SETMASK:
  2374. newset = *set;
  2375. break;
  2376. default:
  2377. return -EINVAL;
  2378. }
  2379. __set_current_blocked(&newset);
  2380. return 0;
  2381. }
  2382. /**
  2383. * sys_rt_sigprocmask - change the list of currently blocked signals
  2384. * @how: whether to add, remove, or set signals
  2385. * @nset: stores pending signals
  2386. * @oset: previous value of signal mask if non-null
  2387. * @sigsetsize: size of sigset_t type
  2388. */
  2389. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
  2390. sigset_t __user *, oset, size_t, sigsetsize)
  2391. {
  2392. sigset_t old_set, new_set;
  2393. int error;
  2394. /* XXX: Don't preclude handling different sized sigset_t's. */
  2395. if (sigsetsize != sizeof(sigset_t))
  2396. return -EINVAL;
  2397. old_set = current->blocked;
  2398. if (nset) {
  2399. if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
  2400. return -EFAULT;
  2401. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2402. error = sigprocmask(how, &new_set, NULL);
  2403. if (error)
  2404. return error;
  2405. }
  2406. if (oset) {
  2407. if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
  2408. return -EFAULT;
  2409. }
  2410. return 0;
  2411. }
  2412. #ifdef CONFIG_COMPAT
  2413. COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
  2414. compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
  2415. {
  2416. sigset_t old_set = current->blocked;
  2417. /* XXX: Don't preclude handling different sized sigset_t's. */
  2418. if (sigsetsize != sizeof(sigset_t))
  2419. return -EINVAL;
  2420. if (nset) {
  2421. sigset_t new_set;
  2422. int error;
  2423. if (get_compat_sigset(&new_set, nset))
  2424. return -EFAULT;
  2425. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2426. error = sigprocmask(how, &new_set, NULL);
  2427. if (error)
  2428. return error;
  2429. }
  2430. return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
  2431. }
  2432. #endif
  2433. static int do_sigpending(sigset_t *set)
  2434. {
  2435. spin_lock_irq(&current->sighand->siglock);
  2436. sigorsets(set, &current->pending.signal,
  2437. &current->signal->shared_pending.signal);
  2438. spin_unlock_irq(&current->sighand->siglock);
  2439. /* Outside the lock because only this thread touches it. */
  2440. sigandsets(set, &current->blocked, set);
  2441. return 0;
  2442. }
  2443. /**
  2444. * sys_rt_sigpending - examine a pending signal that has been raised
  2445. * while blocked
  2446. * @uset: stores pending signals
  2447. * @sigsetsize: size of sigset_t type or larger
  2448. */
  2449. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
  2450. {
  2451. sigset_t set;
  2452. int err;
  2453. if (sigsetsize > sizeof(*uset))
  2454. return -EINVAL;
  2455. err = do_sigpending(&set);
  2456. if (!err && copy_to_user(uset, &set, sigsetsize))
  2457. err = -EFAULT;
  2458. return err;
  2459. }
  2460. #ifdef CONFIG_COMPAT
  2461. COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
  2462. compat_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)
  2470. err = put_compat_sigset(uset, &set, sigsetsize);
  2471. return err;
  2472. }
  2473. #endif
  2474. enum siginfo_layout siginfo_layout(int sig, int si_code)
  2475. {
  2476. enum siginfo_layout layout = SIL_KILL;
  2477. if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
  2478. static const struct {
  2479. unsigned char limit, layout;
  2480. } filter[] = {
  2481. [SIGILL] = { NSIGILL, SIL_FAULT },
  2482. [SIGFPE] = { NSIGFPE, SIL_FAULT },
  2483. [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
  2484. [SIGBUS] = { NSIGBUS, SIL_FAULT },
  2485. [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
  2486. #if defined(SIGEMT) && defined(NSIGEMT)
  2487. [SIGEMT] = { NSIGEMT, SIL_FAULT },
  2488. #endif
  2489. [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
  2490. [SIGPOLL] = { NSIGPOLL, SIL_POLL },
  2491. [SIGSYS] = { NSIGSYS, SIL_SYS },
  2492. };
  2493. if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit)) {
  2494. layout = filter[sig].layout;
  2495. /* Handle the exceptions */
  2496. if ((sig == SIGBUS) &&
  2497. (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
  2498. layout = SIL_FAULT_MCEERR;
  2499. else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
  2500. layout = SIL_FAULT_BNDERR;
  2501. #ifdef SEGV_PKUERR
  2502. else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
  2503. layout = SIL_FAULT_PKUERR;
  2504. #endif
  2505. }
  2506. else if (si_code <= NSIGPOLL)
  2507. layout = SIL_POLL;
  2508. } else {
  2509. if (si_code == SI_TIMER)
  2510. layout = SIL_TIMER;
  2511. else if (si_code == SI_SIGIO)
  2512. layout = SIL_POLL;
  2513. else if (si_code < 0)
  2514. layout = SIL_RT;
  2515. }
  2516. return layout;
  2517. }
  2518. int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
  2519. {
  2520. if (copy_to_user(to, from , sizeof(struct siginfo)))
  2521. return -EFAULT;
  2522. return 0;
  2523. }
  2524. #ifdef CONFIG_COMPAT
  2525. int copy_siginfo_to_user32(struct compat_siginfo __user *to,
  2526. const struct siginfo *from)
  2527. #if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
  2528. {
  2529. return __copy_siginfo_to_user32(to, from, in_x32_syscall());
  2530. }
  2531. int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
  2532. const struct siginfo *from, bool x32_ABI)
  2533. #endif
  2534. {
  2535. struct compat_siginfo new;
  2536. memset(&new, 0, sizeof(new));
  2537. new.si_signo = from->si_signo;
  2538. new.si_errno = from->si_errno;
  2539. new.si_code = from->si_code;
  2540. switch(siginfo_layout(from->si_signo, from->si_code)) {
  2541. case SIL_KILL:
  2542. new.si_pid = from->si_pid;
  2543. new.si_uid = from->si_uid;
  2544. break;
  2545. case SIL_TIMER:
  2546. new.si_tid = from->si_tid;
  2547. new.si_overrun = from->si_overrun;
  2548. new.si_int = from->si_int;
  2549. break;
  2550. case SIL_POLL:
  2551. new.si_band = from->si_band;
  2552. new.si_fd = from->si_fd;
  2553. break;
  2554. case SIL_FAULT:
  2555. new.si_addr = ptr_to_compat(from->si_addr);
  2556. #ifdef __ARCH_SI_TRAPNO
  2557. new.si_trapno = from->si_trapno;
  2558. #endif
  2559. break;
  2560. case SIL_FAULT_MCEERR:
  2561. new.si_addr = ptr_to_compat(from->si_addr);
  2562. #ifdef __ARCH_SI_TRAPNO
  2563. new.si_trapno = from->si_trapno;
  2564. #endif
  2565. new.si_addr_lsb = from->si_addr_lsb;
  2566. break;
  2567. case SIL_FAULT_BNDERR:
  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. new.si_lower = ptr_to_compat(from->si_lower);
  2573. new.si_upper = ptr_to_compat(from->si_upper);
  2574. break;
  2575. case SIL_FAULT_PKUERR:
  2576. new.si_addr = ptr_to_compat(from->si_addr);
  2577. #ifdef __ARCH_SI_TRAPNO
  2578. new.si_trapno = from->si_trapno;
  2579. #endif
  2580. new.si_pkey = from->si_pkey;
  2581. break;
  2582. case SIL_CHLD:
  2583. new.si_pid = from->si_pid;
  2584. new.si_uid = from->si_uid;
  2585. new.si_status = from->si_status;
  2586. #ifdef CONFIG_X86_X32_ABI
  2587. if (x32_ABI) {
  2588. new._sifields._sigchld_x32._utime = from->si_utime;
  2589. new._sifields._sigchld_x32._stime = from->si_stime;
  2590. } else
  2591. #endif
  2592. {
  2593. new.si_utime = from->si_utime;
  2594. new.si_stime = from->si_stime;
  2595. }
  2596. break;
  2597. case SIL_RT:
  2598. new.si_pid = from->si_pid;
  2599. new.si_uid = from->si_uid;
  2600. new.si_int = from->si_int;
  2601. break;
  2602. case SIL_SYS:
  2603. new.si_call_addr = ptr_to_compat(from->si_call_addr);
  2604. new.si_syscall = from->si_syscall;
  2605. new.si_arch = from->si_arch;
  2606. break;
  2607. }
  2608. if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
  2609. return -EFAULT;
  2610. return 0;
  2611. }
  2612. int copy_siginfo_from_user32(struct siginfo *to,
  2613. const struct compat_siginfo __user *ufrom)
  2614. {
  2615. struct compat_siginfo from;
  2616. if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
  2617. return -EFAULT;
  2618. clear_siginfo(to);
  2619. to->si_signo = from.si_signo;
  2620. to->si_errno = from.si_errno;
  2621. to->si_code = from.si_code;
  2622. switch(siginfo_layout(from.si_signo, from.si_code)) {
  2623. case SIL_KILL:
  2624. to->si_pid = from.si_pid;
  2625. to->si_uid = from.si_uid;
  2626. break;
  2627. case SIL_TIMER:
  2628. to->si_tid = from.si_tid;
  2629. to->si_overrun = from.si_overrun;
  2630. to->si_int = from.si_int;
  2631. break;
  2632. case SIL_POLL:
  2633. to->si_band = from.si_band;
  2634. to->si_fd = from.si_fd;
  2635. break;
  2636. case SIL_FAULT:
  2637. to->si_addr = compat_ptr(from.si_addr);
  2638. #ifdef __ARCH_SI_TRAPNO
  2639. to->si_trapno = from.si_trapno;
  2640. #endif
  2641. break;
  2642. case SIL_FAULT_MCEERR:
  2643. to->si_addr = compat_ptr(from.si_addr);
  2644. #ifdef __ARCH_SI_TRAPNO
  2645. to->si_trapno = from.si_trapno;
  2646. #endif
  2647. to->si_addr_lsb = from.si_addr_lsb;
  2648. break;
  2649. case SIL_FAULT_BNDERR:
  2650. to->si_addr = compat_ptr(from.si_addr);
  2651. #ifdef __ARCH_SI_TRAPNO
  2652. to->si_trapno = from.si_trapno;
  2653. #endif
  2654. to->si_lower = compat_ptr(from.si_lower);
  2655. to->si_upper = compat_ptr(from.si_upper);
  2656. break;
  2657. case SIL_FAULT_PKUERR:
  2658. to->si_addr = compat_ptr(from.si_addr);
  2659. #ifdef __ARCH_SI_TRAPNO
  2660. to->si_trapno = from.si_trapno;
  2661. #endif
  2662. to->si_pkey = from.si_pkey;
  2663. break;
  2664. case SIL_CHLD:
  2665. to->si_pid = from.si_pid;
  2666. to->si_uid = from.si_uid;
  2667. to->si_status = from.si_status;
  2668. #ifdef CONFIG_X86_X32_ABI
  2669. if (in_x32_syscall()) {
  2670. to->si_utime = from._sifields._sigchld_x32._utime;
  2671. to->si_stime = from._sifields._sigchld_x32._stime;
  2672. } else
  2673. #endif
  2674. {
  2675. to->si_utime = from.si_utime;
  2676. to->si_stime = from.si_stime;
  2677. }
  2678. break;
  2679. case SIL_RT:
  2680. to->si_pid = from.si_pid;
  2681. to->si_uid = from.si_uid;
  2682. to->si_int = from.si_int;
  2683. break;
  2684. case SIL_SYS:
  2685. to->si_call_addr = compat_ptr(from.si_call_addr);
  2686. to->si_syscall = from.si_syscall;
  2687. to->si_arch = from.si_arch;
  2688. break;
  2689. }
  2690. return 0;
  2691. }
  2692. #endif /* CONFIG_COMPAT */
  2693. /**
  2694. * do_sigtimedwait - wait for queued signals specified in @which
  2695. * @which: queued signals to wait for
  2696. * @info: if non-null, the signal's siginfo is returned here
  2697. * @ts: upper bound on process time suspension
  2698. */
  2699. static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
  2700. const struct timespec *ts)
  2701. {
  2702. ktime_t *to = NULL, timeout = KTIME_MAX;
  2703. struct task_struct *tsk = current;
  2704. sigset_t mask = *which;
  2705. int sig, ret = 0;
  2706. if (ts) {
  2707. if (!timespec_valid(ts))
  2708. return -EINVAL;
  2709. timeout = timespec_to_ktime(*ts);
  2710. to = &timeout;
  2711. }
  2712. /*
  2713. * Invert the set of allowed signals to get those we want to block.
  2714. */
  2715. sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2716. signotset(&mask);
  2717. spin_lock_irq(&tsk->sighand->siglock);
  2718. sig = dequeue_signal(tsk, &mask, info);
  2719. if (!sig && timeout) {
  2720. /*
  2721. * None ready, temporarily unblock those we're interested
  2722. * while we are sleeping in so that we'll be awakened when
  2723. * they arrive. Unblocking is always fine, we can avoid
  2724. * set_current_blocked().
  2725. */
  2726. tsk->real_blocked = tsk->blocked;
  2727. sigandsets(&tsk->blocked, &tsk->blocked, &mask);
  2728. recalc_sigpending();
  2729. spin_unlock_irq(&tsk->sighand->siglock);
  2730. __set_current_state(TASK_INTERRUPTIBLE);
  2731. ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
  2732. HRTIMER_MODE_REL);
  2733. spin_lock_irq(&tsk->sighand->siglock);
  2734. __set_task_blocked(tsk, &tsk->real_blocked);
  2735. sigemptyset(&tsk->real_blocked);
  2736. sig = dequeue_signal(tsk, &mask, info);
  2737. }
  2738. spin_unlock_irq(&tsk->sighand->siglock);
  2739. if (sig)
  2740. return sig;
  2741. return ret ? -EINTR : -EAGAIN;
  2742. }
  2743. /**
  2744. * sys_rt_sigtimedwait - synchronously wait for queued signals specified
  2745. * in @uthese
  2746. * @uthese: queued signals to wait for
  2747. * @uinfo: if non-null, the signal's siginfo is returned here
  2748. * @uts: upper bound on process time suspension
  2749. * @sigsetsize: size of sigset_t type
  2750. */
  2751. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  2752. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  2753. size_t, sigsetsize)
  2754. {
  2755. sigset_t these;
  2756. struct timespec ts;
  2757. siginfo_t info;
  2758. int ret;
  2759. /* XXX: Don't preclude handling different sized sigset_t's. */
  2760. if (sigsetsize != sizeof(sigset_t))
  2761. return -EINVAL;
  2762. if (copy_from_user(&these, uthese, sizeof(these)))
  2763. return -EFAULT;
  2764. if (uts) {
  2765. if (copy_from_user(&ts, uts, sizeof(ts)))
  2766. return -EFAULT;
  2767. }
  2768. ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
  2769. if (ret > 0 && uinfo) {
  2770. if (copy_siginfo_to_user(uinfo, &info))
  2771. ret = -EFAULT;
  2772. }
  2773. return ret;
  2774. }
  2775. #ifdef CONFIG_COMPAT
  2776. COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
  2777. struct compat_siginfo __user *, uinfo,
  2778. struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
  2779. {
  2780. sigset_t s;
  2781. struct timespec t;
  2782. siginfo_t info;
  2783. long ret;
  2784. if (sigsetsize != sizeof(sigset_t))
  2785. return -EINVAL;
  2786. if (get_compat_sigset(&s, uthese))
  2787. return -EFAULT;
  2788. if (uts) {
  2789. if (compat_get_timespec(&t, uts))
  2790. return -EFAULT;
  2791. }
  2792. ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
  2793. if (ret > 0 && uinfo) {
  2794. if (copy_siginfo_to_user32(uinfo, &info))
  2795. ret = -EFAULT;
  2796. }
  2797. return ret;
  2798. }
  2799. #endif
  2800. /**
  2801. * sys_kill - send a signal to a process
  2802. * @pid: the PID of the process
  2803. * @sig: signal to be sent
  2804. */
  2805. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2806. {
  2807. struct siginfo info;
  2808. clear_siginfo(&info);
  2809. info.si_signo = sig;
  2810. info.si_errno = 0;
  2811. info.si_code = SI_USER;
  2812. info.si_pid = task_tgid_vnr(current);
  2813. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2814. return kill_something_info(sig, &info, pid);
  2815. }
  2816. static int
  2817. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2818. {
  2819. struct task_struct *p;
  2820. int error = -ESRCH;
  2821. rcu_read_lock();
  2822. p = find_task_by_vpid(pid);
  2823. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2824. error = check_kill_permission(sig, info, p);
  2825. /*
  2826. * The null signal is a permissions and process existence
  2827. * probe. No signal is actually delivered.
  2828. */
  2829. if (!error && sig) {
  2830. error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
  2831. /*
  2832. * If lock_task_sighand() failed we pretend the task
  2833. * dies after receiving the signal. The window is tiny,
  2834. * and the signal is private anyway.
  2835. */
  2836. if (unlikely(error == -ESRCH))
  2837. error = 0;
  2838. }
  2839. }
  2840. rcu_read_unlock();
  2841. return error;
  2842. }
  2843. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2844. {
  2845. struct siginfo info;
  2846. clear_siginfo(&info);
  2847. info.si_signo = sig;
  2848. info.si_errno = 0;
  2849. info.si_code = SI_TKILL;
  2850. info.si_pid = task_tgid_vnr(current);
  2851. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2852. return do_send_specific(tgid, pid, sig, &info);
  2853. }
  2854. /**
  2855. * sys_tgkill - send signal to one specific thread
  2856. * @tgid: the thread group ID of the thread
  2857. * @pid: the PID of the thread
  2858. * @sig: signal to be sent
  2859. *
  2860. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2861. * exists but it's not belonging to the target process anymore. This
  2862. * method solves the problem of threads exiting and PIDs getting reused.
  2863. */
  2864. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2865. {
  2866. /* This is only valid for single tasks */
  2867. if (pid <= 0 || tgid <= 0)
  2868. return -EINVAL;
  2869. return do_tkill(tgid, pid, sig);
  2870. }
  2871. /**
  2872. * sys_tkill - send signal to one specific task
  2873. * @pid: the PID of the task
  2874. * @sig: signal to be sent
  2875. *
  2876. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2877. */
  2878. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2879. {
  2880. /* This is only valid for single tasks */
  2881. if (pid <= 0)
  2882. return -EINVAL;
  2883. return do_tkill(0, pid, sig);
  2884. }
  2885. static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
  2886. {
  2887. /* Not even root can pretend to send signals from the kernel.
  2888. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2889. */
  2890. if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
  2891. (task_pid_vnr(current) != pid))
  2892. return -EPERM;
  2893. info->si_signo = sig;
  2894. /* POSIX.1b doesn't mention process groups. */
  2895. return kill_proc_info(sig, info, pid);
  2896. }
  2897. /**
  2898. * sys_rt_sigqueueinfo - send signal information to a signal
  2899. * @pid: the PID of the thread
  2900. * @sig: signal to be sent
  2901. * @uinfo: signal info to be sent
  2902. */
  2903. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2904. siginfo_t __user *, uinfo)
  2905. {
  2906. siginfo_t info;
  2907. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2908. return -EFAULT;
  2909. return do_rt_sigqueueinfo(pid, sig, &info);
  2910. }
  2911. #ifdef CONFIG_COMPAT
  2912. COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
  2913. compat_pid_t, pid,
  2914. int, sig,
  2915. struct compat_siginfo __user *, uinfo)
  2916. {
  2917. siginfo_t info;
  2918. int ret = copy_siginfo_from_user32(&info, uinfo);
  2919. if (unlikely(ret))
  2920. return ret;
  2921. return do_rt_sigqueueinfo(pid, sig, &info);
  2922. }
  2923. #endif
  2924. static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2925. {
  2926. /* This is only valid for single tasks */
  2927. if (pid <= 0 || tgid <= 0)
  2928. return -EINVAL;
  2929. /* Not even root can pretend to send signals from the kernel.
  2930. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2931. */
  2932. if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
  2933. (task_pid_vnr(current) != pid))
  2934. return -EPERM;
  2935. info->si_signo = sig;
  2936. return do_send_specific(tgid, pid, sig, info);
  2937. }
  2938. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2939. siginfo_t __user *, uinfo)
  2940. {
  2941. siginfo_t info;
  2942. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2943. return -EFAULT;
  2944. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2945. }
  2946. #ifdef CONFIG_COMPAT
  2947. COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
  2948. compat_pid_t, tgid,
  2949. compat_pid_t, pid,
  2950. int, sig,
  2951. struct compat_siginfo __user *, uinfo)
  2952. {
  2953. siginfo_t info;
  2954. if (copy_siginfo_from_user32(&info, uinfo))
  2955. return -EFAULT;
  2956. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2957. }
  2958. #endif
  2959. /*
  2960. * For kthreads only, must not be used if cloned with CLONE_SIGHAND
  2961. */
  2962. void kernel_sigaction(int sig, __sighandler_t action)
  2963. {
  2964. spin_lock_irq(&current->sighand->siglock);
  2965. current->sighand->action[sig - 1].sa.sa_handler = action;
  2966. if (action == SIG_IGN) {
  2967. sigset_t mask;
  2968. sigemptyset(&mask);
  2969. sigaddset(&mask, sig);
  2970. flush_sigqueue_mask(&mask, &current->signal->shared_pending);
  2971. flush_sigqueue_mask(&mask, &current->pending);
  2972. recalc_sigpending();
  2973. }
  2974. spin_unlock_irq(&current->sighand->siglock);
  2975. }
  2976. EXPORT_SYMBOL(kernel_sigaction);
  2977. void __weak sigaction_compat_abi(struct k_sigaction *act,
  2978. struct k_sigaction *oact)
  2979. {
  2980. }
  2981. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2982. {
  2983. struct task_struct *p = current, *t;
  2984. struct k_sigaction *k;
  2985. sigset_t mask;
  2986. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2987. return -EINVAL;
  2988. k = &p->sighand->action[sig-1];
  2989. spin_lock_irq(&p->sighand->siglock);
  2990. if (oact)
  2991. *oact = *k;
  2992. sigaction_compat_abi(act, oact);
  2993. if (act) {
  2994. sigdelsetmask(&act->sa.sa_mask,
  2995. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2996. *k = *act;
  2997. /*
  2998. * POSIX 3.3.1.3:
  2999. * "Setting a signal action to SIG_IGN for a signal that is
  3000. * pending shall cause the pending signal to be discarded,
  3001. * whether or not it is blocked."
  3002. *
  3003. * "Setting a signal action to SIG_DFL for a signal that is
  3004. * pending and whose default action is to ignore the signal
  3005. * (for example, SIGCHLD), shall cause the pending signal to
  3006. * be discarded, whether or not it is blocked"
  3007. */
  3008. if (sig_handler_ignored(sig_handler(p, sig), sig)) {
  3009. sigemptyset(&mask);
  3010. sigaddset(&mask, sig);
  3011. flush_sigqueue_mask(&mask, &p->signal->shared_pending);
  3012. for_each_thread(p, t)
  3013. flush_sigqueue_mask(&mask, &t->pending);
  3014. }
  3015. }
  3016. spin_unlock_irq(&p->sighand->siglock);
  3017. return 0;
  3018. }
  3019. static int
  3020. do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp)
  3021. {
  3022. struct task_struct *t = current;
  3023. if (oss) {
  3024. memset(oss, 0, sizeof(stack_t));
  3025. oss->ss_sp = (void __user *) t->sas_ss_sp;
  3026. oss->ss_size = t->sas_ss_size;
  3027. oss->ss_flags = sas_ss_flags(sp) |
  3028. (current->sas_ss_flags & SS_FLAG_BITS);
  3029. }
  3030. if (ss) {
  3031. void __user *ss_sp = ss->ss_sp;
  3032. size_t ss_size = ss->ss_size;
  3033. unsigned ss_flags = ss->ss_flags;
  3034. int ss_mode;
  3035. if (unlikely(on_sig_stack(sp)))
  3036. return -EPERM;
  3037. ss_mode = ss_flags & ~SS_FLAG_BITS;
  3038. if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
  3039. ss_mode != 0))
  3040. return -EINVAL;
  3041. if (ss_mode == SS_DISABLE) {
  3042. ss_size = 0;
  3043. ss_sp = NULL;
  3044. } else {
  3045. if (unlikely(ss_size < MINSIGSTKSZ))
  3046. return -ENOMEM;
  3047. }
  3048. t->sas_ss_sp = (unsigned long) ss_sp;
  3049. t->sas_ss_size = ss_size;
  3050. t->sas_ss_flags = ss_flags;
  3051. }
  3052. return 0;
  3053. }
  3054. SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
  3055. {
  3056. stack_t new, old;
  3057. int err;
  3058. if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
  3059. return -EFAULT;
  3060. err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
  3061. current_user_stack_pointer());
  3062. if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
  3063. err = -EFAULT;
  3064. return err;
  3065. }
  3066. int restore_altstack(const stack_t __user *uss)
  3067. {
  3068. stack_t new;
  3069. if (copy_from_user(&new, uss, sizeof(stack_t)))
  3070. return -EFAULT;
  3071. (void)do_sigaltstack(&new, NULL, current_user_stack_pointer());
  3072. /* squash all but EFAULT for now */
  3073. return 0;
  3074. }
  3075. int __save_altstack(stack_t __user *uss, unsigned long sp)
  3076. {
  3077. struct task_struct *t = current;
  3078. int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
  3079. __put_user(t->sas_ss_flags, &uss->ss_flags) |
  3080. __put_user(t->sas_ss_size, &uss->ss_size);
  3081. if (err)
  3082. return err;
  3083. if (t->sas_ss_flags & SS_AUTODISARM)
  3084. sas_ss_reset(t);
  3085. return 0;
  3086. }
  3087. #ifdef CONFIG_COMPAT
  3088. static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
  3089. compat_stack_t __user *uoss_ptr)
  3090. {
  3091. stack_t uss, uoss;
  3092. int ret;
  3093. if (uss_ptr) {
  3094. compat_stack_t uss32;
  3095. if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
  3096. return -EFAULT;
  3097. uss.ss_sp = compat_ptr(uss32.ss_sp);
  3098. uss.ss_flags = uss32.ss_flags;
  3099. uss.ss_size = uss32.ss_size;
  3100. }
  3101. ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
  3102. compat_user_stack_pointer());
  3103. if (ret >= 0 && uoss_ptr) {
  3104. compat_stack_t old;
  3105. memset(&old, 0, sizeof(old));
  3106. old.ss_sp = ptr_to_compat(uoss.ss_sp);
  3107. old.ss_flags = uoss.ss_flags;
  3108. old.ss_size = uoss.ss_size;
  3109. if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
  3110. ret = -EFAULT;
  3111. }
  3112. return ret;
  3113. }
  3114. COMPAT_SYSCALL_DEFINE2(sigaltstack,
  3115. const compat_stack_t __user *, uss_ptr,
  3116. compat_stack_t __user *, uoss_ptr)
  3117. {
  3118. return do_compat_sigaltstack(uss_ptr, uoss_ptr);
  3119. }
  3120. int compat_restore_altstack(const compat_stack_t __user *uss)
  3121. {
  3122. int err = do_compat_sigaltstack(uss, NULL);
  3123. /* squash all but -EFAULT for now */
  3124. return err == -EFAULT ? err : 0;
  3125. }
  3126. int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
  3127. {
  3128. int err;
  3129. struct task_struct *t = current;
  3130. err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
  3131. &uss->ss_sp) |
  3132. __put_user(t->sas_ss_flags, &uss->ss_flags) |
  3133. __put_user(t->sas_ss_size, &uss->ss_size);
  3134. if (err)
  3135. return err;
  3136. if (t->sas_ss_flags & SS_AUTODISARM)
  3137. sas_ss_reset(t);
  3138. return 0;
  3139. }
  3140. #endif
  3141. #ifdef __ARCH_WANT_SYS_SIGPENDING
  3142. /**
  3143. * sys_sigpending - examine pending signals
  3144. * @uset: where mask of pending signal is returned
  3145. */
  3146. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
  3147. {
  3148. sigset_t set;
  3149. int err;
  3150. if (sizeof(old_sigset_t) > sizeof(*uset))
  3151. return -EINVAL;
  3152. err = do_sigpending(&set);
  3153. if (!err && copy_to_user(uset, &set, sizeof(old_sigset_t)))
  3154. err = -EFAULT;
  3155. return err;
  3156. }
  3157. #ifdef CONFIG_COMPAT
  3158. COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
  3159. {
  3160. sigset_t set;
  3161. int err = do_sigpending(&set);
  3162. if (!err)
  3163. err = put_user(set.sig[0], set32);
  3164. return err;
  3165. }
  3166. #endif
  3167. #endif
  3168. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  3169. /**
  3170. * sys_sigprocmask - examine and change blocked signals
  3171. * @how: whether to add, remove, or set signals
  3172. * @nset: signals to add or remove (if non-null)
  3173. * @oset: previous value of signal mask if non-null
  3174. *
  3175. * Some platforms have their own version with special arguments;
  3176. * others support only sys_rt_sigprocmask.
  3177. */
  3178. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
  3179. old_sigset_t __user *, oset)
  3180. {
  3181. old_sigset_t old_set, new_set;
  3182. sigset_t new_blocked;
  3183. old_set = current->blocked.sig[0];
  3184. if (nset) {
  3185. if (copy_from_user(&new_set, nset, sizeof(*nset)))
  3186. return -EFAULT;
  3187. new_blocked = current->blocked;
  3188. switch (how) {
  3189. case SIG_BLOCK:
  3190. sigaddsetmask(&new_blocked, new_set);
  3191. break;
  3192. case SIG_UNBLOCK:
  3193. sigdelsetmask(&new_blocked, new_set);
  3194. break;
  3195. case SIG_SETMASK:
  3196. new_blocked.sig[0] = new_set;
  3197. break;
  3198. default:
  3199. return -EINVAL;
  3200. }
  3201. set_current_blocked(&new_blocked);
  3202. }
  3203. if (oset) {
  3204. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  3205. return -EFAULT;
  3206. }
  3207. return 0;
  3208. }
  3209. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  3210. #ifndef CONFIG_ODD_RT_SIGACTION
  3211. /**
  3212. * sys_rt_sigaction - alter an action taken by a process
  3213. * @sig: signal to be sent
  3214. * @act: new sigaction
  3215. * @oact: used to save the previous sigaction
  3216. * @sigsetsize: size of sigset_t type
  3217. */
  3218. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  3219. const struct sigaction __user *, act,
  3220. struct sigaction __user *, oact,
  3221. size_t, sigsetsize)
  3222. {
  3223. struct k_sigaction new_sa, old_sa;
  3224. int ret = -EINVAL;
  3225. /* XXX: Don't preclude handling different sized sigset_t's. */
  3226. if (sigsetsize != sizeof(sigset_t))
  3227. goto out;
  3228. if (act) {
  3229. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  3230. return -EFAULT;
  3231. }
  3232. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  3233. if (!ret && oact) {
  3234. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  3235. return -EFAULT;
  3236. }
  3237. out:
  3238. return ret;
  3239. }
  3240. #ifdef CONFIG_COMPAT
  3241. COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
  3242. const struct compat_sigaction __user *, act,
  3243. struct compat_sigaction __user *, oact,
  3244. compat_size_t, sigsetsize)
  3245. {
  3246. struct k_sigaction new_ka, old_ka;
  3247. #ifdef __ARCH_HAS_SA_RESTORER
  3248. compat_uptr_t restorer;
  3249. #endif
  3250. int ret;
  3251. /* XXX: Don't preclude handling different sized sigset_t's. */
  3252. if (sigsetsize != sizeof(compat_sigset_t))
  3253. return -EINVAL;
  3254. if (act) {
  3255. compat_uptr_t handler;
  3256. ret = get_user(handler, &act->sa_handler);
  3257. new_ka.sa.sa_handler = compat_ptr(handler);
  3258. #ifdef __ARCH_HAS_SA_RESTORER
  3259. ret |= get_user(restorer, &act->sa_restorer);
  3260. new_ka.sa.sa_restorer = compat_ptr(restorer);
  3261. #endif
  3262. ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
  3263. ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
  3264. if (ret)
  3265. return -EFAULT;
  3266. }
  3267. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3268. if (!ret && oact) {
  3269. ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
  3270. &oact->sa_handler);
  3271. ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
  3272. sizeof(oact->sa_mask));
  3273. ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  3274. #ifdef __ARCH_HAS_SA_RESTORER
  3275. ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  3276. &oact->sa_restorer);
  3277. #endif
  3278. }
  3279. return ret;
  3280. }
  3281. #endif
  3282. #endif /* !CONFIG_ODD_RT_SIGACTION */
  3283. #ifdef CONFIG_OLD_SIGACTION
  3284. SYSCALL_DEFINE3(sigaction, int, sig,
  3285. const struct old_sigaction __user *, act,
  3286. struct old_sigaction __user *, oact)
  3287. {
  3288. struct k_sigaction new_ka, old_ka;
  3289. int ret;
  3290. if (act) {
  3291. old_sigset_t mask;
  3292. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  3293. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  3294. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  3295. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  3296. __get_user(mask, &act->sa_mask))
  3297. return -EFAULT;
  3298. #ifdef __ARCH_HAS_KA_RESTORER
  3299. new_ka.ka_restorer = NULL;
  3300. #endif
  3301. siginitset(&new_ka.sa.sa_mask, mask);
  3302. }
  3303. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3304. if (!ret && oact) {
  3305. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  3306. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  3307. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  3308. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  3309. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  3310. return -EFAULT;
  3311. }
  3312. return ret;
  3313. }
  3314. #endif
  3315. #ifdef CONFIG_COMPAT_OLD_SIGACTION
  3316. COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
  3317. const struct compat_old_sigaction __user *, act,
  3318. struct compat_old_sigaction __user *, oact)
  3319. {
  3320. struct k_sigaction new_ka, old_ka;
  3321. int ret;
  3322. compat_old_sigset_t mask;
  3323. compat_uptr_t handler, restorer;
  3324. if (act) {
  3325. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  3326. __get_user(handler, &act->sa_handler) ||
  3327. __get_user(restorer, &act->sa_restorer) ||
  3328. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  3329. __get_user(mask, &act->sa_mask))
  3330. return -EFAULT;
  3331. #ifdef __ARCH_HAS_KA_RESTORER
  3332. new_ka.ka_restorer = NULL;
  3333. #endif
  3334. new_ka.sa.sa_handler = compat_ptr(handler);
  3335. new_ka.sa.sa_restorer = compat_ptr(restorer);
  3336. siginitset(&new_ka.sa.sa_mask, mask);
  3337. }
  3338. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  3339. if (!ret && oact) {
  3340. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  3341. __put_user(ptr_to_compat(old_ka.sa.sa_handler),
  3342. &oact->sa_handler) ||
  3343. __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  3344. &oact->sa_restorer) ||
  3345. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  3346. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  3347. return -EFAULT;
  3348. }
  3349. return ret;
  3350. }
  3351. #endif
  3352. #ifdef CONFIG_SGETMASK_SYSCALL
  3353. /*
  3354. * For backwards compatibility. Functionality superseded by sigprocmask.
  3355. */
  3356. SYSCALL_DEFINE0(sgetmask)
  3357. {
  3358. /* SMP safe */
  3359. return current->blocked.sig[0];
  3360. }
  3361. SYSCALL_DEFINE1(ssetmask, int, newmask)
  3362. {
  3363. int old = current->blocked.sig[0];
  3364. sigset_t newset;
  3365. siginitset(&newset, newmask);
  3366. set_current_blocked(&newset);
  3367. return old;
  3368. }
  3369. #endif /* CONFIG_SGETMASK_SYSCALL */
  3370. #ifdef __ARCH_WANT_SYS_SIGNAL
  3371. /*
  3372. * For backwards compatibility. Functionality superseded by sigaction.
  3373. */
  3374. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  3375. {
  3376. struct k_sigaction new_sa, old_sa;
  3377. int ret;
  3378. new_sa.sa.sa_handler = handler;
  3379. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  3380. sigemptyset(&new_sa.sa.sa_mask);
  3381. ret = do_sigaction(sig, &new_sa, &old_sa);
  3382. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  3383. }
  3384. #endif /* __ARCH_WANT_SYS_SIGNAL */
  3385. #ifdef __ARCH_WANT_SYS_PAUSE
  3386. SYSCALL_DEFINE0(pause)
  3387. {
  3388. while (!signal_pending(current)) {
  3389. __set_current_state(TASK_INTERRUPTIBLE);
  3390. schedule();
  3391. }
  3392. return -ERESTARTNOHAND;
  3393. }
  3394. #endif
  3395. static int sigsuspend(sigset_t *set)
  3396. {
  3397. current->saved_sigmask = current->blocked;
  3398. set_current_blocked(set);
  3399. while (!signal_pending(current)) {
  3400. __set_current_state(TASK_INTERRUPTIBLE);
  3401. schedule();
  3402. }
  3403. set_restore_sigmask();
  3404. return -ERESTARTNOHAND;
  3405. }
  3406. /**
  3407. * sys_rt_sigsuspend - replace the signal mask for a value with the
  3408. * @unewset value until a signal is received
  3409. * @unewset: new signal mask value
  3410. * @sigsetsize: size of sigset_t type
  3411. */
  3412. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  3413. {
  3414. sigset_t newset;
  3415. /* XXX: Don't preclude handling different sized sigset_t's. */
  3416. if (sigsetsize != sizeof(sigset_t))
  3417. return -EINVAL;
  3418. if (copy_from_user(&newset, unewset, sizeof(newset)))
  3419. return -EFAULT;
  3420. return sigsuspend(&newset);
  3421. }
  3422. #ifdef CONFIG_COMPAT
  3423. COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
  3424. {
  3425. sigset_t newset;
  3426. /* XXX: Don't preclude handling different sized sigset_t's. */
  3427. if (sigsetsize != sizeof(sigset_t))
  3428. return -EINVAL;
  3429. if (get_compat_sigset(&newset, unewset))
  3430. return -EFAULT;
  3431. return sigsuspend(&newset);
  3432. }
  3433. #endif
  3434. #ifdef CONFIG_OLD_SIGSUSPEND
  3435. SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
  3436. {
  3437. sigset_t blocked;
  3438. siginitset(&blocked, mask);
  3439. return sigsuspend(&blocked);
  3440. }
  3441. #endif
  3442. #ifdef CONFIG_OLD_SIGSUSPEND3
  3443. SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
  3444. {
  3445. sigset_t blocked;
  3446. siginitset(&blocked, mask);
  3447. return sigsuspend(&blocked);
  3448. }
  3449. #endif
  3450. __weak const char *arch_vma_name(struct vm_area_struct *vma)
  3451. {
  3452. return NULL;
  3453. }
  3454. void __init signals_init(void)
  3455. {
  3456. /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
  3457. BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
  3458. != offsetof(struct siginfo, _sifields._pad));
  3459. BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
  3460. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  3461. }
  3462. #ifdef CONFIG_KGDB_KDB
  3463. #include <linux/kdb.h>
  3464. /*
  3465. * kdb_send_sig - Allows kdb to send signals without exposing
  3466. * signal internals. This function checks if the required locks are
  3467. * available before calling the main signal code, to avoid kdb
  3468. * deadlocks.
  3469. */
  3470. void kdb_send_sig(struct task_struct *t, int sig)
  3471. {
  3472. static struct task_struct *kdb_prev_t;
  3473. int new_t, ret;
  3474. if (!spin_trylock(&t->sighand->siglock)) {
  3475. kdb_printf("Can't do kill command now.\n"
  3476. "The sigmask lock is held somewhere else in "
  3477. "kernel, try again later\n");
  3478. return;
  3479. }
  3480. new_t = kdb_prev_t != t;
  3481. kdb_prev_t = t;
  3482. if (t->state != TASK_RUNNING && new_t) {
  3483. spin_unlock(&t->sighand->siglock);
  3484. kdb_printf("Process is not RUNNING, sending a signal from "
  3485. "kdb risks deadlock\n"
  3486. "on the run queue locks. "
  3487. "The signal has _not_ been sent.\n"
  3488. "Reissue the kill command if you want to risk "
  3489. "the deadlock.\n");
  3490. return;
  3491. }
  3492. ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
  3493. spin_unlock(&t->sighand->siglock);
  3494. if (ret)
  3495. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  3496. sig, t->pid);
  3497. else
  3498. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  3499. }
  3500. #endif /* CONFIG_KGDB_KDB */