signal.c 76 KB

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