signal.c 86 KB

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