signal.c 72 KB

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