signal.c 86 KB

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