signal.c 124 KB

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