signal.c 103 KB

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