Skip to content

Commit 6ca381c

Browse files
committed
dmidecode: Do not let --dump-bin overwrite an existing file
Make sure that the file passed to option --dump-bin does not already exist. In practice, it is rather unlikely that an honest user would want to overwrite an existing dump file, while this possibility could be used by a rogue user to corrupt a system file. Signed-off-by: Jean Delvare <jdelvare@suse.de> Reviewed-by: Jerry Hoemann <jerry.hoemann@hpe.com>
1 parent d8cfbc8 commit 6ca381c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

dmidecode.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* https://www.dmtf.org/sites/default/files/DSP0270_1.0.1.pdf
6161
*/
6262

63+
#include <fcntl.h>
6364
#include <stdio.h>
6465
#include <string.h>
6566
#include <strings.h>
@@ -5412,13 +5413,22 @@ static void dmi_table_string(const struct dmi_header *h, const u8 *data, u16 ver
54125413
static int dmi_table_dump(const u8 *ep, u32 ep_len, const u8 *table,
54135414
u32 table_len)
54145415
{
5416+
int fd;
54155417
FILE *f;
54165418

5417-
f = fopen(opt.dumpfile, "wb");
5419+
fd = open(opt.dumpfile, O_WRONLY|O_CREAT|O_EXCL, 0666);
5420+
if (fd == -1)
5421+
{
5422+
fprintf(stderr, "%s: ", opt.dumpfile);
5423+
perror("open");
5424+
return -1;
5425+
}
5426+
5427+
f = fdopen(fd, "wb");
54185428
if (!f)
54195429
{
54205430
fprintf(stderr, "%s: ", opt.dumpfile);
5421-
perror("fopen");
5431+
perror("fdopen");
54225432
return -1;
54235433
}
54245434

man/dmidecode.8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH DMIDECODE 8 "January 2019" "dmidecode"
1+
.TH DMIDECODE 8 "February 2023" "dmidecode"
22
.\"
33
.SH NAME
44
dmidecode \- \s-1DMI\s0 table decoder
@@ -164,6 +164,7 @@ hexadecimal and \s-1ASCII\s0. This option is mainly useful for debugging.
164164
Do not decode the entries, instead dump the DMI data to a file in binary
165165
form. The generated file is suitable to pass to \fB--from-dump\fP
166166
later.
167+
\fIFILE\fP must not exist.
167168
.TP
168169
.BR " " " " "--from-dump \fIFILE\fP"
169170
Read the DMI data from a binary file previously generated using

0 commit comments

Comments
 (0)