From nobody Fri Jun 17 19:39:16 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 27E3385C02B; Fri, 17 Jun 2022 19:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LPq9c50Cvz3R96; Fri, 17 Jun 2022 19:39:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=msi4tgt3yaw+C2II8kcso6x1KhjUIzX1FFnO2UC4N2k=; b=iR29m9X8Bt1zLJxmqt4W132vtHQLwj/B/mO1woTr98zTvVTUis5gF0MwuCxPpQPiiutKRm g3FcplluilYAsJ0mL68cZfRPXrhBm68s2e69EHkFDlqAICkxY7OwcO+39xfr9vSGp+vAEM hfdTs2xPdywYgB2mn/yVUjYOwOkSqwxTP8ji7mxaLxzjFrWxzldgTZ7HkekUVACcEDtlFs 5kcEqm6W2PSAgT9Mn1O6J+8WO1TRF/b77r90DRsUP1Vx3hqwyaOtec8ITPMySsXi0MQ2rj rt8ilVhjLmRB+uIbrt77uhdUJsUb1kvu/s8NPRNqV0BGeVb6TlYisKVyc78KIw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 49D56257DF; Fri, 17 Jun 2022 19:39:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25HJdG3B015781; Fri, 17 Jun 2022 19:39:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25HJdGCb015780; Fri, 17 Jun 2022 19:39:16 GMT (envelope-from git) Date: Fri, 17 Jun 2022 19:39:16 GMT Message-Id: <202206171939.25HJdGCb015780@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 70569346989b - stable/13 - linux(4): Refactor signal send methods. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 70569346989bed9c3a69bce22c4d8588ed347a09 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=msi4tgt3yaw+C2II8kcso6x1KhjUIzX1FFnO2UC4N2k=; b=fg+B/u30HXPNXUGUj0aVmWCRXB8sNEKVEes2f6wzoKzh7TrQqlavygyPHPxb//XNBjKJ2r WRATGbiiC/Yw9OFW3xe6R4TueR/p6UeJ2s3yBT0qTPeSTEDU10ECu8m8nLBcH9fg5GYXM5 hpMy+4f5oWdvOBHAwoFp884K26izRA3z7/bWSdKTq434AQBeVyZElLaKViCI69UdutWHPA nbHgZo+3tgOEqWiGQ/VI6EPRh2YIacJhTfe0G0RWrOsReZiD5+zuDTeJo8Kv49pSGuz78O wox0BL2/xCzlaEcKvem6gO93+d9sBQpts5tUSrjyKwh8yAv03AfbBEExp8lJ8Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655494756; a=rsa-sha256; cv=none; b=pPoKV/vHV9S4htuMG59mGnrnmXTHRLvDsgve18sRdXfB9aTbLw+tCbfvEY3JxDJjvV9FGQ fkNeLfgHrcFaWSAVk+ZXWu3fwVjj2NWo5cmstnFebx1R61M2p2YiuEZHEJiDrvu6MBoAO+ OuurnGTZK90isn3X9fK8LvxAlLLloj3Br3d3/mVLB+x/+FZe21bFiM+i/m05aBBriNJsl0 f8BBU+FG/z17w+FO71Xu076dCuRDzbfsU5cMyYklAHwWZy1cQhc3hiv1Ov5Qb9YO54auaQ lOGdUVzSgExw0rkcYlrm+e6rgz5hOhavclM9g6AML+bhq4VORc4Z+LdXY2yUqQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=70569346989bed9c3a69bce22c4d8588ed347a09 commit 70569346989bed9c3a69bce22c4d8588ed347a09 Author: Dmitry Chagin AuthorDate: 2022-04-25 07:22:51 +0000 Commit: Dmitry Chagin CommitDate: 2022-06-17 19:33:55 +0000 linux(4): Refactor signal send methods. Created a couple of helpers to send signals to the specific thread or to the whole process. Use helpers in the corresponding syscalls. This fixes the confusion where a signal destined for a whole process was sent to a specific thread and vice versa. There is an exclusion for the linux_kill() syscall that takes a pid argument and should send a signal to the whole process, but I know at least one example where kill() takes tid. MFC after: 2 weeks (cherry picked from commit 6201a50d0d6d98633dba3db12dea2dfe95295e0e) --- sys/compat/linux/linux_signal.c | 170 +++++++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 72 deletions(-) diff --git a/sys/compat/linux/linux_signal.c b/sys/compat/linux/linux_signal.c index 786bcab2bf5e..c506edae0fc9 100644 --- a/sys/compat/linux/linux_signal.c +++ b/sys/compat/linux/linux_signal.c @@ -58,8 +58,13 @@ __FBSDID("$FreeBSD$"); #include #include -static int linux_do_tkill(struct thread *td, struct thread *tdt, +static int linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi); +static int linux_psignal(struct thread *td, int pid, int sig); +static int linux_tdksignal(struct thread *td, lwpid_t tid, + int tgid, int sig, ksiginfo_t *ksi); +static int linux_tdsignal(struct thread *td, lwpid_t tid, + int tgid, int sig); static void sicode_to_lsicode(int si_code, int *lsi_code); static int linux_common_rt_sigtimedwait(struct thread *, l_sigset_t *, struct timespec *, l_siginfo_t *, @@ -510,7 +515,7 @@ linux_rt_sigtimedwait_time64(struct thread *td, int linux_kill(struct thread *td, struct linux_kill_args *args) { - int l_signum; + int sig; /* * Allow signal 0 as a means to check for privileges @@ -519,40 +524,19 @@ linux_kill(struct thread *td, struct linux_kill_args *args) return (EINVAL); if (args->signum > 0) - l_signum = linux_to_bsd_signal(args->signum); + sig = linux_to_bsd_signal(args->signum); else - l_signum = 0; - - return (kern_kill(td, args->pid, l_signum)); -} - -static int -linux_do_tkill(struct thread *td, struct thread *tdt, ksiginfo_t *ksi) -{ - struct proc *p; - int error; - - p = tdt->td_proc; - AUDIT_ARG_SIGNUM(ksi->ksi_signo); - AUDIT_ARG_PID(p->p_pid); - AUDIT_ARG_PROCESS(p); - - error = p_cansignal(td, p, ksi->ksi_signo); - if (error != 0 || ksi->ksi_signo == 0) - goto out; - - tdksignal(tdt, ksi->ksi_signo, ksi); + sig = 0; -out: - PROC_UNLOCK(p); - return (error); + if (args->pid > PID_MAX) + return (linux_psignal(td, args->pid, sig)); + else + return (kern_kill(td, args->pid, sig)); } int linux_tgkill(struct thread *td, struct linux_tgkill_args *args) { - struct thread *tdt; - ksiginfo_t ksi; int sig; if (args->pid <= 0 || args->tgid <=0) @@ -569,17 +553,7 @@ linux_tgkill(struct thread *td, struct linux_tgkill_args *args) else sig = 0; - tdt = linux_tdfind(td, args->pid, args->tgid); - if (tdt == NULL) - return (ESRCH); - - ksiginfo_init(&ksi); - ksi.ksi_signo = sig; - ksi.ksi_code = SI_LWP; - ksi.ksi_errno = 0; - ksi.ksi_pid = td->td_proc->p_pid; - ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; - return (linux_do_tkill(td, tdt, &ksi)); + return (linux_tdsignal(td, args->pid, args->tgid, sig)); } /* @@ -588,8 +562,6 @@ linux_tgkill(struct thread *td, struct linux_tgkill_args *args) int linux_tkill(struct thread *td, struct linux_tkill_args *args) { - struct thread *tdt; - ksiginfo_t ksi; int sig; if (args->tid <= 0) @@ -600,17 +572,7 @@ linux_tkill(struct thread *td, struct linux_tkill_args *args) sig = linux_to_bsd_signal(args->sig); - tdt = linux_tdfind(td, args->tid, -1); - if (tdt == NULL) - return (ESRCH); - - ksiginfo_init(&ksi); - ksi.ksi_signo = sig; - ksi.ksi_code = SI_LWP; - ksi.ksi_errno = 0; - ksi.ksi_pid = td->td_proc->p_pid; - ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; - return (linux_do_tkill(td, tdt, &ksi)); + return (linux_tdsignal(td, args->tid, -1, sig)); } static void @@ -756,7 +718,6 @@ int linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args) { l_siginfo_t linfo; - struct proc *p; ksiginfo_t ksi; int error; int sig; @@ -778,25 +739,13 @@ linux_rt_sigqueueinfo(struct thread *td, struct linux_rt_sigqueueinfo_args *args if (error != 0) return (error); - error = ESRCH; - if ((p = pfind_any(args->pid)) != NULL) { - error = p_cansignal(td, p, sig); - if (error != 0) { - PROC_UNLOCK(p); - return (error); - } - error = tdsendsignal(p, NULL, sig, &ksi); - PROC_UNLOCK(p); - } - - return (error); + return (linux_pksignal(td, args->pid, sig, &ksi)); } int linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args *args) { l_siginfo_t linfo; - struct thread *tds; ksiginfo_t ksi; int error; int sig; @@ -817,11 +766,7 @@ linux_rt_tgsigqueueinfo(struct thread *td, struct linux_rt_tgsigqueueinfo_args * if (error != 0) return (error); - tds = linux_tdfind(td, args->tid, args->tgid); - if (tds == NULL) - return (ESRCH); - - return (linux_do_tkill(td, tds, &ksi)); + return (linux_tdksignal(td, args->tid, args->tgid, sig, &ksi)); } int @@ -841,3 +786,84 @@ linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) linux_to_bsd_sigset(&lmask, &sigmask); return (kern_sigsuspend(td, sigmask)); } + +static int +linux_tdksignal(struct thread *td, lwpid_t tid, int tgid, int sig, + ksiginfo_t *ksi) +{ + struct thread *tdt; + struct proc *p; + int error; + + tdt = linux_tdfind(td, tid, tgid); + if (tdt == NULL) + return (ESRCH); + + p = tdt->td_proc; + AUDIT_ARG_SIGNUM(sig); + AUDIT_ARG_PID(p->p_pid); + AUDIT_ARG_PROCESS(p); + + error = p_cansignal(td, p, sig); + if (error != 0 || sig == 0) + goto out; + + tdksignal(tdt, sig, ksi); + +out: + PROC_UNLOCK(p); + return (error); +} + +static int +linux_tdsignal(struct thread *td, lwpid_t tid, int tgid, int sig) +{ + ksiginfo_t ksi; + + ksiginfo_init(&ksi); + ksi.ksi_signo = sig; + ksi.ksi_code = SI_LWP; + ksi.ksi_pid = td->td_proc->p_pid; + ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; + return (linux_tdksignal(td, tid, tgid, sig, &ksi)); +} + +static int +linux_pksignal(struct thread *td, int pid, int sig, ksiginfo_t *ksi) +{ + struct thread *tdt; + struct proc *p; + int error; + + tdt = linux_tdfind(td, pid, -1); + if (tdt == NULL) + return (ESRCH); + + p = tdt->td_proc; + AUDIT_ARG_SIGNUM(sig); + AUDIT_ARG_PID(p->p_pid); + AUDIT_ARG_PROCESS(p); + + error = p_cansignal(td, p, sig); + if (error != 0 || sig == 0) + goto out; + + pksignal(p, sig, ksi); + +out: + PROC_UNLOCK(p); + return (error); +} + +static int +linux_psignal(struct thread *td, int pid, int sig) +{ + ksiginfo_t ksi; + + ksiginfo_init(&ksi); + ksi.ksi_signo = sig; + ksi.ksi_code = SI_LWP; + ksi.ksi_pid = td->td_proc->p_pid; + ksi.ksi_uid = td->td_proc->p_ucred->cr_ruid; + return (linux_pksignal(td, pid, sig, &ksi)); +}