public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: Re: [PATCH] conf: Fix one Coverity CID 258163 warning, work around another one
Date: Tue, 24 May 2022 19:05:06 +1000	[thread overview]
Message-ID: <YoyfwsxnZXUD7FcM@yekko> (raw)
In-Reply-To: <20220520090122.2510632-1-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2145 bytes --]

On Fri, May 20, 2022 at 11:01:22AM +0200, Stefano Brivio wrote:
> In conf_runas(), Coverity reports that we might dereference uid and
> gid despite possibly being NULL (CWE-476) because of the check after
> the first sscanf(). They can't be NULL, but I actually wanted to
> check that UID and GID are non-zero (the user could otherwise pass
> --runas root:root and defy the whole mechanism).
> 
> Later on, we have the same type of warning for 'gr': it's compared
> against NULL, so it might be NULL, which is actually the case: but
> in that case, we don't dereference it, because we'll return -ENOENT
> right away. Rewrite the clause to silence the warning.
> 
> Signed-off-by: Stefano Brivio <sbrivio(a)redhat.com>

Reviewed-by: David Gibson <david(a)gibson.dropbear.id.au>

> ---
>  conf.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/conf.c b/conf.c
> index ddad9a3..d615cf5 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -857,7 +857,7 @@ static int conf_runas(const char *opt, unsigned int *uid, unsigned int *gid)
>  	struct group *gr;
>  
>  	/* NOLINTNEXTLINE(cert-err34-c): 2 if conversion succeeds */
> -	if (sscanf(opt, "%u:%u", uid, gid) == 2 && uid && gid)
> +	if (sscanf(opt, "%u:%u", uid, gid) == 2 && *uid && *gid)
>  		return 0;
>  
>  	*uid = strtol(opt, &endptr, 0);
> @@ -874,12 +874,10 @@ static int conf_runas(const char *opt, unsigned int *uid, unsigned int *gid)
>  	/* NOLINTNEXTLINE(cert-err34-c): 2 if conversion succeeds */
>  	if (sscanf(opt, "%" STR(LOGIN_NAME_MAX) "[^:]:"
>  			"%" STR(LOGIN_NAME_MAX) "s", ubuf, gbuf) == 2) {
> -		pw = getpwnam(ubuf);
> -		if (!pw || !(*uid = pw->pw_uid))
> +		if (!(pw = getpwnam(ubuf)) || !(*uid = pw->pw_uid))
>  			return -ENOENT;
>  
> -		gr = getgrnam(gbuf);
> -		if (!gr || !(*gid = gr->gr_gid))
> +		if (!(gr = getgrnam(gbuf)) || !(*gid = gr->gr_gid))
>  			return -ENOENT;
>  
>  		return 0;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2022-05-24  9:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-20  9:01 [PATCH] conf: Fix one Coverity CID 258163 warning, work around another one Stefano Brivio
2022-05-24  9:05 ` David Gibson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YoyfwsxnZXUD7FcM@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).