From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH RFC 3/3] uncompress: skip dentry cache in uncompress_buf_to_buf
Date: Mon, 20 Nov 2023 09:37:50 +0100 [thread overview]
Message-ID: <20231120083750.3967831-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20231120083750.3967831-1-a.fatoum@pengutronix.de>
make_temp() creates a named temporary file, which even after deletion
will keep a negative dentry cache entry that's never freed.
As we don't use the file name for anything, we can just get our
temporary file via open(O_TMPFILE), which won't involve the dentry cache
at all and thereby avoiding leaking memory when fuzzing uncompress_buf_to_buf.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
lib/uncompress.c | 34 +++++++++++++++-------------------
1 file changed, 15 insertions(+), 19 deletions(-)
diff --git a/lib/uncompress.c b/lib/uncompress.c
index 71ac882b87fe..bfe042fcf83e 100644
--- a/lib/uncompress.c
+++ b/lib/uncompress.c
@@ -185,30 +185,26 @@ int uncompress_buf_to_fd(const void *input, size_t input_len,
ssize_t uncompress_buf_to_buf(const void *input, size_t input_len,
void **buf, void(*error_fn)(char *x))
{
- char *dstpath;
size_t size;
- int outfd, ret;
+ int fd, ret;
+ void *p;
- dstpath = make_temp("data-uncompressed");
- if (!dstpath)
- return -ENOMEM;
+ fd = open("/tmp", O_TMPFILE | O_RDWR);
+ if (fd < 0)
+ return -ENODEV;
- outfd = open(dstpath, O_CREAT | O_WRONLY);
- if (outfd < 0) {
- ret = -ENODEV;
- goto free_temp;
- }
-
- ret = uncompress_buf_to_fd(input, input_len, outfd, error_fn);
+ ret = uncompress_buf_to_fd(input, input_len, fd, error_fn);
if (ret)
- goto close_outfd;
+ goto close_fd;
- *buf = read_file(dstpath, &size);
-close_outfd:
- close(outfd);
- unlink(dstpath);
-free_temp:
- free(dstpath);
+ p = read_fd(fd, &size);
+ if (p)
+ *buf = p;
+ else
+ ret = -errno;
+
+close_fd:
+ close(fd);
return ret ?: size;
}
--
2.39.2
next prev parent reply other threads:[~2023-11-20 8:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-20 8:37 [PATCH RFC 1/3] fs: add open O_TMPFILE support Ahmad Fatoum
2023-11-20 8:37 ` [PATCH RFC 2/3] libfile: implement read_fd counterpart to read_file Ahmad Fatoum
2023-11-20 9:49 ` Yann Sionneau
2023-11-20 10:50 ` Ahmad Fatoum
2023-11-21 7:36 ` Sascha Hauer
2023-11-22 16:09 ` Ahmad Fatoum
2023-11-20 8:37 ` Ahmad Fatoum [this message]
2023-11-20 10:49 ` [PATCH] fixup! " Ahmad Fatoum
2023-11-20 13:32 ` [PATCH RFC 1/3] fs: add open O_TMPFILE support Ahmad Fatoum
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=20231120083750.3967831-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox