signal.c 105 KB

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