signal.c 120 KB

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