From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 F0BA730E836 for ; Fri, 22 Aug 2025 14:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872549; cv=none; b=Qei0iky/uqKFc/CAOXXdF7DlbwWKwYSKWbJRQ+SBkqHvgeF3YTdm1Zn3kste4PBSwj7dxRyd8tYQPWqn7413C2VpZM9J3RWSh6pvfO0Lk2BxU//QX8C8eB56/U7BfYbhLkKfUhpbWAW5c353pi67zX8cq1vaWocBzfEmrv6QlI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872549; c=relaxed/simple; bh=ya+/2lywL7BGnBwGQ3T3zHs8dEkBfIpHOA+EH0QGW14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jJ9f3p7gZoENVWSpmb4fpDk1ya8JYoTze8eQaU/wBD7bEzVCsROJ4mCQcLZ/ryCgrPukvtTafykBNEbBEe8qKK7Y+YpnoZRsUcbotR1YILqeD4k9FO0zFxoLhUW40HcJL9RqEEJgrc7j8fv6YtNkn7XxSHf4X31iBhcUZUO0aRk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Fu3ZL2rL; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Fu3ZL2rL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872546; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7vplKdO7OueluSFmo2pcySTxoa/ZLQ49kKhsm4ft+S8=; b=Fu3ZL2rLbxQZnVft3xBOwGWQFcdU/nUuYGYpJmcj+LNa/WQGyTfwiAL6dZluhXzc+iZV/a Xh3T301T2+cghAl0kGPZKq+Gqd/JWe/Z9ELRmx66iSMnPGYE6VrMasmQ0bOu6qZK9V5xfY G3bkEfQPkqJyNPmKbzvPTFJtVa4Y6AU= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-590-25iV1S9KPaidp76cQknsiA-1; Fri, 22 Aug 2025 10:22:23 -0400 X-MC-Unique: 25iV1S9KPaidp76cQknsiA-1 X-Mimecast-MFC-AGG-ID: 25iV1S9KPaidp76cQknsiA_1755872542 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 764CD1800366; Fri, 22 Aug 2025 14:22:22 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 392F61800447; Fri, 22 Aug 2025 14:22:21 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/7] lib: Fix a couple of potential signed oveflows Date: Fri, 22 Aug 2025 15:22:08 +0100 Message-ID: <20250822142215.2475014-2-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Fix keyctl_read_alloc() to check for a potential unsigned overflow when we allocate a buffer with an extra byte added on the end for a NUL. Fix keyctl_dh_compute_alloc() for the same thing. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen --- keyutils.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/keyutils.c b/keyutils.c index 37b6cc3..fd02cda 100644 --- a/keyutils.c +++ b/keyutils.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "keyutils.h" =20 @@ -442,6 +443,8 @@ int keyctl_read_alloc(key_serial_t id, void **_buffer) return -1; =20 for (;;) { + if (ret =3D=3D LONG_MAX) + return -EFBIG; /* Don't let buflen+1 overflow. */ buflen =3D ret; buf =3D malloc(buflen + 1); if (!buf) @@ -515,6 +518,8 @@ int keyctl_dh_compute_alloc(key_serial_t priv, key_seri= al_t prime, if (ret < 0) return -1; =20 + if (ret =3D=3D LONG_MAX) + return -EFBIG; /* Don't let buflen+1 overflow. */ buflen =3D ret; buf =3D malloc(buflen + 1); if (!buf) From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 AB7A4310647 for ; Fri, 22 Aug 2025 14:22:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872553; cv=none; b=SsF0p+iVzYq+Sk38Kcwx4vPonMQQvGJlKoJMrD68f4eRiZPQ4+QMq/hswBOJIdz2CKi5itJ7YB+fQlDwH1G+o4nwZLwF3DkLnI39m/rZh/a7IHkSGKnCEHHBAT0yao9H5dxA4bqIWFdCzGcFsaM2abHL4EZQxvxdEwWc723508A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872553; c=relaxed/simple; bh=EwhQfNdwMRYnfyJhiDl/WI6c9SiGSsdsQ4W0UUcmEFQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e8VaPIQZUq5jf/fDoLfdenc2DjJBaVFd5aI0eFFE9YtC8odFXU5E9I1v7oHY4leFQxKQbE4cDnkKhDA+MbAfYsvTBaSysKwGc2L2lA4ImxGUcN4OcgownDYjbQ6dQ+9MVCQ2n5hXatyZUb6wj1IA2ODTSK4g4wzOULfZHrs+F80= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SmwAyjLq; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SmwAyjLq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OjevWaCAD9+4PGKq/fwSEtJ4889VG5wi2+iz89n84dc=; b=SmwAyjLqN5XekkC7Ub1k7A+Z25GeUzHMaljxlfCnqjPv+8bc0fxh2icPScku2yWJiW2VH4 9P9xpw2EEx4FM/sAIpQXry6bHr/Lrn7V67gni79QWVs15EENobUIirJ2XTYi9YtYRSBdSM F9Pmnrau7SLEUEDTwVZtoeADpH92tj8= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-191-Fnxh-02ROLuLaIZevzUWMg-1; Fri, 22 Aug 2025 10:22:26 -0400 X-MC-Unique: Fnxh-02ROLuLaIZevzUWMg-1 X-Mimecast-MFC-AGG-ID: Fnxh-02ROLuLaIZevzUWMg_1755872545 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AEB121956088; Fri, 22 Aug 2025 14:22:25 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E283730001A1; Fri, 22 Aug 2025 14:22:23 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] request-key: Fix mishandling of last line of config file Date: Fri, 22 Aug 2025 15:22:09 +0100 Message-ID: <20250822142215.2475014-3-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Fix mishandling of the config file by /sbin/request-key whereby the last line of a file, if it is lacking a newline, will trim off the last character and then try and use that. Return an error instead if we find a line without a newline char at the end. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen --- request-key.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/request-key.c b/request-key.c index bf47c0a..d1feec3 100644 --- a/request-key.c +++ b/request-key.c @@ -367,6 +367,8 @@ static void scan_conf_file(struct parameters *params, i= nt dirfd, const char *con /* ignore blank lines and comments */ if (len =3D=3D 1 || buf[0] =3D=3D '#' || isspace(buf[0])) continue; + if (len =3D=3D 0 || buf[len - 1] !=3D '\n') + line_error("Line missing newline\n"); =20 buf[--len] =3D 0; p =3D buf; From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 786123101DA for ; Fri, 22 Aug 2025 14:22:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872553; cv=none; b=nhEPWMnEjNIGJIek1DKccQYaVjQZTgeDRogqdfWQ5GkdwIgd0XNgjisFcn87R2eIPJJbr0uuVIIXlMabQ7SodJP5pDXTDMfDcDnS8+iy1+0KVjBmo11DGYB5eOKArXRQVpm+zepSggPurcBEnUZ7JMeVuA8E+NnCl/JPfDjk7c0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872553; c=relaxed/simple; bh=zCypTlaDHffLCDlbMYrzU8JmLZfE78I8zAGjTuZeDwE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N79o5K+EDkVdkGtulZsgHetSv4h9qbGOXwx3Bhk29yIKSlm9GhaKa9XZZy/JQUfMKeTkJ7wHw//rVm3xf8mqaBmosF5I9qvjWRVdjSNtBSxQsD3wKLhKqr7XjXuthRDxpwD3Cl8BTM+VbCBm0SW7w8FNV1TTivEB4+rCVgkDjm4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=BnSQVsH9; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BnSQVsH9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=egP0+NYa72c2/aBcMaaZPvBLQrtUqtpQbxPIy/YKSxY=; b=BnSQVsH9qJHWZ40CakEvYYPQwE7LoT5OsHIdharf0cJLN6g6dwVkOKzV0XkS5lR+5Maz3b zLX22Fi0JPcsivtQqizB6iztGkhGstQ8w0eQxpH5rDi12Zc2pkX01eIPd1yc2TKfGd2uDX ygF9CHHlonh9LMmbT4R1J/D04dF7Q/k= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-154-uoZDrJKnOwSqlAEAPK7JZA-1; Fri, 22 Aug 2025 10:22:29 -0400 X-MC-Unique: uoZDrJKnOwSqlAEAPK7JZA-1 X-Mimecast-MFC-AGG-ID: uoZDrJKnOwSqlAEAPK7JZA_1755872548 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 47447180045C; Fri, 22 Aug 2025 14:22:28 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 11B9430001A1; Fri, 22 Aug 2025 14:22:26 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/7] test: Hide endianness Date: Fri, 22 Aug 2025 15:22:10 +0100 Message-ID: <20250822142215.2475014-4-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Hide the endianness of the raw binary contents of a keyring by passing it through /usr/bin/od which will automatically byteswap it in 4-byte chunks rather than trying to detect the endianness by examining what may be a binary file to obtain a string the format of which changes over time. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen Link: https://lore.kernel.org/keyrings/2813085.1745332819@warthog.procyon.o= rg.uk/ --- tests/keyctl/reading/valid/runtest.sh | 9 ++----- tests/toolbox.inc.sh | 36 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/tests/keyctl/reading/valid/runtest.sh b/tests/keyctl/reading/v= alid/runtest.sh index 2fb88b9..2527f13 100644 --- a/tests/keyctl/reading/valid/runtest.sh +++ b/tests/keyctl/reading/valid/runtest.sh @@ -40,13 +40,8 @@ expect_payload payload "67697a7a 617264" =20 # read the contents of the keyring as hex and match it to the key ID marker "READ KEYRING" -read_key $keyringid -tmp=3D`printf %08x $keyid` -if [ "$endian" =3D "LE" ] -then - tmp=3D`echo $tmp | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/'` -fi -expect_payload payload $tmp +pipe_key_int32 $keyringid +expect_payload payload $keyid =20 # remove read permission from the key and try reading it again # - we should still have read permission because it's searchable in our diff --git a/tests/toolbox.inc.sh b/tests/toolbox.inc.sh index 609a6c7..6f4fb18 100644 --- a/tests/toolbox.inc.sh +++ b/tests/toolbox.inc.sh @@ -12,19 +12,6 @@ =20 echo =3D=3D=3D $OUTPUTFILE =3D=3D=3D =20 -endian=3D`file -L /proc/$$/exe` -if expr "$endian" : '.* MSB \+\(pie executable\|executable\|shared object\= ).*' >&/dev/null -then - endian=3DBE -elif expr "$endian" : '.* LSB \+\(pie executable\|executable\|shared objec= t\).*' >&/dev/null -then - endian=3DLE -else - echo -e "+++ \e[31;1mCan't Determine Endianness\e[0m" - echo "+++ Can't Determine Endianness" >>$OUTPUTFILE - exit 2 -fi - maxtypelen=3D31 maxtype=3D`for ((i=3D0; i<$((maxtypelen)); i++)); do echo -n a; done` =20 @@ -1055,6 +1042,29 @@ function pipe_key () fi } =20 +##########################################################################= ##### +# +# pipe a key's raw payload to od to stdout, displaying it as a sequence of +# 32-bit numbers, appropriately byteswapped. +# +##########################################################################= ##### +function pipe_key_int32 () +{ + my_exitval=3D0 + if [ "x$1" =3D "x--fail" ] + then + my_exitval=3D1 + shift + fi + + echo keyctl pipe $1 \| od -t u4 -A none >>$OUTPUTFILE + echo `keyctl pipe $1 | od -t u4 -A none` >>$OUTPUTFILE 2>&1 + if [ $? !=3D $my_exitval ] + then + failed + fi +} + ##########################################################################= ##### # # pipe a key's raw payload through md5sum From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 879C7312811 for ; Fri, 22 Aug 2025 14:22:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872556; cv=none; b=gR3sl7uSGJPf9xqFb1eeuoGJJD/H/b1AzDp664AGfQDICWekr2XdZSclaJBYpJAWQ8XOaKFQULam7xeU3Hp7hPnlgLLkSqPpJdpx0fHXuehyFdO2My3zspel6RbVsj1co9LocKduXt5q3HnaRUTL6qwhk4zj5wtnt8guSx16eAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872556; c=relaxed/simple; bh=TraykhXuNIeDPi+bm5l609BGmj55Eaa3Z/gNt9JbzHQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vh/kDOTmezs91RIehoHvKFWfxLpDsGp7gWjyfzZnmJLjZtMt3MqK96RspHajtOW59W+rJAJp1OLMRx7Oi2ZiylsL1hqdFDUuql7EvtTvM8kZ0lIB7m4rg7zsTUbJyVOX03AW3moTKtnAj2QKnHlHjHnN0XOgzJ+iKKsmjxczxc8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eEtmKXq9; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eEtmKXq9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872553; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ySRlbhyiwv/BltP0wsJJxLGif0rcRHsxShcb/MRRnzw=; b=eEtmKXq9sdwknJg+KEXsvt3l9n05maQiWV/OUYvdm+Cm0ihp1T7oedwou/EzRryAh6z3TW 8rFW9s9HbYcXA6oYuB0EtaHX9fAzBQnS2U1nJb7Br3fHPxbdfqdINewn0+/CGuZtY2tCHB k+UOnvSjovn/yVk9Mcqz2Ke/uzlZP1Y= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-613-9-FfmtpcN8CA_5njh_efFg-1; Fri, 22 Aug 2025 10:22:31 -0400 X-MC-Unique: 9-FfmtpcN8CA_5njh_efFg-1 X-Mimecast-MFC-AGG-ID: 9-FfmtpcN8CA_5njh_efFg_1755872550 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A57681956089; Fri, 22 Aug 2025 14:22:30 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 72F9718003FC; Fri, 22 Aug 2025 14:22:29 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/7] tests: Add skips for testing of unsupported features Date: Fri, 22 Aug 2025 15:22:11 +0100 Message-ID: <20250822142215.2475014-5-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add skips for features that are either unsupported by the kernel or by the keyutils package. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen Link: https://lore.kernel.org/keyrings/3089643.1745491480@warthog.procyon.o= rg.uk/ --- tests/features/limits/runtest.sh | 6 +++++ tests/hex2bin.pl | 21 +++++++++++++++ tests/keyctl/id/bad-args/runtest.sh | 6 +++++ tests/keyctl/id/noargs/runtest.sh | 6 +++++ tests/keyctl/id/valid/runtest.sh | 6 +++++ tests/keyctl/move/bad-args/runtest.sh | 6 +++++ tests/keyctl/move/noargs/runtest.sh | 6 +++++ tests/keyctl/move/recursion/runtest.sh | 6 +++++ tests/keyctl/move/valid/runtest.sh | 6 +++++ tests/keyctl/session/valid2/runtest.sh | 6 +++++ tests/keyctl/supports/bad-args/runtest.sh | 6 +++++ tests/keyctl/supports/valid/runtest.sh | 6 +++++ tests/prepare.inc.sh | 23 +++++++++++++++- tests/toolbox.inc.sh | 33 ++++++++++++++++++----- 14 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 tests/hex2bin.pl diff --git a/tests/features/limits/runtest.sh b/tests/features/limits/runte= st.sh index 3af2f5a..7642071 100644 --- a/tests/features/limits/runtest.sh +++ b/tests/features/limits/runtest.sh @@ -9,6 +9,12 @@ result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 +if ! keyutils_at_or_later_than 1.6.2 +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl --test'" + exit 0 +fi + # This doesn't work on MIPS earler than 3.19 because of a kernel bug kver=3D`uname -r` kmch=3D`uname -m` diff --git a/tests/hex2bin.pl b/tests/hex2bin.pl new file mode 100644 index 0000000..4f0f27a --- /dev/null +++ b/tests/hex2bin.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w +use strict; + +die "Format:\n\t$0 []*\n\t$0 -\n" unless (@ARGV); + +my $str =3D ""; + +if ($ARGV[0] eq "-") { + shift(@ARGV); + $str .=3D $_ while (); +} else { + $str =3D join("", @ARGV); +} + +$str =3D~ s/[ \t\n]//g; +die "odd length string\n" if (length($str) & 1); + +for (; $str; $str =3D substr($str, 2)) { + my $pair =3D hex(substr($str, 0, 2)); + print pack("C", $pair); +} diff --git a/tests/keyctl/id/bad-args/runtest.sh b/tests/keyctl/id/bad-args= /runtest.sh index 957d1a5..bba62c6 100644 --- a/tests/keyctl/id/bad-args/runtest.sh +++ b/tests/keyctl/id/bad-args/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_id_command =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl id'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/id/noargs/runtest.sh b/tests/keyctl/id/noargs/run= test.sh index aff9de6..b95c596 100644 --- a/tests/keyctl/id/noargs/runtest.sh +++ b/tests/keyctl/id/noargs/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_id_command =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl id'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/id/valid/runtest.sh b/tests/keyctl/id/valid/runte= st.sh index ffed995..2c06b3d 100644 --- a/tests/keyctl/id/valid/runtest.sh +++ b/tests/keyctl/id/valid/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_id_command =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl id'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/move/bad-args/runtest.sh b/tests/keyctl/move/bad-= args/runtest.sh index b1c7e66..9410941 100644 --- a/tests/keyctl/move/bad-args/runtest.sh +++ b/tests/keyctl/move/bad-args/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_move_key =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl move'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/move/noargs/runtest.sh b/tests/keyctl/move/noargs= /runtest.sh index 29a91f1..8ad91e9 100644 --- a/tests/keyctl/move/noargs/runtest.sh +++ b/tests/keyctl/move/noargs/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_move_key =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl move'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/move/recursion/runtest.sh b/tests/keyctl/move/rec= ursion/runtest.sh index 36cd5cb..8b90be8 100644 --- a/tests/keyctl/move/recursion/runtest.sh +++ b/tests/keyctl/move/recursion/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_move_key =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl move'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/move/valid/runtest.sh b/tests/keyctl/move/valid/r= untest.sh index 31b51d7..20ccff2 100644 --- a/tests/keyctl/move/valid/runtest.sh +++ b/tests/keyctl/move/valid/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_move_key =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl move'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/session/valid2/runtest.sh b/tests/keyctl/session/= valid2/runtest.sh index 12ad234..1642395 100644 --- a/tests/keyctl/session/valid2/runtest.sh +++ b/tests/keyctl/session/valid2/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_id_command =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl id'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/supports/bad-args/runtest.sh b/tests/keyctl/suppo= rts/bad-args/runtest.sh index 05581a4..f87f517 100644 --- a/tests/keyctl/supports/bad-args/runtest.sh +++ b/tests/keyctl/supports/bad-args/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_capabilities =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl supports'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/keyctl/supports/valid/runtest.sh b/tests/keyctl/supports= /valid/runtest.sh index 2c62ef2..4e41200 100644 --- a/tests/keyctl/supports/valid/runtest.sh +++ b/tests/keyctl/supports/valid/runtest.sh @@ -6,6 +6,12 @@ =20 # ---- do the actual testing ---- =20 +if [ $have_capabilities =3D 0 ] +then + toolbox_skip_test $TEST "SKIPPING DUE TO LACK OF 'keyctl supports'" + exit 0 +fi + result=3DPASS echo "++++ BEGINNING TEST" >$OUTPUTFILE =20 diff --git a/tests/prepare.inc.sh b/tests/prepare.inc.sh index 4033d69..be134da 100644 --- a/tests/prepare.inc.sh +++ b/tests/prepare.inc.sh @@ -112,11 +112,14 @@ then esac fi =20 +have_capabilities=3D0 have_key_invalidate=3D0 have_big_key_type=3D0 have_dh_compute=3D0 -have_restrict_keyring=3D0 +have_move_key=3D0 have_notify=3D0 +have_public_key=3D0 +have_restrict_keyring=3D0 =20 if keyctl supports capabilities >&/dev/null then @@ -179,3 +182,21 @@ if [ "$SKIPINSTALLREQ" =3D "yes" ] then skip_install_required=3D1 fi + +# +# Check if "keyctl id" is supported +# +have_id_command=3D0 +if keyutils_at_or_later_than 1.6.2 +then + have_id_command=3D1 +fi + +# +# Check if "keyctl pkey_*" are supported +# +have_pkey_commands=3D0 +if keyutils_at_or_later_than 1.6 +then + have_pkey_commands=3D1 +fi diff --git a/tests/toolbox.inc.sh b/tests/toolbox.inc.sh index 6f4fb18..212b353 100644 --- a/tests/toolbox.inc.sh +++ b/tests/toolbox.inc.sh @@ -613,8 +613,15 @@ function create_key () my_keyring=3D$4 fi =20 - echo keyctl add "$@" >>$OUTPUTFILE - keyctl add "$@" >>$OUTPUTFILE 2>&1 + if [ "$1" =3D "-x" ] && version_less_than $OSRELEASE 9 + then + shift + echo perl ../../../hex2bin.pl "$3" "|" keyctl padd "$1 $2 $4" >>$OUTPUTFI= LE + perl ../../../hex2bin.pl "$3" | keyctl padd "$1" "$2" "$4" >>$OUTPUTFILE + else + echo keyctl add "$@" >>$OUTPUTFILE + keyctl add "$@" >>$OUTPUTFILE 2>&1 + fi e=3D$? if [ $e =3D=3D $my_exitval ] then @@ -682,8 +689,15 @@ function pcreate_key () my_keyring=3D$3 fi =20 - echo echo -n $data \| keyctl padd "$@" >>$OUTPUTFILE - echo -n $data | keyctl padd "$@" >>$OUTPUTFILE 2>&1 + if [ "$1" =3D "-x" ] && version_less_than $OSRELEASE 9 + then + shift + echo echo -n $data \| perl ../../../hex2bin.pl "|" keyctl padd "$@" >>$OU= TPUTFILE + echo -n $data | perl ../../../hex2bin.pl - | keyctl padd "$@" >>$OUTPUTFI= LE + else + echo echo -n $data \| keyctl padd "$@" >>$OUTPUTFILE + echo -n $data | keyctl padd "$@" >>$OUTPUTFILE 2>&1 + fi e=3D$? if [ $e =3D=3D $my_exitval ] then @@ -1232,8 +1246,15 @@ function update_key () shift fi =20 - echo keyctl update "$@" >>$OUTPUTFILE - keyctl update "$@" >>$OUTPUTFILE 2>&1 + if [ "x$1" =3D "x-x" ] && version_less_than $OSRELEASE 9 + then + shift + echo perl ../../../hex2bin.pl "$2" "|" keyctl pupdate "$1" >>$OUTPUTFILE + perl ../../../hex2bin.pl "$2" | keyctl pupdate "$1" >>$OUTPUTFILE + else + echo keyctl update "$@" >>$OUTPUTFILE + keyctl update "$@" >>$OUTPUTFILE 2>&1 + fi e=3D$? if [ $e =3D=3D $my_exitval ] then From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 1CCD73128B1 for ; Fri, 22 Aug 2025 14:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872560; cv=none; b=gzU8WkuQDFcjEh4W6ps2CZEwLkr3gOKQZjRioGgK3cuOAjyxZD/pP7dKdYGHQRWRZrE3TW5iJwsbL/tmcLK27FkhkK3d4we253k2XeHQPDIoe/E2C+klkoghL2tuLJE8Gvg7Yh8WhNgka9jAQuRPRdBYslawl9/bXW7uNj9elRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872560; c=relaxed/simple; bh=8ZSK0v83UzgndVd+6j+Ob2zWxBO+LwO1vSvTVJ6AS7c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ep1bgq3n6H3mSVObRGqD90k2yf72PXifPXC/NUlAETfaXlCbU4ZyrfsNaUx48nko287O/qut+XrWGMCLiv1o15I5ayHYpSadOSme0s76yUuehHGz22f9bAhsKYGExMaK+6CP6L4TAlwGzkJanB3+ylm+aFptAgGcXiuOuLYMnGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dCcWrB+k; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dCcWrB+k" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872558; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Zg5th46DkHtvRDX7ITaK0hA9kgkdfvZehuTYviRd8Ck=; b=dCcWrB+khi7lICkIbOFVzsUZ0k/C5mH1CgqzR6tP2lpgskyrkZhWDU8yFv6j1rOVOeEKlX APPf01jFhxvak1wB2fpbAEbJiuZ+B0Eq8up6YkBUNmY1HCATMR1Ts4f23fKcxQ5tEM3YEy pLVvjvLEcng94orvetQW6Vm9NtXIxB8= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-35-3B29MiX5Nwub36e3U3GTUg-1; Fri, 22 Aug 2025 10:22:34 -0400 X-MC-Unique: 3B29MiX5Nwub36e3U3GTUg-1 X-Mimecast-MFC-AGG-ID: 3B29MiX5Nwub36e3U3GTUg_1755872553 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4AE4919560B4; Fri, 22 Aug 2025 14:22:33 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1457E19560B0; Fri, 22 Aug 2025 14:22:31 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/7] request-key: Add help text Date: Fri, 22 Aug 2025 15:22:12 +0100 Message-ID: <20250822142215.2475014-6-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add --help text to /sbin/request-key. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen --- man/request-key.8 | 42 +++++++++++++++++++++++++++------- request-key.c | 57 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 14 deletions(-) diff --git a/man/request-key.8 b/man/request-key.8 index 50a7506..15b6bb8 100644 --- a/man/request-key.8 +++ b/man/request-key.8 @@ -7,20 +7,37 @@ .\" as published by the Free Software Foundation; either version .\" 2 of the License, or (at your option) any later version. .\" -.TH REQUEST-KEY 8 "15 Nov 2011" Linux "Linux Key Management Utilities" +.TH REQUEST-KEY 8 "22 Aug 2025" Linux "Linux Key Management Utilities" .SH NAME request\-key \- handle key instantiation callback requests from the kernel .SH SYNOPSIS -\fB/sbin/request\-key \fR - [] +.nf +\fB/sbin/request\-key\fP [\fB--help\fP|\fB--version\fP] +\fB/sbin/request\-key\fP \fIop key uid gid t-ring p-ring s-ring\fP [\fIinf= o\fP] +\fB/sbin/request\-key -d [-lnv] -D\fP \fIdesc\fP \fIop key\fP... +.fi .SH DESCRIPTION This program is invoked by the kernel when the kernel is asked for a key t= hat it doesn't have immediately available. The kernel creates a partially set = up -key and then calls out to this program to instantiate it. It is not intend= ed -to be called directly. +key and then calls out to this program to instantiate it. It is not intend= ed to +be called directly. A debugging capability is available through command l= ine +options, however, to aid in testing and in debugging configuration. .PP -However, for debugging purposes, it can be given some options on the comma= nd -line: +The normally required parameters are: +.IP \fBop\fP +The operation being done, such as "create" if a key is being instantiated = for +creation. +.IP \fBkey\fP +The ID of the key being operated upon. +.IP "\fBuid\fP, \fBgid\fP" +The ownership of the task that caused the key to be created. +.IP "\fBt-ring\fP, \fBp-ring\fP, \fBs-ring\fP" +The thread, process and session keyrings of the task that caused the key t= o be +created. +.IP \fBinfo\fP +The optional callout info that can be passed by \fIrequest_key(2)\fP. +.PP +The options that may also be supplied are: .IP \fB-d\fP Turn on debugging mode. In this mode, no attempts are made to access any = keys and, if a handler program is selected, it won't be executed; instead, this @@ -39,10 +56,19 @@ will be copied to the system log - this will prevent th= at. .IP \fB-v\fP Turn on debugging output. This may be specified multiple times to produce increasing levels of verbosity. +.IP \fB--help\fP +Print help text and exit. .IP \fB--version\fP Print the program version and exit. +.SH EXAMPLES +When using the debugging mode, all the parameters must be given, though a = lot +of them don't matter and can be just set to 0, e.g.: +.PP +.nf +request-key -d -D "user;0;0;0;debug:bar" create 0 0 0 0 0 0 foo +.fi .SH ERRORS -All errors will be logged to the syslog. +All errors will be logged to the syslog unless the \fB-n\fP option is give= n. .SH FILES .ul /etc/request\-key.d/*.conf diff --git a/request-key.c b/request-key.c index d1feec3..9a7d741 100644 --- a/request-key.c +++ b/request-key.c @@ -111,7 +111,7 @@ static void error(const char *fmt, ...) { va_list va; =20 - if (verbosity) { + if (verbosity || debug_mode) { va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); @@ -138,6 +138,45 @@ static void oops(int x) error("Died on signal %d", x); } =20 +static const char help_text[] =3D + "Usage: request-key [OPTIONS] [PARAMS]\n" + " request-key [OPTIONS] -d -D [PARAMS]\n" + "\n" + "Where the required parameters, [PARAMS], are, in order:\n" + " : The operation type (e.g. 'create')\n" + " : The ID of the key to be operated upon\n" + " : The UID of the requesting process\n" + " : The GID of the requesting process\n" + " : The thread keyring of the requesting process (or 0)\n" + " : The process keyring of the requesting process (or 0)\n" + " : The session keyring of the requesting process (or 0)\n" + " : The callout data supplied to the request\n" + "\n" + "and [OPTIONS] are none or more of\n" + " -d : Debug mode for direct cmdline testing\n" + " -D : Description for debug mode\n" + " -l : Use config from local dir, not /etc\n" + " -n : Don't log to syslog\n" + " -v : Turn up verbosity (can use multiple times)\n" + " --version: Print version and exit\n" + " --help : Print this text and exit\n" + "\n" + "Service program lookup testing can be done with the '-d' option, but\n" + "as there is no actual key to query, the called must supply the key's\n" + "attributes manually using '-D' in the form returned by the\n" + "'keyctl rdescribe' command, for example:\n" + "\n" + " ./request-key -d -D \"user;0;0;0;debug:bar\" create 0 0 0 0 0 0 foo\n" + "\n" + "where 'user' is the key type and 'debug:bar' is the key description.\n" + ; + +static struct option long_options[] =3D { + { .name =3D "help", .val =3D 1 }, + { .name =3D "version", .val =3D 2 }, + {} +}; + /*************************************************************************= ****/ /* * @@ -149,19 +188,25 @@ int main(int argc, char *argv[]) char *buf; int ret, ntype, dpos, n, fd, opt; =20 - if (argc =3D=3D 2 && strcmp(argv[1], "--version") =3D=3D 0) { - printf("request-key from %s (Built %s)\n", - keyutils_version_string, keyutils_build_string); - return 0; + if (argc =3D=3D 1) { + fputs(help_text, stderr); + exit(2); } =20 signal(SIGSEGV, oops); signal(SIGBUS, oops); signal(SIGPIPE, SIG_IGN); =20 - while (opt =3D getopt(argc, argv, "D:dlnv"), + while (opt =3D getopt_long(argc, argv, "D:dlnv", long_options, NULL), opt !=3D -1) { switch (opt) { + case 1: + fputs(help_text, stderr); + exit(2); + case 2: + printf("request-key from %s (Built %s)\n", + keyutils_version_string, keyutils_build_string); + return 0; case 'D': test_desc =3D optarg; break; From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 7C5C530E0FC for ; Fri, 22 Aug 2025 14:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872562; cv=none; b=XFYUPGPknV2VIPx9/EhjrvkINqNmfWrUuw0Yd25CrAvUlyWFJjb8RfddizKiC5TuGhGtrYCkp2S2hmgzsIpV9ibCLYUkFx3UVnnnNVTxVP/6rig+265BEtQZpaxSWHUn/Tt0IgTHWTyHImS6QC4tL32ut8VXTARiVZi7cDWghgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872562; c=relaxed/simple; bh=j1vPITvQZRDC7p9CURE4NAGVCfaoq3pQr6ZkNtGZYds=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e7NqTFGYwPNcFj37ypUoMGIw2KuX+d/T06mIIL/WgbeuhTvhBmtx8bBBATziqyfcgGvaVzs2gojAjVc1M/tpLxzlJo+HXrfo4pDjZBYS8gws/DQoFIo3GYZDGu0KD4agidqc0aUrq6ayZCET/jC0R+fRl6s6lNjasCnok0vqeeU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cJKzJAJt; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cJKzJAJt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872558; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IUJpUooiNBv3XSIVDQcnwAYMx5ZgTm1CmlE2bCCM7T8=; b=cJKzJAJttm/CCxi93SsiPdjFBNzUItLsxknxHHrrMjeSCNtDdQrpgsksL6XQNzyDlMcPlc 9T/u7wP24wEwq5rMgJDK+MTnXZtDHeSkf8k6lijDPDbeufcLsCrpdJJVWRmCJJCDDrGyI5 TG+XjQnLGEIB404Fi26/BzsP7sv9J6o= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-358-Xn1AZ9JgOvKdnNYJ1kYVDQ-1; Fri, 22 Aug 2025 10:22:36 -0400 X-MC-Unique: Xn1AZ9JgOvKdnNYJ1kYVDQ-1 X-Mimecast-MFC-AGG-ID: Xn1AZ9JgOvKdnNYJ1kYVDQ_1755872555 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C0FCD180034A; Fri, 22 Aug 2025 14:22:35 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8F56B180028F; Fri, 22 Aug 2025 14:22:34 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/7] request-key: Add a simpler debug test Date: Fri, 22 Aug 2025 15:22:13 +0100 Message-ID: <20250822142215.2475014-7-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Add a simpler debug test to avoid the need to supply all the parameters that don't take part in the matching. This looks like: request-key --check [] [] request-key --check user debug:bar The unspecified parameters are just defaulted. Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen --- man/request-key.8 | 38 ++++++--- request-key.c | 203 ++++++++++++++++++++++++++++++---------------- 2 files changed, 161 insertions(+), 80 deletions(-) diff --git a/man/request-key.8 b/man/request-key.8 index 15b6bb8..90121d9 100644 --- a/man/request-key.8 +++ b/man/request-key.8 @@ -14,14 +14,18 @@ request\-key \- handle key instantiation callback reque= sts from the kernel .nf \fB/sbin/request\-key\fP [\fB--help\fP|\fB--version\fP] \fB/sbin/request\-key\fP \fIop key uid gid t-ring p-ring s-ring\fP [\fIinf= o\fP] -\fB/sbin/request\-key -d [-lnv] -D\fP \fIdesc\fP \fIop key\fP... +\fB/sbin/request\-key -d\fP [\fB-lnv\fP] [\fB-D\fP \fIdesc\fP] \fIop key\f= P... +\fB/sbin/request\-key --check\fP [\fB-lnv\fP] \fItype desc\fP [\fIinfo\fP]= [\fIop\fP] .fi .SH DESCRIPTION This program is invoked by the kernel when the kernel is asked for a key t= hat it doesn't have immediately available. The kernel creates a partially set = up -key and then calls out to this program to instantiate it. It is not intend= ed to -be called directly. A debugging capability is available through command l= ine -options, however, to aid in testing and in debugging configuration. +key and then calls out to this program to instantiate it. It is not genera= lly +intended to be called directly. +.PP +Debugging capabilities are also available through command line options to = aid +in testing and in configuration debugging. +.SH COMMAND-LINE OPTIONS .PP The normally required parameters are: .IP \fBop\fP @@ -39,12 +43,12 @@ The optional callout info that can be passed by \fIrequ= est_key(2)\fP. .PP The options that may also be supplied are: .IP \fB-d\fP -Turn on debugging mode. In this mode, no attempts are made to access any = keys +Debug the lookup. If this is supplied, no attempts are made to access any= keys and, if a handler program is selected, it won't be executed; instead, this -program will print a message and exit 0. -.IP \fB-D \fP -In debugging mode, use the proposed key description specified with this ra= ther -than the sample ("user;0;0;1f0000;debug:1234") built into the program. +program will print a message and exit. +.IP "\fB-D\fP \fIdescription\fP" +Use with \fB-d\fP to provide a specific key description rather than using = the +sample ("user;0;0;1f0000;debug:1234") built into the program. .IP \fB-l\fP Use configuration from the current directory. The program will use .IR request-key.d/* " and " request-key.conf @@ -56,6 +60,9 @@ will be copied to the system log - this will prevent that. .IP \fB-v\fP Turn on debugging output. This may be specified multiple times to produce increasing levels of verbosity. +.IP \fB--check\fP +If this is provide, then a simpler debugging mode is engaged that defaults= most +of the arguments, but otherwise operates much the same as '-d'. .IP \fB--help\fP Print help text and exit. .IP \fB--version\fP @@ -67,8 +74,19 @@ of them don't matter and can be just set to 0, e.g.: .nf request-key -d -D "user;0;0;0;debug:bar" create 0 0 0 0 0 0 foo .fi +.PP +With the simple check mode, the parameters that aren't involved in matching +are just defaulted and only those that are necessary are given; further, o= nly +the basic key description needs be supplied, e.g.: +.PP +.nf +request-key --check user debug:bar +request-key --check user debug:bar foo +request-key --check user debug:bar foo create +.fi .SH ERRORS -All errors will be logged to the syslog unless the \fB-n\fP option is give= n. +All errors will be logged to the syslog unless \fB-n\fP or \fB--check\fP a= re +given. Errors will also be logged to stderr if \fB-v\fP is given. .SH FILES .ul /etc/request\-key.d/*.conf diff --git a/request-key.c b/request-key.c index 9a7d741..302d083 100644 --- a/request-key.c +++ b/request-key.c @@ -133,6 +133,17 @@ static void error(const char *fmt, ...) #define file_error(FMT, ...) error("%s: "FMT, conffile, ## __VA_ARGS__) #define line_error(FMT, ...) error("%s:%d: "FMT, conffile, confline, ## _= _VA_ARGS__) =20 +static __attribute__((noreturn, format(printf, 1, 2))) +void cmderror(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); + exit(2); +} + static void oops(int x) { error("Died on signal %d", x); @@ -141,6 +152,7 @@ static void oops(int x) static const char help_text[] =3D "Usage: request-key [OPTIONS] [PARAMS]\n" " request-key [OPTIONS] -d -D [PARAMS]\n" + " request-key [OPTIONS] --check [] []\n" "\n" "Where the required parameters, [PARAMS], are, in order:\n" " : The operation type (e.g. 'create')\n" @@ -158,22 +170,33 @@ static const char help_text[] =3D " -l : Use config from local dir, not /etc\n" " -n : Don't log to syslog\n" " -v : Turn up verbosity (can use multiple times)\n" - " --version: Print version and exit\n" " --help : Print this text and exit\n" + " --version: Print version and exit\n" "\n" "Service program lookup testing can be done with the '-d' option, but\n" - "as there is no actual key to query, the called must supply the key's\n" - "attributes manually using '-D' in the form returned by the\n" - "'keyctl rdescribe' command, for example:\n" + "as there is no actual key to query, the caller must supply the key's\n" + "attributes manually. For debug mode this is done using the '-D'\n" + "option with a parameter in the form returned by the 'keyctl rdescribe'\n" + "command, for example:\n" "\n" " ./request-key -d -D \"user;0;0;0;debug:bar\" create 0 0 0 0 0 0 foo\n" "\n" "where 'user' is the key type and 'debug:bar' is the key description.\n" + "\n" + "A simpler test mode is also available by specifying '--check'. In\n" + "this mode, most of the parameters are set to fixed values and the\n" + "'callout' and 'op' values may be defaulted (to \"\" and \"create\"\n" + "respectively), for example:\n" + "\n" + " ./request-key --check user debug:bar\n" + " ./request-key --check user debug:bar foo\n" + " ./request-key --check user debug:bar foo create\n" ; =20 static struct option long_options[] =3D { { .name =3D "help", .val =3D 1 }, { .name =3D "version", .val =3D 2 }, + { .name =3D "check", .val =3D 3 }, {} }; =20 @@ -183,7 +206,7 @@ static struct option long_options[] =3D { */ int main(int argc, char *argv[]) { - struct parameters params; + struct parameters params =3D {}; char *test_desc =3D "user;0;0;1f0000;debug:1234"; char *buf; int ret, ntype, dpos, n, fd, opt; @@ -202,15 +225,23 @@ int main(int argc, char *argv[]) switch (opt) { case 1: fputs(help_text, stderr); - exit(2); + exit(0); case 2: printf("request-key from %s (Built %s)\n", keyutils_version_string, keyutils_build_string); return 0; + case 3: + if (debug_mode) + cmderror("Multiple debug modes specified\n"); + debug_mode =3D 2; + xnolog =3D 1; + break; case 'D': test_desc =3D optarg; break; case 'd': + if (debug_mode) + cmderror("Multiple debug modes specified\n"); debug_mode =3D 1; break; case 'l': @@ -228,80 +259,102 @@ int main(int argc, char *argv[]) argc -=3D optind; argv +=3D optind; =20 - if (argc !=3D 7 && argc !=3D 8) - error("Unexpected argument count: %d\n", argc); + switch (debug_mode) { + case 0: + /* If we were invoked directly by the kernel, make sure we have + * stdin, stdout and stderr set. + */ + fd =3D open("/dev/null", O_RDWR); + if (fd < 0) + error("open"); + if (fd > 2) { + close(fd); + } + else if (fd < 2) { + ret =3D dup(fd); + if (ret < 0) + error("dup failed: %m\n"); =20 - fd =3D open("/dev/null", O_RDWR); - if (fd < 0) - error("open"); - if (fd > 2) { - close(fd); - } - else if (fd < 2) { - ret =3D dup(fd); - if (ret < 0) - error("dup failed: %m\n"); + if (ret < 2 && dup(fd) < 0) + error("dup failed: %m\n"); + } + /* Fallthrough. */ + + case 1: + if (argc !=3D 7 && argc !=3D 8) + cmderror("Unexpected argument count: %d\n", argc); + + params.op =3D argv[0]; + params.key =3D argv[1]; + params.uid =3D argv[2]; + params.gid =3D argv[3]; + params.thread_keyring =3D argv[4]; + params.process_keyring =3D argv[5]; + params.session_keyring =3D argv[6]; + params.callout_info =3D argv[7]; + + params.key_id =3D atoi(params.key); + + if (!debug_mode) { + /* assume authority over the key + * - older kernel doesn't support this function + */ + ret =3D keyctl_assume_authority(params.key_id); + if (ret < 0 && !(argc =3D=3D 8 || errno =3D=3D EOPNOTSUPP)) + error("Failed to assume authority over key %d (%m)\n", + params.key_id); =20 - if (ret < 2 && dup(fd) < 0) - error("dup failed: %m\n"); - } + /* ask the kernel to describe the key to us */ + ret =3D keyctl_describe_alloc(params.key_id, &buf); + if (ret < 0) + goto inaccessible; =20 - params.op =3D argv[0]; - params.key =3D argv[1]; - params.uid =3D argv[2]; - params.gid =3D argv[3]; - params.thread_keyring =3D argv[4]; - params.process_keyring =3D argv[5]; - params.session_keyring =3D argv[6]; - params.callout_info =3D argv[7]; + /* get hold of the callout info */ + if (!params.callout_info) { + void *tmp; =20 - params.key_id =3D atoi(params.key); + if (keyctl_read_alloc(KEY_SPEC_REQKEY_AUTH_KEY, &tmp) < 0) + error("Failed to retrieve callout info (%m)\n"); =20 - /* assume authority over the key - * - older kernel doesn't support this function - */ - if (!debug_mode) { - ret =3D keyctl_assume_authority(params.key_id); - if (ret < 0 && !(argc =3D=3D 8 || errno =3D=3D EOPNOTSUPP)) - error("Failed to assume authority over key %d (%m)\n", - params.key_id); - } + params.callout_info =3D tmp; + } + } else { + buf =3D strdup(test_desc); + } =20 - /* ask the kernel to describe the key to us */ - if (!debug_mode) { - ret =3D keyctl_describe_alloc(params.key_id, &buf); - if (ret < 0) - goto inaccessible; - } else { - buf =3D strdup(test_desc); + /* extract the type and description from the key */ + debug("Key descriptor: \"%s\"\n", buf); + ntype =3D -1; + dpos =3D -1; + + n =3D sscanf(buf, "%*[^;]%n;%*d;%*d;%x;%n", &ntype, &n, &dpos); + if (n !=3D 1) + error("Failed to parse key description\n"); + + params.key_type =3D buf; + params.key_type[ntype] =3D 0; + params.key_desc =3D buf + dpos; + break; + + case 2: + /* request-key [] [] */ + if (argc < 2 || argc > 4) + cmderror("Unexpected argument count: %d\n", argc); + params.key_type =3D argv[0]; + params.key_desc =3D argv[1]; + params.callout_info =3D argc > 2 ? argv[2] : ""; + params.op =3D argc > 3 ? argv[3] : "create"; + params.key =3D "undef-key"; + params.uid =3D "undef-uid"; + params.gid =3D "undef-gid"; + params.thread_keyring =3D "undef-tring"; + params.process_keyring =3D "undef-pring"; + params.session_keyring =3D "undef-sring"; + break; } =20 - /* extract the type and description from the key */ - debug("Key descriptor: \"%s\"\n", buf); - ntype =3D -1; - dpos =3D -1; - - n =3D sscanf(buf, "%*[^;]%n;%*d;%*d;%x;%n", &ntype, &n, &dpos); - if (n !=3D 1) - error("Failed to parse key description\n"); - - params.key_type =3D buf; - params.key_type[ntype] =3D 0; - params.key_desc =3D buf + dpos; - debug("Key type: %s\n", params.key_type); debug("Key desc: %s\n", params.key_desc); - - /* get hold of the callout info */ - if (!params.callout_info) { - void *tmp; - - if (keyctl_read_alloc(KEY_SPEC_REQKEY_AUTH_KEY, &tmp) < 0) - error("Failed to retrieve callout info (%m)\n"); - - params.callout_info =3D tmp; - } - debug("CALLOUT: '%s'\n", params.callout_info); =20 /* determine the action to perform */ @@ -734,6 +787,16 @@ static void execute_program(struct parameters *params,= char *cmdline) debug("%s %s\n", pipeit ? "PipeThru" : "Run", prog); for (ap =3D argv; *ap; ap++) debug("- argv[%td] =3D \"%s\"\n", ap - argv, *ap); + } else if (debug_mode) { + char **ap; + + printf("Run: "); + if (pipeit) + putchar('|'); + printf("%s", prog); + for (ap =3D argv; *ap; ap++) + printf(" \"%s\"", *ap); + printf("\n"); } =20 /* become the same UID/GID as the key requesting process */ From nobody Fri Oct 3 23:02:10 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 E15DC309DDB for ; Fri, 22 Aug 2025 14:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872876; cv=none; b=RhKoSZ09DBx+km/3zIhYGQQHn321PqrbRAwhGJ/Ml9KANm48U6rZctghKpIRlrzGW1/leAhn8sbpu50/LImY8bwwbUeMtuvBA1ZW85yk3TC4U9fCpYBhm7TTDB9zbwPv0Y4sNrNHLF5quCDcca/BR2/UMkWkx2YcRpIK8kU72tg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872876; c=relaxed/simple; bh=rCTWizcS8tppI1Pb0q/Qf3pwJuoMcIAjRl0Y1SBqJ7k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sMsnegrcV/YEzAtiacbu1LEaa5hL9DKKh0B5FW4mIXWbHwpU6EecsrvcLGwoARpihrgtP7cLc37WWmuBeL+DW4nqH8QMQr5ulpd6xXG8Ygcz/TQieLb6370uXwmqrhjXgBVNmDHb/n/ix1VUwROGRapcnsDBD9SYFp4QRuDV3VY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LS/FdKZx; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LS/FdKZx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755872873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MyiW9WG9d02tUGD/bkZ0ruTVUbMBtZ9QTexYOI6sGjk=; b=LS/FdKZxEN6o9hW2CaQp8Bucu91jvLAgaZrNTthKAB85jQb0jf+L1Jd88iH9MSQf/7hYBq vPkKOFv+Ks4RTKERGR8aDz3+XJXScDETif5IIf/bmsrMUb1vSL95mQhmvlBXlcZK+AGr0G q7AnLkljjmsiDXgTY2NhDcfECJrwSU8= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-413-xBDlz-VGM3KQFE3OqAiivQ-1; Fri, 22 Aug 2025 10:22:39 -0400 X-MC-Unique: xBDlz-VGM3KQFE3OqAiivQ-1 X-Mimecast-MFC-AGG-ID: xBDlz-VGM3KQFE3OqAiivQ_1755872558 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 762F31956087; Fri, 22 Aug 2025 14:22:38 +0000 (UTC) Received: from warthog.procyon.org.com (unknown [10.42.28.132]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3B68C1955F24; Fri, 22 Aug 2025 14:22:37 +0000 (UTC) From: David Howells To: Jarkko Sakkinen Cc: David Howells , keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/7] request-key: Support the promised multiwildcard matching Date: Fri, 22 Aug 2025 15:22:14 +0100 Message-ID: <20250822142215.2475014-8-dhowells@redhat.com> In-Reply-To: <20250822142215.2475014-1-dhowells@redhat.com> References: <20250822142215.2475014-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" The manual page for request-key.conf says that multiple wildcards can be used in match elements in the config files, so implement this feature. Also add a flag, "--test-match", to allow that match function to be tested directly, e.g.: request-key --test-match "**a*a***i**a**a**" antidisestablishmentarianism Signed-off-by: David Howells Reviewed-by: Jarkko Sakkinen --- man/request-key.8 | 19 +++- request-key.c | 252 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 217 insertions(+), 54 deletions(-) diff --git a/man/request-key.8 b/man/request-key.8 index 90121d9..7cec6e8 100644 --- a/man/request-key.8 +++ b/man/request-key.8 @@ -16,6 +16,7 @@ request\-key \- handle key instantiation callback request= s from the kernel \fB/sbin/request\-key\fP \fIop key uid gid t-ring p-ring s-ring\fP [\fIinf= o\fP] \fB/sbin/request\-key -d\fP [\fB-lnv\fP] [\fB-D\fP \fIdesc\fP] \fIop key\f= P... \fB/sbin/request\-key --check\fP [\fB-lnv\fP] \fItype desc\fP [\fIinfo\fP]= [\fIop\fP] +\fB/sbin/request\-key --match\fP [\fB-v\fP] \fIpattern datum\fP [\fIdatum.= ..\fP] .fi .SH DESCRIPTION This program is invoked by the kernel when the kernel is asked for a key t= hat @@ -65,6 +66,10 @@ If this is provide, then a simpler debugging mode is eng= aged that defaults most of the arguments, but otherwise operates much the same as '-d'. .IP \fB--help\fP Print help text and exit. +.IP \fB--match\fP +Test the pattern matcher. The first parameter is the pattern and all +subsequent parameters are strings to try matching against that. The progr= am +exits 1 if any match failure occurs, 0 if all succeed. .IP \fB--version\fP Print the program version and exit. .SH EXAMPLES @@ -84,9 +89,19 @@ request-key --check user debug:bar request-key --check user debug:bar foo request-key --check user debug:bar foo create .fi +.PP +Pattern match testing can be done directly without invoking any of the loo= kup +code and without reference to the configuration, e.g.: +.PP +.nf +request-key --match "debug:*" debug:a user:b user:debug:c +request-key --match -v "*a*a***a*a**a**" antidisestablishmentarianism +.fi +.PP .SH ERRORS -All errors will be logged to the syslog unless \fB-n\fP or \fB--check\fP a= re -given. Errors will also be logged to stderr if \fB-v\fP is given. +All errors will be logged to the syslog unless one of \fB-n\fP, \fB--check= \fP +or \fB--match\fP are given. Errors will also be logged to stderr if \fB-v= \fP +is given. .SH FILES .ul /etc/request\-key.d/*.conf diff --git a/request-key.c b/request-key.c index 302d083..97afeee 100644 --- a/request-key.c +++ b/request-key.c @@ -16,6 +16,7 @@ * data: command name, e.g.: "/home/dhowells/request-key-create.sh" */ =20 +#define _GNU_SOURCE #include #include #include @@ -56,6 +57,7 @@ struct parameters { }; =20 static int verbosity; +static int match_debug; static int xlocaldirs; static int xnolog; static int debug_mode; @@ -81,8 +83,8 @@ static void pipe_to_program(struct parameters *params, char **argv) __attribute__((noreturn)); =20 -static int match(const char *pattern, int plen, const char *datum, int dle= n, - unsigned int *wildness); +static int match(const char *pattern, unsigned int plen, + const char *datum, unsigned int dlen); =20 static void debug(const char *fmt, ...) __attribute__((format(printf, 1, 2= ))); static void debug(const char *fmt, ...) @@ -153,6 +155,7 @@ static const char help_text[] =3D "Usage: request-key [OPTIONS] [PARAMS]\n" " request-key [OPTIONS] -d -D [PARAMS]\n" " request-key [OPTIONS] --check [] []\n" + " request-key [OPTIONS] --match [datum ...]\n" "\n" "Where the required parameters, [PARAMS], are, in order:\n" " : The operation type (e.g. 'create')\n" @@ -168,6 +171,7 @@ static const char help_text[] =3D " -d : Debug mode for direct cmdline testing\n" " -D : Description for debug mode\n" " -l : Use config from local dir, not /etc\n" + " -M : Enable string-matching algorithm debugging\n" " -n : Don't log to syslog\n" " -v : Turn up verbosity (can use multiple times)\n" " --help : Print this text and exit\n" @@ -191,12 +195,20 @@ static const char help_text[] =3D " ./request-key --check user debug:bar\n" " ./request-key --check user debug:bar foo\n" " ./request-key --check user debug:bar foo create\n" + "\n" + "Another test mode is available ('--match') that allows the\n" + "pattern match evaluator to be tested against a bunch of sample\n" + "strings. For each string, either 'No' or 'Yes(n)' is printed,\n" + "where 'n' is the number of wild-matched characters. For example:\n" + "\n" + " ./request-key --match \"**foo**bar*\" \"foobar\"\n" ; =20 static struct option long_options[] =3D { { .name =3D "help", .val =3D 1 }, { .name =3D "version", .val =3D 2 }, { .name =3D "check", .val =3D 3 }, + { .name =3D "match", .val =3D 4 }, {} }; =20 @@ -236,6 +248,12 @@ int main(int argc, char *argv[]) debug_mode =3D 2; xnolog =3D 1; break; + case 4: + if (debug_mode) + cmderror("Multiple debug modes specified\n"); + debug_mode =3D 3; + xnolog =3D 1; + break; case 'D': test_desc =3D optarg; break; @@ -351,6 +369,30 @@ int main(int argc, char *argv[]) params.process_keyring =3D "undef-pring"; params.session_keyring =3D "undef-sring"; break; + + case 3: + const char *pattern; + int e =3D 0; + + if (verbosity) + match_debug =3D 1; + if (argc < 2) + cmderror("Insufficient arguments\n"); + pattern =3D argv[0]; + argv++; + argc--; + for (; argc; argc--) { + int m =3D match(pattern, strlen(pattern), + argv[0], strlen(argv[0])); + if (m >=3D 0) { + printf("Yes(%d)\n", m); + } else { + printf("No\n"); + e =3D 3; + } + argv++; + } + exit(e); } =20 debug("Key type: %s\n", params.key_type); @@ -450,6 +492,7 @@ static void scan_conf_file(struct parameters *params, i= nt dirfd, const char *con for (confline =3D 1;; confline++) { unsigned int wildness[4] =3D {}; unsigned int len; + int m; =20 /* read the file line-by-line */ if (!fgets(buf, sizeof(buf), conf)) { @@ -478,9 +521,10 @@ static void scan_conf_file(struct parameters *params, = int dirfd, const char *con goto syntax_error; *p =3D 0; =20 - if (!match(q, p - q, params->op, params->oplen, &wildness[0])) + m =3D match(q, p - q, params->op, params->oplen); + if (m < 0) continue; - + wildness[0] +=3D m; p++; =20 /* attempt to match the type */ @@ -494,8 +538,10 @@ static void scan_conf_file(struct parameters *params, = int dirfd, const char *con goto syntax_error; *p =3D 0; =20 - if (!match(q, p - q, params->key_type, params->ktlen, &wildness[1])) + m =3D match(q, p - q, params->key_type, params->ktlen); + if (m < 0) continue; + wildness[1] +=3D m; =20 p++; =20 @@ -510,9 +556,10 @@ static void scan_conf_file(struct parameters *params, = int dirfd, const char *con goto syntax_error; *p =3D 0; =20 - if (!match(q, p - q, params->key_desc, params->kdlen, &wildness[2])) + m =3D match(q, p - q, params->key_desc, params->kdlen); + if (m < 0) continue; - + wildness[2] +=3D m; p++; =20 /* attempt to match the callout info */ @@ -526,8 +573,10 @@ static void scan_conf_file(struct parameters *params, = int dirfd, const char *con goto syntax_error; *p =3D 0; =20 - if (!match(q, p - q, params->callout_info, params->cilen, &wildness[3])) + m =3D match(q, p - q, params->callout_info, params->cilen); + if (m < 0) continue; + wildness[3] +=3D m; =20 p++; =20 @@ -569,75 +618,174 @@ syntax_error: line_error("Syntax error\n"); } =20 +static void mdebug(const char *msg, + const char *pattern, unsigned int plen, + const char *datum, unsigned int dlen) +{ + if (match_debug) + printf("- %s(%*.*s,%*.*s)\n", + msg, plen, plen, pattern, dlen, dlen, datum); +} + +static void mdebug2(const char *msg) +{ + if (match_debug) + printf("- %s\n", msg); +} + /*************************************************************************= ****/ /* * attempt to match a datum to a pattern * - one asterisk is allowed anywhere in the pattern to indicate a wildcard - * - returns true if matched, false if not - * - adds the total number of chars skipped by wildcard to *_wildness + * - returns -1 if not matched or the number of chars skipped by wildcard */ -static int match(const char *pattern, int plen, const char *datum, int dle= n, - unsigned int *_wildness) +static int match(const char *pattern, unsigned int plen, + const char *datum, unsigned int dlen) { - const char *asterisk; - int n; + const char *left, *right; + unsigned int wild =3D 0, n; =20 if (verbosity >=3D 2) debug("match(%*.*s,%*.*s)", plen, plen, pattern, dlen, dlen, datum); + mdebug("match", pattern, plen, datum, dlen); =20 - asterisk =3D memchr(pattern, '*', plen); - if (!asterisk) { - /* exact match only if no wildcard */ - if (plen =3D=3D dlen && memcmp(pattern, datum, dlen) =3D=3D 0) - goto yes; + if (plen <=3D 0) goto no; + + /* Special case "*" matches everything */ + if (plen =3D=3D 1 && pattern[0] =3D=3D '*') { + wild =3D dlen; + goto yes; } =20 - /* the datum mustn't be shorter than the pattern without the asterisk */ - if (dlen < plen - 1) + left =3D memchr(pattern, '*', plen); + if (!left) { + /* No wildcard: exact match only. */ + mdebug2("exact-only"); + if (plen =3D=3D dlen && memcmp(pattern, datum, dlen) =3D=3D 0) { + wild =3D 0; + goto yes; + } goto no; + } =20 - n =3D asterisk - pattern; - if (n =3D=3D 0) { - /* wildcard at beginning of pattern */ - pattern++; - if (!*pattern) - goto yes_wildcard; /* "*" matches everything */ + if (left > pattern) { + /* Check prefix matches. */ + mdebug("prefix", pattern, left - pattern, datum, dlen); + if (dlen < left - pattern || + memcmp(pattern, datum, left - pattern) !=3D 0) { + mdebug2("prefix-x"); + goto no; + } + n =3D left - pattern; + datum +=3D n; + dlen -=3D n; + pattern +=3D n + 1; + plen -=3D n + 1; + while (plen > 0 && *pattern =3D=3D '*') { + pattern++; + plen--; + } + if (!plen) { + /* Simple prefix match. */ + mdebug2("simple-prefix"); + wild =3D dlen; + goto yes; + } + } else { + while (plen > 0 && *pattern =3D=3D '*') { + pattern++; + plen--; + } + } =20 - /* match the end of the datum */ - if (memcmp(pattern, datum + (dlen - (plen - 1)), plen - 1) =3D=3D 0) - goto yes_wildcard; - goto no; + right =3D memrchr(pattern, '*', plen); + if (right !=3D pattern + plen - 1) { + /* Check suffix matches */ + const char *suffix =3D right ? right + 1 : pattern; + const char *p_end =3D pattern + plen; + const char *d_end =3D datum + dlen; + int slen =3D p_end - suffix; + + mdebug("suffix", suffix, slen, datum, dlen); + if (slen > dlen || + memcmp(suffix, d_end - slen, slen) !=3D 0) { + mdebug2("suffix-x"); + goto no; + } + dlen -=3D slen; + plen -=3D slen; + while (plen > 0 && pattern[plen - 1] =3D=3D '*') + plen--; + if (!plen) { + /* Simple prefix+suffix match. */ + wild =3D dlen; + goto yes; + } + } else { + while (plen > 0 && pattern[plen - 1] =3D=3D '*') + plen--; } =20 - /* need to match beginning of datum for "abc*" and "abc*def" */ - if (memcmp(pattern, datum, n) !=3D 0) - goto no; + /* We now have a remnant part of the pattern bounded by a pair of + * wildcards (e.g.: [aa**]bb*cc*dd[**ee]) and we need to find the + * middle substrings in order in the remnant part of the datum. The + * remnant pattern fragment may also contain further wildcards. + */ + for (;;) { + mdebug("middle", pattern, plen, datum, dlen); + const char *sub =3D pattern, *p; + int slen, skip; + + left =3D memchr(pattern, '*', plen); + if (!left) { + /* No further wildcard */ + mdebug2("middle-1"); + if (plen > dlen) + goto no; + p =3D memmem(datum, dlen, pattern, plen); + if (!p) { + mdebug2("middle-1-x"); + goto no; + } + wild +=3D dlen - plen; + goto yes; + } =20 - if (!asterisk[1]) - goto yes_wildcard; /* "abc*" matches */ + slen =3D left - pattern; + pattern =3D left + 1; + plen -=3D slen + 1; =20 - /* match the end of the datum */ - asterisk++; - n =3D plen - n - 1; - if (memcmp(pattern, datum + (dlen - n), n) =3D=3D 0) - goto yes_wildcard; + p =3D memmem(datum, dlen, sub, slen); + if (!p) { + mdebug2("middle-x"); + goto no; + } + skip =3D p - datum; + wild +=3D skip; + datum +=3D skip + slen; + dlen -=3D skip + slen; + + while (plen && *pattern =3D=3D '*') { + pattern++; + plen--; + } =20 -no: - if (verbosity >=3D 2) - debug(" =3D no\n"); - return 0; + /* plen really shouldn't be 0 here. */ + if (plen <=3D 0) { + error("plen unexpectedly 0\n"); + goto no; + } + } =20 yes: if (verbosity >=3D 2) - debug(" =3D yes (w=3D0)\n"); - return 1; - -yes_wildcard: - *_wildness +=3D dlen - (plen - 1); + debug(" =3D yes (w=3D%u)\n", wild); + return wild; +no: if (verbosity >=3D 2) - debug(" =3D yes (w=3D%u)\n", dlen - (plen - 1)); - return 1; + debug(" =3D no\n"); + return -1; =20 } /* end match() */