signal.c 85 KB

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