2023-04-06 06:55:45

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

Ensure fd is closed on error paths.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/pmu.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 96ef317bac41..9eedbfc9e863 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
continue;

fd = openat(dirfd, name, O_RDONLY);
+ if (fd == -1) {
+ pr_debug("Cannot open %s\n", name);
+ continue;
+ }
file = fdopen(fd, "r");
if (!file) {
- pr_debug("Cannot open %s\n", name);
+ close(fd);
continue;
}

@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
continue;

fd = openat(caps_fd, name, O_RDONLY);
+ if (fd == -1)
+ continue;
file = fdopen(fd, "r");
- if (!file)
+ if (!file) {
+ close(fd);
continue;
+ }

if (!fgets(value, sizeof(value), file) ||
(perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
--
2.40.0.348.gf938b09366-goog


2023-04-06 20:21:28

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

On Wed, Apr 5, 2023 at 11:53 PM Ian Rogers <[email protected]> wrote:
>
> Ensure fd is closed on error paths.
>
> Signed-off-by: Ian Rogers <[email protected]>

Acked-by: Namhyung Kim <[email protected]>

Thanks,
Namhyung

> ---
> tools/perf/util/pmu.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 96ef317bac41..9eedbfc9e863 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
> continue;
>
> fd = openat(dirfd, name, O_RDONLY);
> + if (fd == -1) {
> + pr_debug("Cannot open %s\n", name);
> + continue;
> + }
> file = fdopen(fd, "r");
> if (!file) {
> - pr_debug("Cannot open %s\n", name);
> + close(fd);
> continue;
> }
>
> @@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
> continue;
>
> fd = openat(caps_fd, name, O_RDONLY);
> + if (fd == -1)
> + continue;
> file = fdopen(fd, "r");
> - if (!file)
> + if (!file) {
> + close(fd);
> continue;
> + }
>
> if (!fgets(value, sizeof(value), file) ||
> (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
> --
> 2.40.0.348.gf938b09366-goog
>

2023-04-06 21:14:04

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

Em Wed, Apr 05, 2023 at 11:52:24PM -0700, Ian Rogers escreveu:
> Ensure fd is closed on error paths.

Was this reported by Jiri?

- Arnaldo

> Signed-off-by: Ian Rogers <[email protected]>
> ---
> tools/perf/util/pmu.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 96ef317bac41..9eedbfc9e863 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
> continue;
>
> fd = openat(dirfd, name, O_RDONLY);
> + if (fd == -1) {
> + pr_debug("Cannot open %s\n", name);
> + continue;
> + }
> file = fdopen(fd, "r");
> if (!file) {
> - pr_debug("Cannot open %s\n", name);
> + close(fd);
> continue;
> }
>
> @@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
> continue;
>
> fd = openat(caps_fd, name, O_RDONLY);
> + if (fd == -1)
> + continue;
> file = fdopen(fd, "r");
> - if (!file)
> + if (!file) {
> + close(fd);
> continue;
> + }
>
> if (!fgets(value, sizeof(value), file) ||
> (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
> --
> 2.40.0.348.gf938b09366-goog
>

--

- Arnaldo

2023-04-06 21:34:54

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

On Thu, Apr 6, 2023 at 2:09 PM Arnaldo Carvalho de Melo <[email protected]> wrote:
>
> Em Wed, Apr 05, 2023 at 11:52:24PM -0700, Ian Rogers escreveu:
> > Ensure fd is closed on error paths.
>
> Was this reported by Jiri?

Jiri's one was in the code I modified, whereas these were from
Namhyung's previous openat change. It is the same issue, I'm agnostic
on whether it needs a tag or not.

Thanks,
Ian

> - Arnaldo
>
> > Signed-off-by: Ian Rogers <[email protected]>
> > ---
> > tools/perf/util/pmu.c | 12 ++++++++++--
> > 1 file changed, 10 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> > index 96ef317bac41..9eedbfc9e863 100644
> > --- a/tools/perf/util/pmu.c
> > +++ b/tools/perf/util/pmu.c
> > @@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
> > continue;
> >
> > fd = openat(dirfd, name, O_RDONLY);
> > + if (fd == -1) {
> > + pr_debug("Cannot open %s\n", name);
> > + continue;
> > + }
> > file = fdopen(fd, "r");
> > if (!file) {
> > - pr_debug("Cannot open %s\n", name);
> > + close(fd);
> > continue;
> > }
> >
> > @@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
> > continue;
> >
> > fd = openat(caps_fd, name, O_RDONLY);
> > + if (fd == -1)
> > + continue;
> > file = fdopen(fd, "r");
> > - if (!file)
> > + if (!file) {
> > + close(fd);
> > continue;
> > + }
> >
> > if (!fgets(value, sizeof(value), file) ||
> > (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
> > --
> > 2.40.0.348.gf938b09366-goog
> >
>
> --
>
> - Arnaldo

2023-04-06 21:43:54

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

On Wed, Apr 05, 2023 at 11:52:24PM -0700, Ian Rogers wrote:
> Ensure fd is closed on error paths.
>
> Signed-off-by: Ian Rogers <[email protected]>

nice catch

Acked-by: Jiri Olsa <[email protected]>

thanks,
jirka

> ---
> tools/perf/util/pmu.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 96ef317bac41..9eedbfc9e863 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
> continue;
>
> fd = openat(dirfd, name, O_RDONLY);
> + if (fd == -1) {
> + pr_debug("Cannot open %s\n", name);
> + continue;
> + }
> file = fdopen(fd, "r");
> if (!file) {
> - pr_debug("Cannot open %s\n", name);
> + close(fd);
> continue;
> }
>
> @@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
> continue;
>
> fd = openat(caps_fd, name, O_RDONLY);
> + if (fd == -1)
> + continue;
> file = fdopen(fd, "r");
> - if (!file)
> + if (!file) {
> + close(fd);
> continue;
> + }
>
> if (!fgets(value, sizeof(value), file) ||
> (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
> --
> 2.40.0.348.gf938b09366-goog
>