aboutsummaryrefslogtreecommitdiffstats
path: root/package/network/utils/layerscape/restool/patches/0001-restool-fix-get_device_file-function.patch
blob: 2745fd02a0ea19725b7c51a3e7f408e13fecb518 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
From 37f0f1550e7822584b858edde416a694fb902236 Mon Sep 17 00:00:00 2001
From: Ioana Ciornei <ioana.ciornei@nxp.com>
Date: Tue, 31 Jul 2018 13:33:20 +0300
Subject: [PATCH] restool: fix get_device_file() function

This patch fixes multiple problems encountered in the
get_device_file() function:
 - The deprecated atoi() function is replaced by strtoul
 - An invalid memory access was being performed by using
 memory from dir->d_name even after closedir(). This is
 fixed by a strdup() on the device filename.
 - Also, error prints now print any relevant error code.

Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
---
 restool.c | 44 ++++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/restool.c b/restool.c
index 7553659..78fd1bf 100644
--- a/restool.c
+++ b/restool.c
@@ -1185,8 +1185,13 @@ out:
 
 static int get_device_file(void)
 {
+	int num_dev_files = 0;
+	struct dirent *dir;
 	int error = 0;
+	char *device;
 	int num_char;
+	long val;
+	DIR *d;
 
 	memset(restool.device_file, '\0', DEV_FILE_SIZE);
 
@@ -1214,10 +1219,6 @@ static int get_device_file(void)
 			goto out;
 		}
 	} else {
-		DIR           *d;
-		struct dirent *dir;
-		int num_dev_files = 0;
-		char *dprc_index;
 
 		d = opendir("/dev");
 		if (!d) {
@@ -1227,26 +1228,34 @@ static int get_device_file(void)
 		}
 		while ((dir = readdir(d)) != NULL) {
 			if (strncmp(dir->d_name, "dprc.", 5) == 0) {
-				dprc_index = &dir->d_name[5];
-				num_dev_files += 1;
+				if (num_dev_files == 0)
+					device = strdup(dir->d_name);
+				num_dev_files++;
 			}
 		}
 		closedir(d);
 
 		if (num_dev_files == 1) {
-			int temp_len = strlen(dprc_index);
+			errno = 0;
+			val = strtoul(&device[5], NULL, 0);
+			if ((errno == ERANGE && val == LONG_MAX) ||
+			    ( errno != 0 && val == 0 )) {
+				ERROR_PRINTF("error: device file malformed\n");
+				error = -1;
+				goto out_free_device;;
+			}
+			restool.root_dprc_id = val;
 
-			temp_len += 10;
-			num_char = sprintf(restool.device_file, "/dev/dprc.%s",
-					   dprc_index);
-			if (num_char != temp_len) {
-				ERROR_PRINTF("sprintf error\n");
+			num_char = snprintf(restool.device_file, DEV_FILE_SIZE,
+					    "/dev/dprc.%d", restool.root_dprc_id);
+			if (num_char < 0 || num_char >= DEV_FILE_SIZE) {
+				ERROR_PRINTF("error: device file malformed\n");
 				error = -1;
-				goto out;
+				goto out_free_device;
 			}
-			restool.root_dprc_id = atoi(dprc_index);
-			if (access(restool.device_file, F_OK) != 0)
-				printf("no such dev file\n");
+			error = access(restool.device_file, F_OK);
+			if (error != 0)
+				ERROR_PRINTF("error: access(%s) = %d\n", restool.device_file, error);
 		} else {
 			error = -1;
 			if (num_dev_files == 0)
@@ -1255,6 +1264,9 @@ static int get_device_file(void)
 				ERROR_PRINTF("error: multiple root containers\n");
 		}
 	}
+
+out_free_device:
+	free(device);
 out:
 	return error;
 }
-- 
2.17.1