signal.c 86 KB

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