diff mbox series

[ndctl,4/8] dimm: fix potential fd leakage in dimm_action()

Message ID 1ca17cf4-a5fd-786d-fa50-8ed09ccd55e4@huawei.com (mailing list archive)
State New
Headers show
Series fix serverl issues reported by Coverity | expand

Commit Message

Zhiqiang Liu Nov. 6, 2020, 9:26 a.m. UTC
In dimm_action(), actx.f_out and actx.f_in may be set by calling
fopen(). If exceptions occur, we will directly goto out tag.
However, we did not close actx.f_out|actx.f_in in out tag, which
will cause fd leakage.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
 ndctl/dimm.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

Jeff Moyer Nov. 20, 2020, 4:36 p.m. UTC | #1
Zhiqiang Liu <liuzhiqiang26@huawei.com> writes:

> In dimm_action(), actx.f_out and actx.f_in may be set by calling
> fopen(). If exceptions occur, we will directly goto out tag.
> However, we did not close actx.f_out|actx.f_in in out tag, which
> will cause fd leakage.
>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
>  ndctl/dimm.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/ndctl/dimm.c b/ndctl/dimm.c
> index 90eb0b8..2f52cda 100644
> --- a/ndctl/dimm.c
> +++ b/ndctl/dimm.c
> @@ -1352,7 +1352,7 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
>  			fprintf(stderr, "failed to open: %s: (%s)\n",
>  					param.infile, strerror(errno));
>  			rc = -errno;
> -			goto out;
> +			goto out_close_fout;
>  		}
>  	}
>
> @@ -1371,7 +1371,7 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
>  		fprintf(stderr, "'%s' is not a valid label version\n",
>  				param.labelversion);
>  		rc = -EINVAL;
> -		goto out;
> +		goto out_close_fin_fout;
>  	}
>
>  	rc = 0;
> @@ -1423,12 +1423,14 @@ static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
>  		util_display_json_array(actx.f_out, actx.jdimms, flags);
>  	}
>
> -	if (actx.f_out != stdout)
> -		fclose(actx.f_out);
> -
> + out_close_fin_fout:
>  	if (actx.f_in != stdin)
>  		fclose(actx.f_in);
>
> + out_close_fout:
> +	if (actx.f_out != stdout)
> +		fclose(actx.f_out);
> +
>   out:
>  	/*
>  	 * count if some actions succeeded, 0 if none were attempted,

Acked-by: Jeff Moyer <jmoyer@redhat.com>
diff mbox series

Patch

diff --git a/ndctl/dimm.c b/ndctl/dimm.c
index 90eb0b8..2f52cda 100644
--- a/ndctl/dimm.c
+++ b/ndctl/dimm.c
@@ -1352,7 +1352,7 @@  static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
 			fprintf(stderr, "failed to open: %s: (%s)\n",
 					param.infile, strerror(errno));
 			rc = -errno;
-			goto out;
+			goto out_close_fout;
 		}
 	}

@@ -1371,7 +1371,7 @@  static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
 		fprintf(stderr, "'%s' is not a valid label version\n",
 				param.labelversion);
 		rc = -EINVAL;
-		goto out;
+		goto out_close_fin_fout;
 	}

 	rc = 0;
@@ -1423,12 +1423,14 @@  static int dimm_action(int argc, const char **argv, struct ndctl_ctx *ctx,
 		util_display_json_array(actx.f_out, actx.jdimms, flags);
 	}

-	if (actx.f_out != stdout)
-		fclose(actx.f_out);
-
+ out_close_fin_fout:
 	if (actx.f_in != stdin)
 		fclose(actx.f_in);

+ out_close_fout:
+	if (actx.f_out != stdout)
+		fclose(actx.f_out);
+
  out:
 	/*
 	 * count if some actions succeeded, 0 if none were attempted,