signal.c 79 KB

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