[LTP] [PATCH v2 03/33] keyctl11: Test KEYCTL_DESCRIBE with exact buffer size

Cyril Hrubis chrubis@suse.cz
Fri Sep 11 10:01:14 CEST 2026


Hi!
> +#include "keyctl_common.h"
> +
> +static key_serial_t key;
> +static long desc_len;
> +static char *buf;
> +
> +static void setup(void)
> +{
> +	SAFE_KEYCTL(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
> +
> +	key = new_user_key("k", "payload", 7, KEY_SPEC_PROCESS_KEYRING);
> +	SAFE_KEYCTL(KEYCTL_SETPERM, key, KEY_PERM_SET, 0, 0);
> +
> +	TEST(keyctl(KEYCTL_DESCRIBE, key, (unsigned long)NULL, 0, 0));
> +	if (TST_RET < 0)
> +		tst_brk(TBROK | TTERRNO, "KEYCTL_DESCRIBE failed");
> +
> +	desc_len = TST_RET;
> +	buf = SAFE_MALLOC(desc_len);
> +}
> +
> +static void cleanup(void)
> +{
> +	free(buf);
> +}
> +
> +static void run(void)
> +{
> +	memset(buf, 0, desc_len);

If we are trying to check that the string was copied including the
terminating byte we should memset the buffer to 0xff, right?

> +	TST_EXP_EQ_LI_SILENT(keyctl(KEYCTL_DESCRIBE, key, (unsigned long)buf,
> +				    desc_len, 0), desc_len);
> +	if (!TST_PASS)
> +		return;
> +
> +	if (buf[desc_len - 1] != '\0') {
> +		tst_res(TFAIL, "description is not NUL terminated");
> +		return;
> +	}
> +
> +	tst_res(TPASS, "full description including NUL fits exact %ld byte buffer",
> +		desc_len);
> +}

Also this is quite similar to the previous test, maybe it would make
sense to put this check in the previous test instead.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list