signal.c 86 KB

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