summaryrefslogtreecommitdiffstats
path: root/cli_classic.c
diff options
context:
space:
mode:
Diffstat (limited to 'cli_classic.c')
-rw-r--r--cli_classic.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/cli_classic.c b/cli_classic.c
index 3dbaec2b5..3160d769e 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -22,6 +22,7 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <getopt.h>
#include "flash.h"
@@ -110,6 +111,18 @@ static int check_filename(char *filename, const char *type)
return 0;
}
+/* Ensure a file is open by means of fstat */
+static bool check_file(FILE *file)
+{
+#ifndef STANDALONE
+ struct stat statbuf;
+
+ if (fstat(fileno(file), &statbuf) < 0)
+ return false;
+#endif /* !STANDALONE */
+ return true;
+}
+
static int parse_wp_range(unsigned int *start, unsigned int *len)
{
char *endptr = NULL, *token = NULL;
@@ -219,7 +232,16 @@ int main(int argc, char *argv[])
struct layout_include_args *include_args = NULL;
char *wp_mode_opt = NULL;
- flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
+ /*
+ * Safety-guard against a user who has (mistakenly) closed
+ * stdout or stderr before exec'ing flashrom. We disable
+ * logging in this case to prevent writing log data to a flash
+ * chip when a flash device gets opened with fd 1 or 2.
+ */
+ if (check_file(stdout) && check_file(stderr)) {
+ flashrom_set_log_callback(
+ (flashrom_log_callback *)&flashrom_print_cb);
+ }
print_version();
print_banner();