signal.c 105 KB

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