From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from confino.investici.org (confino.investici.org [93.190.126.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 136ED42F6EB for ; Mon, 6 Jul 2026 12:40:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.190.126.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783341606; cv=none; b=Chk37a8ailUk6q4bSqOviKObKHHYOTMIdgBY0+aAL2uaXAcfoIPg7dkRUo6TerS88qAjGK9sf9cAFaoNZgvXVm2xdu36vAbeOoBHlBlZWJYXHz72DNLl5XT/yddNh3P9S+cZJRI6QMdyt8jQc72F2KlHgDJArbtmlE1FDn1Fz1c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783341606; c=relaxed/simple; bh=b4CgbFMgMgnul89cfSS2mr3dG63UV+J2Z2caGnkkLC8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JzbuAZ22fPHBIg5ZYTOoz44CZ2ER9QcH4xuTDBgW23c2gsDqYyNjiNVF5sD+ghZ1rB2YJlmHzdLvsY7pk0Yjd238KjEn3z0+GL5NVDpMgClQ6Du21IqfSSw7gkejGT36c9m26kJIWj5moLHHTNsJyYIEsCLBRqV/u+SAwFUf6bY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=grrlz.net; spf=pass smtp.mailfrom=grrlz.net; dkim=pass (1024-bit key) header.d=grrlz.net header.i=@grrlz.net header.b=u/bTffML; arc=none smtp.client-ip=93.190.126.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=grrlz.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=grrlz.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=grrlz.net header.i=@grrlz.net header.b="u/bTffML" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grrlz.net; s=stigmate; t=1783341603; bh=1ya5u7l0j8h+atNYxnCCCl7Lq5EQpX2b2RvsPFW66+U=; h=From:To:Cc:Subject:Date:From; b=u/bTffMLb4uSjhPgixszmxrQO17ThV95ozJO2L451urFxKTkDNuu1i/D+NpSJCUQo hkQIP7XPEA8YHxGl+6s0gtrP46qKNzctnUL1Vq26QeWEMt8kO+1fWlQ8YBM4LVg9au hgnSmlTn2KWzvpFRWQfxuQexwi7X3XiaRu6Ti9yk= Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4gv3pq3jXlz111N; Mon, 06 Jul 2026 12:40:03 +0000 (UTC) Received: by mx1.investici.org (Postfix) id 4gv3pp6f06z110p; Mon, 06 Jul 2026 12:40:02 +0000 (UTC) From: Bradley Morgan To: oleg@redhat.com, brauner@kernel.org Cc: akpm@linux-foundation.org, tglx@kernel.org, peterz@infradead.org, linux-kernel@vger.kernel.org, Bradley Morgan Subject: [PATCH] signal: add a helper for the si_code kernel impersonation check Date: Mon, 6 Jul 2026 12:40:03 +0000 Message-ID: <20260706124003.8000-1-include@grrlz.net> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The check that prevents userspace from sending siginfo with si_code values reserved to the kernel is duplicated across do_rt_sigqueueinfo(), do_rt_tgsigqueueinfo() and do_pidfd_send_signal(). Move the check into a helper so the rule lives in one place. Signed-off-by: Bradley Morgan --- kernel/signal.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/kernel/signal.c b/kernel/signal.c index 9c2b32c4d755..b603d9b75aa3 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -3943,6 +3943,16 @@ static void prepare_kill_siginfo(int sig, struct kernel_siginfo *info, info->si_uid = from_kuid_munged(current_user_ns(), current_uid()); } +/* + * si_code values >= 0 and SI_TKILL are reserved to the kernel. Not even + * root can use them to pretend a signal was sent by the kernel or by + * kill()/tgkill(), except when signaling itself. + */ +static bool si_code_reserved_to_kernel(int si_code) +{ + return si_code >= 0 || si_code == SI_TKILL; +} + /** * sys_kill - send a signal to a process * @pid: the PID of the process @@ -4038,7 +4048,7 @@ static int do_pidfd_send_signal(struct pid *pid, int sig, enum pid_type type, /* Only allow sending arbitrary signals to yourself. */ if ((task_pid(current) != pid || type > PIDTYPE_TGID) && - (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL)) + si_code_reserved_to_kernel(kinfo.si_code)) return -EPERM; } else { prepare_kill_siginfo(sig, &kinfo, type); @@ -4193,11 +4203,8 @@ SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig) static int do_rt_sigqueueinfo(pid_t pid, int sig, kernel_siginfo_t *info) { - /* Not even root can pretend to send signals from the kernel. - * Nor can they impersonate a kill()/tgkill(), which adds source info. - */ - if ((info->si_code >= 0 || info->si_code == SI_TKILL) && - (task_pid_vnr(current) != pid)) + if (si_code_reserved_to_kernel(info->si_code) && + task_pid_vnr(current) != pid) return -EPERM; /* POSIX.1b doesn't mention process groups. */ @@ -4240,11 +4247,8 @@ static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, kernel_siginfo_t if (pid <= 0 || tgid <= 0) return -EINVAL; - /* Not even root can pretend to send signals from the kernel. - * Nor can they impersonate a kill()/tgkill(), which adds source info. - */ - if ((info->si_code >= 0 || info->si_code == SI_TKILL) && - (task_pid_vnr(current) != pid)) + if (si_code_reserved_to_kernel(info->si_code) && + task_pid_vnr(current) != pid) return -EPERM; return do_send_specific(tgid, pid, sig, info); -- 2.53.0