signal.c 95 KB

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