[LTP] [PATCH v3 09/36] keyctl15: Test KEYCTL_GET_SECURITY label retrieval

Cyril Hrubis chrubis@suse.cz
Thu Sep 17 13:50:39 CEST 2026


Hi!
> Maybe refine it like below:
> 
> static void run(void)
> {
> 	int rc;
> 
> 	memset(buf, 0, sizeof(buf));
> 
> 	rc = SAFE_KEYCTL(KEYCTL_GET_SECURITY, key, (unsigned long)buf, sizeof(buf), 0);
> 
> 	if (rc < 1) {
> 		tst_res(TFAIL, "returned %d, expected >= 1", rc);
> 		return;
> 	}
> 
> 	if (rc == 1) {
> 		if (buf[0] != '\0')
> 			tst_res(TFAIL, "empty label is not NUL terminated");
> 		else
> 			tst_res(TPASS, "no label set, empty string returned");
> 		return;
> 	}
> 
> 	if (buf[0] == '\0')
> 		tst_res(TFAIL, "non-empty label is NUL terminated");
> 	else
> 		tst_res(TPASS, "security label returned, full length %d", rc);
> }

Shouldn't we check that the security label is NUL terminated as well?

The algorithm description in the doc comment actually says what should
be done:


	if (rc == 0) {
		tst_res(TFAIL, "empty security label not NUL terminated");
		return;
	}

	if (rc > sizeof(buf)) {
		tst_res(TFAIL, "buffer too small");
		return;
	}

	if (buf[rc-1] != '\0') {
		tst_res(TFAIL, "security label not NUL terminated");
		return;
	}

	tst_res(TPASS, "Security label was NUL terminated");

And perhaps we can as well check that there is no NUL byte in the buffer
before rc as well:

	for (i = 0; i < rc; i++) {
		if (buf[i] == '\0')
			tst_res(TFAIL, "NUL in the middle of security label at %i", buf[i]);
	}


-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list