signal.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  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. rcu_read_lock();
  1032. for (;;) {
  1033. sighand = rcu_dereference(tsk->sighand);
  1034. if (unlikely(sighand == NULL))
  1035. break;
  1036. spin_lock_irqsave(&sighand->siglock, *flags);
  1037. if (likely(sighand == tsk->sighand))
  1038. break;
  1039. spin_unlock_irqrestore(&sighand->siglock, *flags);
  1040. }
  1041. rcu_read_unlock();
  1042. return sighand;
  1043. }
  1044. /*
  1045. * send signal info to all the members of a group
  1046. */
  1047. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1048. {
  1049. int ret;
  1050. rcu_read_lock();
  1051. ret = check_kill_permission(sig, info, p);
  1052. rcu_read_unlock();
  1053. if (!ret && sig)
  1054. ret = do_send_sig_info(sig, info, p, true);
  1055. return ret;
  1056. }
  1057. /*
  1058. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1059. * control characters do (^C, ^Z etc)
  1060. * - the caller must hold at least a readlock on tasklist_lock
  1061. */
  1062. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1063. {
  1064. struct task_struct *p = NULL;
  1065. int retval, success;
  1066. success = 0;
  1067. retval = -ESRCH;
  1068. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1069. int err = group_send_sig_info(sig, info, p);
  1070. success |= !err;
  1071. retval = err;
  1072. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1073. return success ? 0 : retval;
  1074. }
  1075. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1076. {
  1077. int error = -ESRCH;
  1078. struct task_struct *p;
  1079. rcu_read_lock();
  1080. retry:
  1081. p = pid_task(pid, PIDTYPE_PID);
  1082. if (p) {
  1083. error = group_send_sig_info(sig, info, p);
  1084. if (unlikely(error == -ESRCH))
  1085. /*
  1086. * The task was unhashed in between, try again.
  1087. * If it is dead, pid_task() will return NULL,
  1088. * if we race with de_thread() it will find the
  1089. * new leader.
  1090. */
  1091. goto retry;
  1092. }
  1093. rcu_read_unlock();
  1094. return error;
  1095. }
  1096. int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1097. {
  1098. int error;
  1099. rcu_read_lock();
  1100. error = kill_pid_info(sig, info, find_vpid(pid));
  1101. rcu_read_unlock();
  1102. return error;
  1103. }
  1104. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1105. int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
  1106. uid_t uid, uid_t euid, u32 secid)
  1107. {
  1108. int ret = -EINVAL;
  1109. struct task_struct *p;
  1110. const struct cred *pcred;
  1111. unsigned long flags;
  1112. if (!valid_signal(sig))
  1113. return ret;
  1114. rcu_read_lock();
  1115. p = pid_task(pid, PIDTYPE_PID);
  1116. if (!p) {
  1117. ret = -ESRCH;
  1118. goto out_unlock;
  1119. }
  1120. pcred = __task_cred(p);
  1121. if (si_fromuser(info) &&
  1122. euid != pcred->suid && euid != pcred->uid &&
  1123. uid != pcred->suid && uid != pcred->uid) {
  1124. ret = -EPERM;
  1125. goto out_unlock;
  1126. }
  1127. ret = security_task_kill(p, info, sig, secid);
  1128. if (ret)
  1129. goto out_unlock;
  1130. if (sig) {
  1131. if (lock_task_sighand(p, &flags)) {
  1132. ret = __send_signal(sig, info, p, 1, 0);
  1133. unlock_task_sighand(p, &flags);
  1134. } else
  1135. ret = -ESRCH;
  1136. }
  1137. out_unlock:
  1138. rcu_read_unlock();
  1139. return ret;
  1140. }
  1141. EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
  1142. /*
  1143. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1144. *
  1145. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1146. * is probably wrong. Should make it like BSD or SYSV.
  1147. */
  1148. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1149. {
  1150. int ret;
  1151. if (pid > 0) {
  1152. rcu_read_lock();
  1153. ret = kill_pid_info(sig, info, find_vpid(pid));
  1154. rcu_read_unlock();
  1155. return ret;
  1156. }
  1157. read_lock(&tasklist_lock);
  1158. if (pid != -1) {
  1159. ret = __kill_pgrp_info(sig, info,
  1160. pid ? find_vpid(-pid) : task_pgrp(current));
  1161. } else {
  1162. int retval = 0, count = 0;
  1163. struct task_struct * p;
  1164. for_each_process(p) {
  1165. if (task_pid_vnr(p) > 1 &&
  1166. !same_thread_group(p, current)) {
  1167. int err = group_send_sig_info(sig, info, p);
  1168. ++count;
  1169. if (err != -EPERM)
  1170. retval = err;
  1171. }
  1172. }
  1173. ret = count ? retval : -ESRCH;
  1174. }
  1175. read_unlock(&tasklist_lock);
  1176. return ret;
  1177. }
  1178. /*
  1179. * These are for backward compatibility with the rest of the kernel source.
  1180. */
  1181. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1182. {
  1183. /*
  1184. * Make sure legacy kernel users don't send in bad values
  1185. * (normal paths check this in check_kill_permission).
  1186. */
  1187. if (!valid_signal(sig))
  1188. return -EINVAL;
  1189. return do_send_sig_info(sig, info, p, false);
  1190. }
  1191. #define __si_special(priv) \
  1192. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1193. int
  1194. send_sig(int sig, struct task_struct *p, int priv)
  1195. {
  1196. return send_sig_info(sig, __si_special(priv), p);
  1197. }
  1198. void
  1199. force_sig(int sig, struct task_struct *p)
  1200. {
  1201. force_sig_info(sig, SEND_SIG_PRIV, p);
  1202. }
  1203. /*
  1204. * When things go south during signal handling, we
  1205. * will force a SIGSEGV. And if the signal that caused
  1206. * the problem was already a SIGSEGV, we'll want to
  1207. * make sure we don't even try to deliver the signal..
  1208. */
  1209. int
  1210. force_sigsegv(int sig, struct task_struct *p)
  1211. {
  1212. if (sig == SIGSEGV) {
  1213. unsigned long flags;
  1214. spin_lock_irqsave(&p->sighand->siglock, flags);
  1215. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1216. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1217. }
  1218. force_sig(SIGSEGV, p);
  1219. return 0;
  1220. }
  1221. int kill_pgrp(struct pid *pid, int sig, int priv)
  1222. {
  1223. int ret;
  1224. read_lock(&tasklist_lock);
  1225. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1226. read_unlock(&tasklist_lock);
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL(kill_pgrp);
  1230. int kill_pid(struct pid *pid, int sig, int priv)
  1231. {
  1232. return kill_pid_info(sig, __si_special(priv), pid);
  1233. }
  1234. EXPORT_SYMBOL(kill_pid);
  1235. /*
  1236. * These functions support sending signals using preallocated sigqueue
  1237. * structures. This is needed "because realtime applications cannot
  1238. * afford to lose notifications of asynchronous events, like timer
  1239. * expirations or I/O completions". In the case of POSIX Timers
  1240. * we allocate the sigqueue structure from the timer_create. If this
  1241. * allocation fails we are able to report the failure to the application
  1242. * with an EAGAIN error.
  1243. */
  1244. struct sigqueue *sigqueue_alloc(void)
  1245. {
  1246. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1247. if (q)
  1248. q->flags |= SIGQUEUE_PREALLOC;
  1249. return q;
  1250. }
  1251. void sigqueue_free(struct sigqueue *q)
  1252. {
  1253. unsigned long flags;
  1254. spinlock_t *lock = &current->sighand->siglock;
  1255. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1256. /*
  1257. * We must hold ->siglock while testing q->list
  1258. * to serialize with collect_signal() or with
  1259. * __exit_signal()->flush_sigqueue().
  1260. */
  1261. spin_lock_irqsave(lock, flags);
  1262. q->flags &= ~SIGQUEUE_PREALLOC;
  1263. /*
  1264. * If it is queued it will be freed when dequeued,
  1265. * like the "regular" sigqueue.
  1266. */
  1267. if (!list_empty(&q->list))
  1268. q = NULL;
  1269. spin_unlock_irqrestore(lock, flags);
  1270. if (q)
  1271. __sigqueue_free(q);
  1272. }
  1273. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  1274. {
  1275. int sig = q->info.si_signo;
  1276. struct sigpending *pending;
  1277. unsigned long flags;
  1278. int ret;
  1279. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1280. ret = -1;
  1281. if (!likely(lock_task_sighand(t, &flags)))
  1282. goto ret;
  1283. ret = 1; /* the signal is ignored */
  1284. if (!prepare_signal(sig, t, 0))
  1285. goto out;
  1286. ret = 0;
  1287. if (unlikely(!list_empty(&q->list))) {
  1288. /*
  1289. * If an SI_TIMER entry is already queue just increment
  1290. * the overrun count.
  1291. */
  1292. BUG_ON(q->info.si_code != SI_TIMER);
  1293. q->info.si_overrun++;
  1294. goto out;
  1295. }
  1296. q->info.si_overrun = 0;
  1297. signalfd_notify(t, sig);
  1298. pending = group ? &t->signal->shared_pending : &t->pending;
  1299. list_add_tail(&q->list, &pending->list);
  1300. sigaddset(&pending->signal, sig);
  1301. complete_signal(sig, t, group);
  1302. out:
  1303. unlock_task_sighand(t, &flags);
  1304. ret:
  1305. return ret;
  1306. }
  1307. /*
  1308. * Let a parent know about the death of a child.
  1309. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1310. *
  1311. * Returns -1 if our parent ignored us and so we've switched to
  1312. * self-reaping, or else @sig.
  1313. */
  1314. int do_notify_parent(struct task_struct *tsk, int sig)
  1315. {
  1316. struct siginfo info;
  1317. unsigned long flags;
  1318. struct sighand_struct *psig;
  1319. int ret = sig;
  1320. BUG_ON(sig == -1);
  1321. /* do_notify_parent_cldstop should have been called instead. */
  1322. BUG_ON(task_is_stopped_or_traced(tsk));
  1323. BUG_ON(!task_ptrace(tsk) &&
  1324. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1325. info.si_signo = sig;
  1326. info.si_errno = 0;
  1327. /*
  1328. * we are under tasklist_lock here so our parent is tied to
  1329. * us and cannot exit and release its namespace.
  1330. *
  1331. * the only it can is to switch its nsproxy with sys_unshare,
  1332. * bu uncharing pid namespaces is not allowed, so we'll always
  1333. * see relevant namespace
  1334. *
  1335. * write_lock() currently calls preempt_disable() which is the
  1336. * same as rcu_read_lock(), but according to Oleg, this is not
  1337. * correct to rely on this
  1338. */
  1339. rcu_read_lock();
  1340. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1341. info.si_uid = __task_cred(tsk)->uid;
  1342. rcu_read_unlock();
  1343. info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
  1344. tsk->signal->utime));
  1345. info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
  1346. tsk->signal->stime));
  1347. info.si_status = tsk->exit_code & 0x7f;
  1348. if (tsk->exit_code & 0x80)
  1349. info.si_code = CLD_DUMPED;
  1350. else if (tsk->exit_code & 0x7f)
  1351. info.si_code = CLD_KILLED;
  1352. else {
  1353. info.si_code = CLD_EXITED;
  1354. info.si_status = tsk->exit_code >> 8;
  1355. }
  1356. psig = tsk->parent->sighand;
  1357. spin_lock_irqsave(&psig->siglock, flags);
  1358. if (!task_ptrace(tsk) && sig == SIGCHLD &&
  1359. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1360. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1361. /*
  1362. * We are exiting and our parent doesn't care. POSIX.1
  1363. * defines special semantics for setting SIGCHLD to SIG_IGN
  1364. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1365. * automatically and not left for our parent's wait4 call.
  1366. * Rather than having the parent do it as a magic kind of
  1367. * signal handler, we just set this to tell do_exit that we
  1368. * can be cleaned up without becoming a zombie. Note that
  1369. * we still call __wake_up_parent in this case, because a
  1370. * blocked sys_wait4 might now return -ECHILD.
  1371. *
  1372. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1373. * is implementation-defined: we do (if you don't want
  1374. * it, just use SIG_IGN instead).
  1375. */
  1376. ret = tsk->exit_signal = -1;
  1377. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1378. sig = -1;
  1379. }
  1380. if (valid_signal(sig) && sig > 0)
  1381. __group_send_sig_info(sig, &info, tsk->parent);
  1382. __wake_up_parent(tsk, tsk->parent);
  1383. spin_unlock_irqrestore(&psig->siglock, flags);
  1384. return ret;
  1385. }
  1386. /**
  1387. * do_notify_parent_cldstop - notify parent of stopped/continued state change
  1388. * @tsk: task reporting the state change
  1389. * @for_ptracer: the notification is for ptracer
  1390. * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
  1391. *
  1392. * Notify @tsk's parent that the stopped/continued state has changed. If
  1393. * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
  1394. * If %true, @tsk reports to @tsk->parent which should be the ptracer.
  1395. *
  1396. * CONTEXT:
  1397. * Must be called with tasklist_lock at least read locked.
  1398. */
  1399. static void do_notify_parent_cldstop(struct task_struct *tsk,
  1400. bool for_ptracer, int why)
  1401. {
  1402. struct siginfo info;
  1403. unsigned long flags;
  1404. struct task_struct *parent;
  1405. struct sighand_struct *sighand;
  1406. if (for_ptracer) {
  1407. parent = tsk->parent;
  1408. } else {
  1409. tsk = tsk->group_leader;
  1410. parent = tsk->real_parent;
  1411. }
  1412. info.si_signo = SIGCHLD;
  1413. info.si_errno = 0;
  1414. /*
  1415. * see comment in do_notify_parent() about the following 4 lines
  1416. */
  1417. rcu_read_lock();
  1418. info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
  1419. info.si_uid = __task_cred(tsk)->uid;
  1420. rcu_read_unlock();
  1421. info.si_utime = cputime_to_clock_t(tsk->utime);
  1422. info.si_stime = cputime_to_clock_t(tsk->stime);
  1423. info.si_code = why;
  1424. switch (why) {
  1425. case CLD_CONTINUED:
  1426. info.si_status = SIGCONT;
  1427. break;
  1428. case CLD_STOPPED:
  1429. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1430. break;
  1431. case CLD_TRAPPED:
  1432. info.si_status = tsk->exit_code & 0x7f;
  1433. break;
  1434. default:
  1435. BUG();
  1436. }
  1437. sighand = parent->sighand;
  1438. spin_lock_irqsave(&sighand->siglock, flags);
  1439. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1440. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1441. __group_send_sig_info(SIGCHLD, &info, parent);
  1442. /*
  1443. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1444. */
  1445. __wake_up_parent(tsk, parent);
  1446. spin_unlock_irqrestore(&sighand->siglock, flags);
  1447. }
  1448. static inline int may_ptrace_stop(void)
  1449. {
  1450. if (!likely(task_ptrace(current)))
  1451. return 0;
  1452. /*
  1453. * Are we in the middle of do_coredump?
  1454. * If so and our tracer is also part of the coredump stopping
  1455. * is a deadlock situation, and pointless because our tracer
  1456. * is dead so don't allow us to stop.
  1457. * If SIGKILL was already sent before the caller unlocked
  1458. * ->siglock we must see ->core_state != NULL. Otherwise it
  1459. * is safe to enter schedule().
  1460. */
  1461. if (unlikely(current->mm->core_state) &&
  1462. unlikely(current->mm == current->parent->mm))
  1463. return 0;
  1464. return 1;
  1465. }
  1466. /*
  1467. * Return non-zero if there is a SIGKILL that should be waking us up.
  1468. * Called with the siglock held.
  1469. */
  1470. static int sigkill_pending(struct task_struct *tsk)
  1471. {
  1472. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1473. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1474. }
  1475. /*
  1476. * Test whether the target task of the usual cldstop notification - the
  1477. * real_parent of @child - is in the same group as the ptracer.
  1478. */
  1479. static bool real_parent_is_ptracer(struct task_struct *child)
  1480. {
  1481. return same_thread_group(child->parent, child->real_parent);
  1482. }
  1483. /*
  1484. * This must be called with current->sighand->siglock held.
  1485. *
  1486. * This should be the path for all ptrace stops.
  1487. * We always set current->last_siginfo while stopped here.
  1488. * That makes it a way to test a stopped process for
  1489. * being ptrace-stopped vs being job-control-stopped.
  1490. *
  1491. * If we actually decide not to stop at all because the tracer
  1492. * is gone, we keep current->exit_code unless clear_code.
  1493. */
  1494. static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  1495. __releases(&current->sighand->siglock)
  1496. __acquires(&current->sighand->siglock)
  1497. {
  1498. bool gstop_done = false;
  1499. if (arch_ptrace_stop_needed(exit_code, info)) {
  1500. /*
  1501. * The arch code has something special to do before a
  1502. * ptrace stop. This is allowed to block, e.g. for faults
  1503. * on user stack pages. We can't keep the siglock while
  1504. * calling arch_ptrace_stop, so we must release it now.
  1505. * To preserve proper semantics, we must do this before
  1506. * any signal bookkeeping like checking group_stop_count.
  1507. * Meanwhile, a SIGKILL could come in before we retake the
  1508. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1509. * So after regaining the lock, we must check for SIGKILL.
  1510. */
  1511. spin_unlock_irq(&current->sighand->siglock);
  1512. arch_ptrace_stop(exit_code, info);
  1513. spin_lock_irq(&current->sighand->siglock);
  1514. if (sigkill_pending(current))
  1515. return;
  1516. }
  1517. /*
  1518. * If @why is CLD_STOPPED, we're trapping to participate in a group
  1519. * stop. Do the bookkeeping. Note that if SIGCONT was delievered
  1520. * while siglock was released for the arch hook, PENDING could be
  1521. * clear now. We act as if SIGCONT is received after TASK_TRACED
  1522. * is entered - ignore it.
  1523. */
  1524. if (why == CLD_STOPPED && (current->group_stop & GROUP_STOP_PENDING))
  1525. gstop_done = task_participate_group_stop(current);
  1526. current->last_siginfo = info;
  1527. current->exit_code = exit_code;
  1528. /*
  1529. * TRACED should be visible before TRAPPING is cleared; otherwise,
  1530. * the tracer might fail do_wait().
  1531. */
  1532. set_current_state(TASK_TRACED);
  1533. /*
  1534. * We're committing to trapping. Clearing GROUP_STOP_TRAPPING and
  1535. * transition to TASK_TRACED should be atomic with respect to
  1536. * siglock. This hsould be done after the arch hook as siglock is
  1537. * released and regrabbed across it.
  1538. */
  1539. task_clear_group_stop_trapping(current);
  1540. spin_unlock_irq(&current->sighand->siglock);
  1541. read_lock(&tasklist_lock);
  1542. if (may_ptrace_stop()) {
  1543. /*
  1544. * Notify parents of the stop.
  1545. *
  1546. * While ptraced, there are two parents - the ptracer and
  1547. * the real_parent of the group_leader. The ptracer should
  1548. * know about every stop while the real parent is only
  1549. * interested in the completion of group stop. The states
  1550. * for the two don't interact with each other. Notify
  1551. * separately unless they're gonna be duplicates.
  1552. */
  1553. do_notify_parent_cldstop(current, true, why);
  1554. if (gstop_done && !real_parent_is_ptracer(current))
  1555. do_notify_parent_cldstop(current, false, why);
  1556. /*
  1557. * Don't want to allow preemption here, because
  1558. * sys_ptrace() needs this task to be inactive.
  1559. *
  1560. * XXX: implement read_unlock_no_resched().
  1561. */
  1562. preempt_disable();
  1563. read_unlock(&tasklist_lock);
  1564. preempt_enable_no_resched();
  1565. schedule();
  1566. } else {
  1567. /*
  1568. * By the time we got the lock, our tracer went away.
  1569. * Don't drop the lock yet, another tracer may come.
  1570. *
  1571. * If @gstop_done, the ptracer went away between group stop
  1572. * completion and here. During detach, it would have set
  1573. * GROUP_STOP_PENDING on us and we'll re-enter TASK_STOPPED
  1574. * in do_signal_stop() on return, so notifying the real
  1575. * parent of the group stop completion is enough.
  1576. */
  1577. if (gstop_done)
  1578. do_notify_parent_cldstop(current, false, why);
  1579. __set_current_state(TASK_RUNNING);
  1580. if (clear_code)
  1581. current->exit_code = 0;
  1582. read_unlock(&tasklist_lock);
  1583. }
  1584. /*
  1585. * While in TASK_TRACED, we were considered "frozen enough".
  1586. * Now that we woke up, it's crucial if we're supposed to be
  1587. * frozen that we freeze now before running anything substantial.
  1588. */
  1589. try_to_freeze();
  1590. /*
  1591. * We are back. Now reacquire the siglock before touching
  1592. * last_siginfo, so that we are sure to have synchronized with
  1593. * any signal-sending on another CPU that wants to examine it.
  1594. */
  1595. spin_lock_irq(&current->sighand->siglock);
  1596. current->last_siginfo = NULL;
  1597. /*
  1598. * Queued signals ignored us while we were stopped for tracing.
  1599. * So check for any that we should take before resuming user mode.
  1600. * This sets TIF_SIGPENDING, but never clears it.
  1601. */
  1602. recalc_sigpending_tsk(current);
  1603. }
  1604. void ptrace_notify(int exit_code)
  1605. {
  1606. siginfo_t info;
  1607. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1608. memset(&info, 0, sizeof info);
  1609. info.si_signo = SIGTRAP;
  1610. info.si_code = exit_code;
  1611. info.si_pid = task_pid_vnr(current);
  1612. info.si_uid = current_uid();
  1613. /* Let the debugger run. */
  1614. spin_lock_irq(&current->sighand->siglock);
  1615. ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
  1616. spin_unlock_irq(&current->sighand->siglock);
  1617. }
  1618. /*
  1619. * This performs the stopping for SIGSTOP and other stop signals.
  1620. * We have to stop all threads in the thread group.
  1621. * Returns non-zero if we've actually stopped and released the siglock.
  1622. * Returns zero if we didn't stop and still hold the siglock.
  1623. */
  1624. static int do_signal_stop(int signr)
  1625. {
  1626. struct signal_struct *sig = current->signal;
  1627. if (!(current->group_stop & GROUP_STOP_PENDING)) {
  1628. unsigned int gstop = GROUP_STOP_PENDING | GROUP_STOP_CONSUME;
  1629. struct task_struct *t;
  1630. /* signr will be recorded in task->group_stop for retries */
  1631. WARN_ON_ONCE(signr & ~GROUP_STOP_SIGMASK);
  1632. if (!likely(current->group_stop & GROUP_STOP_DEQUEUED) ||
  1633. unlikely(signal_group_exit(sig)))
  1634. return 0;
  1635. /*
  1636. * There is no group stop already in progress. We must
  1637. * initiate one now.
  1638. *
  1639. * While ptraced, a task may be resumed while group stop is
  1640. * still in effect and then receive a stop signal and
  1641. * initiate another group stop. This deviates from the
  1642. * usual behavior as two consecutive stop signals can't
  1643. * cause two group stops when !ptraced. That is why we
  1644. * also check !task_is_stopped(t) below.
  1645. *
  1646. * The condition can be distinguished by testing whether
  1647. * SIGNAL_STOP_STOPPED is already set. Don't generate
  1648. * group_exit_code in such case.
  1649. *
  1650. * This is not necessary for SIGNAL_STOP_CONTINUED because
  1651. * an intervening stop signal is required to cause two
  1652. * continued events regardless of ptrace.
  1653. */
  1654. if (!(sig->flags & SIGNAL_STOP_STOPPED))
  1655. sig->group_exit_code = signr;
  1656. else
  1657. WARN_ON_ONCE(!task_ptrace(current));
  1658. current->group_stop &= ~GROUP_STOP_SIGMASK;
  1659. current->group_stop |= signr | gstop;
  1660. sig->group_stop_count = 1;
  1661. for (t = next_thread(current); t != current;
  1662. t = next_thread(t)) {
  1663. t->group_stop &= ~GROUP_STOP_SIGMASK;
  1664. /*
  1665. * Setting state to TASK_STOPPED for a group
  1666. * stop is always done with the siglock held,
  1667. * so this check has no races.
  1668. */
  1669. if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
  1670. t->group_stop |= signr | gstop;
  1671. sig->group_stop_count++;
  1672. signal_wake_up(t, 0);
  1673. }
  1674. }
  1675. }
  1676. retry:
  1677. if (likely(!task_ptrace(current))) {
  1678. int notify = 0;
  1679. /*
  1680. * If there are no other threads in the group, or if there
  1681. * is a group stop in progress and we are the last to stop,
  1682. * report to the parent.
  1683. */
  1684. if (task_participate_group_stop(current))
  1685. notify = CLD_STOPPED;
  1686. __set_current_state(TASK_STOPPED);
  1687. spin_unlock_irq(&current->sighand->siglock);
  1688. /*
  1689. * Notify the parent of the group stop completion. Because
  1690. * we're not holding either the siglock or tasklist_lock
  1691. * here, ptracer may attach inbetween; however, this is for
  1692. * group stop and should always be delivered to the real
  1693. * parent of the group leader. The new ptracer will get
  1694. * its notification when this task transitions into
  1695. * TASK_TRACED.
  1696. */
  1697. if (notify) {
  1698. read_lock(&tasklist_lock);
  1699. do_notify_parent_cldstop(current, false, notify);
  1700. read_unlock(&tasklist_lock);
  1701. }
  1702. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1703. schedule();
  1704. spin_lock_irq(&current->sighand->siglock);
  1705. } else {
  1706. ptrace_stop(current->group_stop & GROUP_STOP_SIGMASK,
  1707. CLD_STOPPED, 0, NULL);
  1708. current->exit_code = 0;
  1709. }
  1710. /*
  1711. * GROUP_STOP_PENDING could be set if another group stop has
  1712. * started since being woken up or ptrace wants us to transit
  1713. * between TASK_STOPPED and TRACED. Retry group stop.
  1714. */
  1715. if (current->group_stop & GROUP_STOP_PENDING) {
  1716. WARN_ON_ONCE(!(current->group_stop & GROUP_STOP_SIGMASK));
  1717. goto retry;
  1718. }
  1719. /* PTRACE_ATTACH might have raced with task killing, clear trapping */
  1720. task_clear_group_stop_trapping(current);
  1721. spin_unlock_irq(&current->sighand->siglock);
  1722. tracehook_finish_jctl();
  1723. return 1;
  1724. }
  1725. static int ptrace_signal(int signr, siginfo_t *info,
  1726. struct pt_regs *regs, void *cookie)
  1727. {
  1728. if (!task_ptrace(current))
  1729. return signr;
  1730. ptrace_signal_deliver(regs, cookie);
  1731. /* Let the debugger run. */
  1732. ptrace_stop(signr, CLD_TRAPPED, 0, info);
  1733. /* We're back. Did the debugger cancel the sig? */
  1734. signr = current->exit_code;
  1735. if (signr == 0)
  1736. return signr;
  1737. current->exit_code = 0;
  1738. /*
  1739. * Update the siginfo structure if the signal has
  1740. * changed. If the debugger wanted something
  1741. * specific in the siginfo structure then it should
  1742. * have updated *info via PTRACE_SETSIGINFO.
  1743. */
  1744. if (signr != info->si_signo) {
  1745. info->si_signo = signr;
  1746. info->si_errno = 0;
  1747. info->si_code = SI_USER;
  1748. info->si_pid = task_pid_vnr(current->parent);
  1749. info->si_uid = task_uid(current->parent);
  1750. }
  1751. /* If the (new) signal is now blocked, requeue it. */
  1752. if (sigismember(&current->blocked, signr)) {
  1753. specific_send_sig_info(signr, info, current);
  1754. signr = 0;
  1755. }
  1756. return signr;
  1757. }
  1758. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1759. struct pt_regs *regs, void *cookie)
  1760. {
  1761. struct sighand_struct *sighand = current->sighand;
  1762. struct signal_struct *signal = current->signal;
  1763. int signr;
  1764. relock:
  1765. /*
  1766. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1767. * While in TASK_STOPPED, we were considered "frozen enough".
  1768. * Now that we woke up, it's crucial if we're supposed to be
  1769. * frozen that we freeze now before running anything substantial.
  1770. */
  1771. try_to_freeze();
  1772. spin_lock_irq(&sighand->siglock);
  1773. /*
  1774. * Every stopped thread goes here after wakeup. Check to see if
  1775. * we should notify the parent, prepare_signal(SIGCONT) encodes
  1776. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  1777. */
  1778. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1779. struct task_struct *leader;
  1780. int why;
  1781. if (signal->flags & SIGNAL_CLD_CONTINUED)
  1782. why = CLD_CONTINUED;
  1783. else
  1784. why = CLD_STOPPED;
  1785. signal->flags &= ~SIGNAL_CLD_MASK;
  1786. spin_unlock_irq(&sighand->siglock);
  1787. /*
  1788. * Notify the parent that we're continuing. This event is
  1789. * always per-process and doesn't make whole lot of sense
  1790. * for ptracers, who shouldn't consume the state via
  1791. * wait(2) either, but, for backward compatibility, notify
  1792. * the ptracer of the group leader too unless it's gonna be
  1793. * a duplicate.
  1794. */
  1795. read_lock(&tasklist_lock);
  1796. do_notify_parent_cldstop(current, false, why);
  1797. leader = current->group_leader;
  1798. if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
  1799. do_notify_parent_cldstop(leader, true, why);
  1800. read_unlock(&tasklist_lock);
  1801. goto relock;
  1802. }
  1803. for (;;) {
  1804. struct k_sigaction *ka;
  1805. /*
  1806. * Tracing can induce an artificial signal and choose sigaction.
  1807. * The return value in @signr determines the default action,
  1808. * but @info->si_signo is the signal number we will report.
  1809. */
  1810. signr = tracehook_get_signal(current, regs, info, return_ka);
  1811. if (unlikely(signr < 0))
  1812. goto relock;
  1813. if (unlikely(signr != 0))
  1814. ka = return_ka;
  1815. else {
  1816. if (unlikely(current->group_stop &
  1817. GROUP_STOP_PENDING) && do_signal_stop(0))
  1818. goto relock;
  1819. signr = dequeue_signal(current, &current->blocked,
  1820. info);
  1821. if (!signr)
  1822. break; /* will return 0 */
  1823. if (signr != SIGKILL) {
  1824. signr = ptrace_signal(signr, info,
  1825. regs, cookie);
  1826. if (!signr)
  1827. continue;
  1828. }
  1829. ka = &sighand->action[signr-1];
  1830. }
  1831. /* Trace actually delivered signals. */
  1832. trace_signal_deliver(signr, info, ka);
  1833. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1834. continue;
  1835. if (ka->sa.sa_handler != SIG_DFL) {
  1836. /* Run the handler. */
  1837. *return_ka = *ka;
  1838. if (ka->sa.sa_flags & SA_ONESHOT)
  1839. ka->sa.sa_handler = SIG_DFL;
  1840. break; /* will return non-zero "signr" value */
  1841. }
  1842. /*
  1843. * Now we are doing the default action for this signal.
  1844. */
  1845. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1846. continue;
  1847. /*
  1848. * Global init gets no signals it doesn't want.
  1849. * Container-init gets no signals it doesn't want from same
  1850. * container.
  1851. *
  1852. * Note that if global/container-init sees a sig_kernel_only()
  1853. * signal here, the signal must have been generated internally
  1854. * or must have come from an ancestor namespace. In either
  1855. * case, the signal cannot be dropped.
  1856. */
  1857. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  1858. !sig_kernel_only(signr))
  1859. continue;
  1860. if (sig_kernel_stop(signr)) {
  1861. /*
  1862. * The default action is to stop all threads in
  1863. * the thread group. The job control signals
  1864. * do nothing in an orphaned pgrp, but SIGSTOP
  1865. * always works. Note that siglock needs to be
  1866. * dropped during the call to is_orphaned_pgrp()
  1867. * because of lock ordering with tasklist_lock.
  1868. * This allows an intervening SIGCONT to be posted.
  1869. * We need to check for that and bail out if necessary.
  1870. */
  1871. if (signr != SIGSTOP) {
  1872. spin_unlock_irq(&sighand->siglock);
  1873. /* signals can be posted during this window */
  1874. if (is_current_pgrp_orphaned())
  1875. goto relock;
  1876. spin_lock_irq(&sighand->siglock);
  1877. }
  1878. if (likely(do_signal_stop(info->si_signo))) {
  1879. /* It released the siglock. */
  1880. goto relock;
  1881. }
  1882. /*
  1883. * We didn't actually stop, due to a race
  1884. * with SIGCONT or something like that.
  1885. */
  1886. continue;
  1887. }
  1888. spin_unlock_irq(&sighand->siglock);
  1889. /*
  1890. * Anything else is fatal, maybe with a core dump.
  1891. */
  1892. current->flags |= PF_SIGNALED;
  1893. if (sig_kernel_coredump(signr)) {
  1894. if (print_fatal_signals)
  1895. print_fatal_signal(regs, info->si_signo);
  1896. /*
  1897. * If it was able to dump core, this kills all
  1898. * other threads in the group and synchronizes with
  1899. * their demise. If we lost the race with another
  1900. * thread getting here, it set group_exit_code
  1901. * first and our do_group_exit call below will use
  1902. * that value and ignore the one we pass it.
  1903. */
  1904. do_coredump(info->si_signo, info->si_signo, regs);
  1905. }
  1906. /*
  1907. * Death signals, no core dump.
  1908. */
  1909. do_group_exit(info->si_signo);
  1910. /* NOTREACHED */
  1911. }
  1912. spin_unlock_irq(&sighand->siglock);
  1913. return signr;
  1914. }
  1915. /*
  1916. * It could be that complete_signal() picked us to notify about the
  1917. * group-wide signal. Other threads should be notified now to take
  1918. * the shared signals in @which since we will not.
  1919. */
  1920. static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
  1921. {
  1922. sigset_t retarget;
  1923. struct task_struct *t;
  1924. sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
  1925. if (sigisemptyset(&retarget))
  1926. return;
  1927. t = tsk;
  1928. while_each_thread(tsk, t) {
  1929. if (t->flags & PF_EXITING)
  1930. continue;
  1931. if (!has_pending_signals(&retarget, &t->blocked))
  1932. continue;
  1933. /* Remove the signals this thread can handle. */
  1934. sigandsets(&retarget, &retarget, &t->blocked);
  1935. if (!signal_pending(t))
  1936. signal_wake_up(t, 0);
  1937. if (sigisemptyset(&retarget))
  1938. break;
  1939. }
  1940. }
  1941. void exit_signals(struct task_struct *tsk)
  1942. {
  1943. int group_stop = 0;
  1944. sigset_t unblocked;
  1945. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  1946. tsk->flags |= PF_EXITING;
  1947. return;
  1948. }
  1949. spin_lock_irq(&tsk->sighand->siglock);
  1950. /*
  1951. * From now this task is not visible for group-wide signals,
  1952. * see wants_signal(), do_signal_stop().
  1953. */
  1954. tsk->flags |= PF_EXITING;
  1955. if (!signal_pending(tsk))
  1956. goto out;
  1957. unblocked = tsk->blocked;
  1958. signotset(&unblocked);
  1959. retarget_shared_pending(tsk, &unblocked);
  1960. if (unlikely(tsk->group_stop & GROUP_STOP_PENDING) &&
  1961. task_participate_group_stop(tsk))
  1962. group_stop = CLD_STOPPED;
  1963. out:
  1964. spin_unlock_irq(&tsk->sighand->siglock);
  1965. /*
  1966. * If group stop has completed, deliver the notification. This
  1967. * should always go to the real parent of the group leader.
  1968. */
  1969. if (unlikely(group_stop)) {
  1970. read_lock(&tasklist_lock);
  1971. do_notify_parent_cldstop(tsk, false, group_stop);
  1972. read_unlock(&tasklist_lock);
  1973. }
  1974. }
  1975. EXPORT_SYMBOL(recalc_sigpending);
  1976. EXPORT_SYMBOL_GPL(dequeue_signal);
  1977. EXPORT_SYMBOL(flush_signals);
  1978. EXPORT_SYMBOL(force_sig);
  1979. EXPORT_SYMBOL(send_sig);
  1980. EXPORT_SYMBOL(send_sig_info);
  1981. EXPORT_SYMBOL(sigprocmask);
  1982. EXPORT_SYMBOL(block_all_signals);
  1983. EXPORT_SYMBOL(unblock_all_signals);
  1984. /*
  1985. * System call entry points.
  1986. */
  1987. /**
  1988. * sys_restart_syscall - restart a system call
  1989. */
  1990. SYSCALL_DEFINE0(restart_syscall)
  1991. {
  1992. struct restart_block *restart = &current_thread_info()->restart_block;
  1993. return restart->fn(restart);
  1994. }
  1995. long do_no_restart_syscall(struct restart_block *param)
  1996. {
  1997. return -EINTR;
  1998. }
  1999. static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
  2000. {
  2001. if (signal_pending(tsk) && !thread_group_empty(tsk)) {
  2002. sigset_t newblocked;
  2003. /* A set of now blocked but previously unblocked signals. */
  2004. sigandnsets(&newblocked, newset, &current->blocked);
  2005. retarget_shared_pending(tsk, &newblocked);
  2006. }
  2007. tsk->blocked = *newset;
  2008. recalc_sigpending();
  2009. }
  2010. /**
  2011. * set_current_blocked - change current->blocked mask
  2012. * @newset: new mask
  2013. *
  2014. * It is wrong to change ->blocked directly, this helper should be used
  2015. * to ensure the process can't miss a shared signal we are going to block.
  2016. */
  2017. void set_current_blocked(const sigset_t *newset)
  2018. {
  2019. struct task_struct *tsk = current;
  2020. spin_lock_irq(&tsk->sighand->siglock);
  2021. __set_task_blocked(tsk, newset);
  2022. spin_unlock_irq(&tsk->sighand->siglock);
  2023. }
  2024. /*
  2025. * This is also useful for kernel threads that want to temporarily
  2026. * (or permanently) block certain signals.
  2027. *
  2028. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  2029. * interface happily blocks "unblockable" signals like SIGKILL
  2030. * and friends.
  2031. */
  2032. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  2033. {
  2034. struct task_struct *tsk = current;
  2035. sigset_t newset;
  2036. /* Lockless, only current can change ->blocked, never from irq */
  2037. if (oldset)
  2038. *oldset = tsk->blocked;
  2039. switch (how) {
  2040. case SIG_BLOCK:
  2041. sigorsets(&newset, &tsk->blocked, set);
  2042. break;
  2043. case SIG_UNBLOCK:
  2044. sigandnsets(&newset, &tsk->blocked, set);
  2045. break;
  2046. case SIG_SETMASK:
  2047. newset = *set;
  2048. break;
  2049. default:
  2050. return -EINVAL;
  2051. }
  2052. set_current_blocked(&newset);
  2053. return 0;
  2054. }
  2055. /**
  2056. * sys_rt_sigprocmask - change the list of currently blocked signals
  2057. * @how: whether to add, remove, or set signals
  2058. * @set: stores pending signals
  2059. * @oset: previous value of signal mask if non-null
  2060. * @sigsetsize: size of sigset_t type
  2061. */
  2062. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
  2063. sigset_t __user *, oset, size_t, sigsetsize)
  2064. {
  2065. sigset_t old_set, new_set;
  2066. int error;
  2067. /* XXX: Don't preclude handling different sized sigset_t's. */
  2068. if (sigsetsize != sizeof(sigset_t))
  2069. return -EINVAL;
  2070. old_set = current->blocked;
  2071. if (nset) {
  2072. if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
  2073. return -EFAULT;
  2074. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2075. error = sigprocmask(how, &new_set, NULL);
  2076. if (error)
  2077. return error;
  2078. }
  2079. if (oset) {
  2080. if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
  2081. return -EFAULT;
  2082. }
  2083. return 0;
  2084. }
  2085. long do_sigpending(void __user *set, unsigned long sigsetsize)
  2086. {
  2087. long error = -EINVAL;
  2088. sigset_t pending;
  2089. if (sigsetsize > sizeof(sigset_t))
  2090. goto out;
  2091. spin_lock_irq(&current->sighand->siglock);
  2092. sigorsets(&pending, &current->pending.signal,
  2093. &current->signal->shared_pending.signal);
  2094. spin_unlock_irq(&current->sighand->siglock);
  2095. /* Outside the lock because only this thread touches it. */
  2096. sigandsets(&pending, &current->blocked, &pending);
  2097. error = -EFAULT;
  2098. if (!copy_to_user(set, &pending, sigsetsize))
  2099. error = 0;
  2100. out:
  2101. return error;
  2102. }
  2103. /**
  2104. * sys_rt_sigpending - examine a pending signal that has been raised
  2105. * while blocked
  2106. * @set: stores pending signals
  2107. * @sigsetsize: size of sigset_t type or larger
  2108. */
  2109. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
  2110. {
  2111. return do_sigpending(set, sigsetsize);
  2112. }
  2113. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  2114. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  2115. {
  2116. int err;
  2117. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  2118. return -EFAULT;
  2119. if (from->si_code < 0)
  2120. return __copy_to_user(to, from, sizeof(siginfo_t))
  2121. ? -EFAULT : 0;
  2122. /*
  2123. * If you change siginfo_t structure, please be sure
  2124. * this code is fixed accordingly.
  2125. * Please remember to update the signalfd_copyinfo() function
  2126. * inside fs/signalfd.c too, in case siginfo_t changes.
  2127. * It should never copy any pad contained in the structure
  2128. * to avoid security leaks, but must copy the generic
  2129. * 3 ints plus the relevant union member.
  2130. */
  2131. err = __put_user(from->si_signo, &to->si_signo);
  2132. err |= __put_user(from->si_errno, &to->si_errno);
  2133. err |= __put_user((short)from->si_code, &to->si_code);
  2134. switch (from->si_code & __SI_MASK) {
  2135. case __SI_KILL:
  2136. err |= __put_user(from->si_pid, &to->si_pid);
  2137. err |= __put_user(from->si_uid, &to->si_uid);
  2138. break;
  2139. case __SI_TIMER:
  2140. err |= __put_user(from->si_tid, &to->si_tid);
  2141. err |= __put_user(from->si_overrun, &to->si_overrun);
  2142. err |= __put_user(from->si_ptr, &to->si_ptr);
  2143. break;
  2144. case __SI_POLL:
  2145. err |= __put_user(from->si_band, &to->si_band);
  2146. err |= __put_user(from->si_fd, &to->si_fd);
  2147. break;
  2148. case __SI_FAULT:
  2149. err |= __put_user(from->si_addr, &to->si_addr);
  2150. #ifdef __ARCH_SI_TRAPNO
  2151. err |= __put_user(from->si_trapno, &to->si_trapno);
  2152. #endif
  2153. #ifdef BUS_MCEERR_AO
  2154. /*
  2155. * Other callers might not initialize the si_lsb field,
  2156. * so check explicitly for the right codes here.
  2157. */
  2158. if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
  2159. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  2160. #endif
  2161. break;
  2162. case __SI_CHLD:
  2163. err |= __put_user(from->si_pid, &to->si_pid);
  2164. err |= __put_user(from->si_uid, &to->si_uid);
  2165. err |= __put_user(from->si_status, &to->si_status);
  2166. err |= __put_user(from->si_utime, &to->si_utime);
  2167. err |= __put_user(from->si_stime, &to->si_stime);
  2168. break;
  2169. case __SI_RT: /* This is not generated by the kernel as of now. */
  2170. case __SI_MESGQ: /* But this is */
  2171. err |= __put_user(from->si_pid, &to->si_pid);
  2172. err |= __put_user(from->si_uid, &to->si_uid);
  2173. err |= __put_user(from->si_ptr, &to->si_ptr);
  2174. break;
  2175. default: /* this is just in case for now ... */
  2176. err |= __put_user(from->si_pid, &to->si_pid);
  2177. err |= __put_user(from->si_uid, &to->si_uid);
  2178. break;
  2179. }
  2180. return err;
  2181. }
  2182. #endif
  2183. /**
  2184. * do_sigtimedwait - wait for queued signals specified in @which
  2185. * @which: queued signals to wait for
  2186. * @info: if non-null, the signal's siginfo is returned here
  2187. * @ts: upper bound on process time suspension
  2188. */
  2189. int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
  2190. const struct timespec *ts)
  2191. {
  2192. struct task_struct *tsk = current;
  2193. long timeout = MAX_SCHEDULE_TIMEOUT;
  2194. sigset_t mask = *which;
  2195. int sig;
  2196. if (ts) {
  2197. if (!timespec_valid(ts))
  2198. return -EINVAL;
  2199. timeout = timespec_to_jiffies(ts);
  2200. /*
  2201. * We can be close to the next tick, add another one
  2202. * to ensure we will wait at least the time asked for.
  2203. */
  2204. if (ts->tv_sec || ts->tv_nsec)
  2205. timeout++;
  2206. }
  2207. /*
  2208. * Invert the set of allowed signals to get those we want to block.
  2209. */
  2210. sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2211. signotset(&mask);
  2212. spin_lock_irq(&tsk->sighand->siglock);
  2213. sig = dequeue_signal(tsk, &mask, info);
  2214. if (!sig && timeout) {
  2215. /*
  2216. * None ready, temporarily unblock those we're interested
  2217. * while we are sleeping in so that we'll be awakened when
  2218. * they arrive. Unblocking is always fine, we can avoid
  2219. * set_current_blocked().
  2220. */
  2221. tsk->real_blocked = tsk->blocked;
  2222. sigandsets(&tsk->blocked, &tsk->blocked, &mask);
  2223. recalc_sigpending();
  2224. spin_unlock_irq(&tsk->sighand->siglock);
  2225. timeout = schedule_timeout_interruptible(timeout);
  2226. spin_lock_irq(&tsk->sighand->siglock);
  2227. __set_task_blocked(tsk, &tsk->real_blocked);
  2228. siginitset(&tsk->real_blocked, 0);
  2229. sig = dequeue_signal(tsk, &mask, info);
  2230. }
  2231. spin_unlock_irq(&tsk->sighand->siglock);
  2232. if (sig)
  2233. return sig;
  2234. return timeout ? -EINTR : -EAGAIN;
  2235. }
  2236. /**
  2237. * sys_rt_sigtimedwait - synchronously wait for queued signals specified
  2238. * in @uthese
  2239. * @uthese: queued signals to wait for
  2240. * @uinfo: if non-null, the signal's siginfo is returned here
  2241. * @uts: upper bound on process time suspension
  2242. * @sigsetsize: size of sigset_t type
  2243. */
  2244. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  2245. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  2246. size_t, sigsetsize)
  2247. {
  2248. sigset_t these;
  2249. struct timespec ts;
  2250. siginfo_t info;
  2251. int ret;
  2252. /* XXX: Don't preclude handling different sized sigset_t's. */
  2253. if (sigsetsize != sizeof(sigset_t))
  2254. return -EINVAL;
  2255. if (copy_from_user(&these, uthese, sizeof(these)))
  2256. return -EFAULT;
  2257. if (uts) {
  2258. if (copy_from_user(&ts, uts, sizeof(ts)))
  2259. return -EFAULT;
  2260. }
  2261. ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
  2262. if (ret > 0 && uinfo) {
  2263. if (copy_siginfo_to_user(uinfo, &info))
  2264. ret = -EFAULT;
  2265. }
  2266. return ret;
  2267. }
  2268. /**
  2269. * sys_kill - send a signal to a process
  2270. * @pid: the PID of the process
  2271. * @sig: signal to be sent
  2272. */
  2273. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2274. {
  2275. struct siginfo info;
  2276. info.si_signo = sig;
  2277. info.si_errno = 0;
  2278. info.si_code = SI_USER;
  2279. info.si_pid = task_tgid_vnr(current);
  2280. info.si_uid = current_uid();
  2281. return kill_something_info(sig, &info, pid);
  2282. }
  2283. static int
  2284. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2285. {
  2286. struct task_struct *p;
  2287. int error = -ESRCH;
  2288. rcu_read_lock();
  2289. p = find_task_by_vpid(pid);
  2290. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2291. error = check_kill_permission(sig, info, p);
  2292. /*
  2293. * The null signal is a permissions and process existence
  2294. * probe. No signal is actually delivered.
  2295. */
  2296. if (!error && sig) {
  2297. error = do_send_sig_info(sig, info, p, false);
  2298. /*
  2299. * If lock_task_sighand() failed we pretend the task
  2300. * dies after receiving the signal. The window is tiny,
  2301. * and the signal is private anyway.
  2302. */
  2303. if (unlikely(error == -ESRCH))
  2304. error = 0;
  2305. }
  2306. }
  2307. rcu_read_unlock();
  2308. return error;
  2309. }
  2310. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2311. {
  2312. struct siginfo info;
  2313. info.si_signo = sig;
  2314. info.si_errno = 0;
  2315. info.si_code = SI_TKILL;
  2316. info.si_pid = task_tgid_vnr(current);
  2317. info.si_uid = current_uid();
  2318. return do_send_specific(tgid, pid, sig, &info);
  2319. }
  2320. /**
  2321. * sys_tgkill - send signal to one specific thread
  2322. * @tgid: the thread group ID of the thread
  2323. * @pid: the PID of the thread
  2324. * @sig: signal to be sent
  2325. *
  2326. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2327. * exists but it's not belonging to the target process anymore. This
  2328. * method solves the problem of threads exiting and PIDs getting reused.
  2329. */
  2330. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2331. {
  2332. /* This is only valid for single tasks */
  2333. if (pid <= 0 || tgid <= 0)
  2334. return -EINVAL;
  2335. return do_tkill(tgid, pid, sig);
  2336. }
  2337. /**
  2338. * sys_tkill - send signal to one specific task
  2339. * @pid: the PID of the task
  2340. * @sig: signal to be sent
  2341. *
  2342. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2343. */
  2344. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2345. {
  2346. /* This is only valid for single tasks */
  2347. if (pid <= 0)
  2348. return -EINVAL;
  2349. return do_tkill(0, pid, sig);
  2350. }
  2351. /**
  2352. * sys_rt_sigqueueinfo - send signal information to a signal
  2353. * @pid: the PID of the thread
  2354. * @sig: signal to be sent
  2355. * @uinfo: signal info to be sent
  2356. */
  2357. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2358. siginfo_t __user *, uinfo)
  2359. {
  2360. siginfo_t info;
  2361. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2362. return -EFAULT;
  2363. /* Not even root can pretend to send signals from the kernel.
  2364. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2365. */
  2366. if (info.si_code >= 0 || info.si_code == SI_TKILL) {
  2367. /* We used to allow any < 0 si_code */
  2368. WARN_ON_ONCE(info.si_code < 0);
  2369. return -EPERM;
  2370. }
  2371. info.si_signo = sig;
  2372. /* POSIX.1b doesn't mention process groups. */
  2373. return kill_proc_info(sig, &info, pid);
  2374. }
  2375. long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2376. {
  2377. /* This is only valid for single tasks */
  2378. if (pid <= 0 || tgid <= 0)
  2379. return -EINVAL;
  2380. /* Not even root can pretend to send signals from the kernel.
  2381. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2382. */
  2383. if (info->si_code >= 0 || info->si_code == SI_TKILL) {
  2384. /* We used to allow any < 0 si_code */
  2385. WARN_ON_ONCE(info->si_code < 0);
  2386. return -EPERM;
  2387. }
  2388. info->si_signo = sig;
  2389. return do_send_specific(tgid, pid, sig, info);
  2390. }
  2391. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2392. siginfo_t __user *, uinfo)
  2393. {
  2394. siginfo_t info;
  2395. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2396. return -EFAULT;
  2397. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2398. }
  2399. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2400. {
  2401. struct task_struct *t = current;
  2402. struct k_sigaction *k;
  2403. sigset_t mask;
  2404. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2405. return -EINVAL;
  2406. k = &t->sighand->action[sig-1];
  2407. spin_lock_irq(&current->sighand->siglock);
  2408. if (oact)
  2409. *oact = *k;
  2410. if (act) {
  2411. sigdelsetmask(&act->sa.sa_mask,
  2412. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2413. *k = *act;
  2414. /*
  2415. * POSIX 3.3.1.3:
  2416. * "Setting a signal action to SIG_IGN for a signal that is
  2417. * pending shall cause the pending signal to be discarded,
  2418. * whether or not it is blocked."
  2419. *
  2420. * "Setting a signal action to SIG_DFL for a signal that is
  2421. * pending and whose default action is to ignore the signal
  2422. * (for example, SIGCHLD), shall cause the pending signal to
  2423. * be discarded, whether or not it is blocked"
  2424. */
  2425. if (sig_handler_ignored(sig_handler(t, sig), sig)) {
  2426. sigemptyset(&mask);
  2427. sigaddset(&mask, sig);
  2428. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2429. do {
  2430. rm_from_queue_full(&mask, &t->pending);
  2431. t = next_thread(t);
  2432. } while (t != current);
  2433. }
  2434. }
  2435. spin_unlock_irq(&current->sighand->siglock);
  2436. return 0;
  2437. }
  2438. int
  2439. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2440. {
  2441. stack_t oss;
  2442. int error;
  2443. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2444. oss.ss_size = current->sas_ss_size;
  2445. oss.ss_flags = sas_ss_flags(sp);
  2446. if (uss) {
  2447. void __user *ss_sp;
  2448. size_t ss_size;
  2449. int ss_flags;
  2450. error = -EFAULT;
  2451. if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
  2452. goto out;
  2453. error = __get_user(ss_sp, &uss->ss_sp) |
  2454. __get_user(ss_flags, &uss->ss_flags) |
  2455. __get_user(ss_size, &uss->ss_size);
  2456. if (error)
  2457. goto out;
  2458. error = -EPERM;
  2459. if (on_sig_stack(sp))
  2460. goto out;
  2461. error = -EINVAL;
  2462. /*
  2463. * Note - this code used to test ss_flags incorrectly:
  2464. * old code may have been written using ss_flags==0
  2465. * to mean ss_flags==SS_ONSTACK (as this was the only
  2466. * way that worked) - this fix preserves that older
  2467. * mechanism.
  2468. */
  2469. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2470. goto out;
  2471. if (ss_flags == SS_DISABLE) {
  2472. ss_size = 0;
  2473. ss_sp = NULL;
  2474. } else {
  2475. error = -ENOMEM;
  2476. if (ss_size < MINSIGSTKSZ)
  2477. goto out;
  2478. }
  2479. current->sas_ss_sp = (unsigned long) ss_sp;
  2480. current->sas_ss_size = ss_size;
  2481. }
  2482. error = 0;
  2483. if (uoss) {
  2484. error = -EFAULT;
  2485. if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
  2486. goto out;
  2487. error = __put_user(oss.ss_sp, &uoss->ss_sp) |
  2488. __put_user(oss.ss_size, &uoss->ss_size) |
  2489. __put_user(oss.ss_flags, &uoss->ss_flags);
  2490. }
  2491. out:
  2492. return error;
  2493. }
  2494. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2495. /**
  2496. * sys_sigpending - examine pending signals
  2497. * @set: where mask of pending signal is returned
  2498. */
  2499. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
  2500. {
  2501. return do_sigpending(set, sizeof(*set));
  2502. }
  2503. #endif
  2504. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2505. /**
  2506. * sys_sigprocmask - examine and change blocked signals
  2507. * @how: whether to add, remove, or set signals
  2508. * @nset: signals to add or remove (if non-null)
  2509. * @oset: previous value of signal mask if non-null
  2510. *
  2511. * Some platforms have their own version with special arguments;
  2512. * others support only sys_rt_sigprocmask.
  2513. */
  2514. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
  2515. old_sigset_t __user *, oset)
  2516. {
  2517. old_sigset_t old_set, new_set;
  2518. sigset_t new_blocked;
  2519. old_set = current->blocked.sig[0];
  2520. if (nset) {
  2521. if (copy_from_user(&new_set, nset, sizeof(*nset)))
  2522. return -EFAULT;
  2523. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2524. new_blocked = current->blocked;
  2525. switch (how) {
  2526. case SIG_BLOCK:
  2527. sigaddsetmask(&new_blocked, new_set);
  2528. break;
  2529. case SIG_UNBLOCK:
  2530. sigdelsetmask(&new_blocked, new_set);
  2531. break;
  2532. case SIG_SETMASK:
  2533. new_blocked.sig[0] = new_set;
  2534. break;
  2535. default:
  2536. return -EINVAL;
  2537. }
  2538. set_current_blocked(&new_blocked);
  2539. }
  2540. if (oset) {
  2541. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2542. return -EFAULT;
  2543. }
  2544. return 0;
  2545. }
  2546. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2547. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2548. /**
  2549. * sys_rt_sigaction - alter an action taken by a process
  2550. * @sig: signal to be sent
  2551. * @act: new sigaction
  2552. * @oact: used to save the previous sigaction
  2553. * @sigsetsize: size of sigset_t type
  2554. */
  2555. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2556. const struct sigaction __user *, act,
  2557. struct sigaction __user *, oact,
  2558. size_t, sigsetsize)
  2559. {
  2560. struct k_sigaction new_sa, old_sa;
  2561. int ret = -EINVAL;
  2562. /* XXX: Don't preclude handling different sized sigset_t's. */
  2563. if (sigsetsize != sizeof(sigset_t))
  2564. goto out;
  2565. if (act) {
  2566. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2567. return -EFAULT;
  2568. }
  2569. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2570. if (!ret && oact) {
  2571. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2572. return -EFAULT;
  2573. }
  2574. out:
  2575. return ret;
  2576. }
  2577. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2578. #ifdef __ARCH_WANT_SYS_SGETMASK
  2579. /*
  2580. * For backwards compatibility. Functionality superseded by sigprocmask.
  2581. */
  2582. SYSCALL_DEFINE0(sgetmask)
  2583. {
  2584. /* SMP safe */
  2585. return current->blocked.sig[0];
  2586. }
  2587. SYSCALL_DEFINE1(ssetmask, int, newmask)
  2588. {
  2589. int old;
  2590. spin_lock_irq(&current->sighand->siglock);
  2591. old = current->blocked.sig[0];
  2592. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2593. sigmask(SIGSTOP)));
  2594. recalc_sigpending();
  2595. spin_unlock_irq(&current->sighand->siglock);
  2596. return old;
  2597. }
  2598. #endif /* __ARCH_WANT_SGETMASK */
  2599. #ifdef __ARCH_WANT_SYS_SIGNAL
  2600. /*
  2601. * For backwards compatibility. Functionality superseded by sigaction.
  2602. */
  2603. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  2604. {
  2605. struct k_sigaction new_sa, old_sa;
  2606. int ret;
  2607. new_sa.sa.sa_handler = handler;
  2608. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2609. sigemptyset(&new_sa.sa.sa_mask);
  2610. ret = do_sigaction(sig, &new_sa, &old_sa);
  2611. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2612. }
  2613. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2614. #ifdef __ARCH_WANT_SYS_PAUSE
  2615. SYSCALL_DEFINE0(pause)
  2616. {
  2617. while (!signal_pending(current)) {
  2618. current->state = TASK_INTERRUPTIBLE;
  2619. schedule();
  2620. }
  2621. return -ERESTARTNOHAND;
  2622. }
  2623. #endif
  2624. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2625. /**
  2626. * sys_rt_sigsuspend - replace the signal mask for a value with the
  2627. * @unewset value until a signal is received
  2628. * @unewset: new signal mask value
  2629. * @sigsetsize: size of sigset_t type
  2630. */
  2631. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  2632. {
  2633. sigset_t newset;
  2634. /* XXX: Don't preclude handling different sized sigset_t's. */
  2635. if (sigsetsize != sizeof(sigset_t))
  2636. return -EINVAL;
  2637. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2638. return -EFAULT;
  2639. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2640. spin_lock_irq(&current->sighand->siglock);
  2641. current->saved_sigmask = current->blocked;
  2642. current->blocked = newset;
  2643. recalc_sigpending();
  2644. spin_unlock_irq(&current->sighand->siglock);
  2645. current->state = TASK_INTERRUPTIBLE;
  2646. schedule();
  2647. set_restore_sigmask();
  2648. return -ERESTARTNOHAND;
  2649. }
  2650. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2651. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2652. {
  2653. return NULL;
  2654. }
  2655. void __init signals_init(void)
  2656. {
  2657. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2658. }
  2659. #ifdef CONFIG_KGDB_KDB
  2660. #include <linux/kdb.h>
  2661. /*
  2662. * kdb_send_sig_info - Allows kdb to send signals without exposing
  2663. * signal internals. This function checks if the required locks are
  2664. * available before calling the main signal code, to avoid kdb
  2665. * deadlocks.
  2666. */
  2667. void
  2668. kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
  2669. {
  2670. static struct task_struct *kdb_prev_t;
  2671. int sig, new_t;
  2672. if (!spin_trylock(&t->sighand->siglock)) {
  2673. kdb_printf("Can't do kill command now.\n"
  2674. "The sigmask lock is held somewhere else in "
  2675. "kernel, try again later\n");
  2676. return;
  2677. }
  2678. spin_unlock(&t->sighand->siglock);
  2679. new_t = kdb_prev_t != t;
  2680. kdb_prev_t = t;
  2681. if (t->state != TASK_RUNNING && new_t) {
  2682. kdb_printf("Process is not RUNNING, sending a signal from "
  2683. "kdb risks deadlock\n"
  2684. "on the run queue locks. "
  2685. "The signal has _not_ been sent.\n"
  2686. "Reissue the kill command if you want to risk "
  2687. "the deadlock.\n");
  2688. return;
  2689. }
  2690. sig = info->si_signo;
  2691. if (send_sig_info(sig, info, t))
  2692. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  2693. sig, t->pid);
  2694. else
  2695. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  2696. }
  2697. #endif /* CONFIG_KGDB_KDB */