Skip to contentSkip to conversation

Covscan: Don't access garbage

PATCH In review Phil Sutter 2 participants 8 messages Aug 21, 2017 – Aug 22, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Covscan: Don't access garbage

This series collects patches from v1 which resolve situations where garbage might be read, either due to missing initialization of variables or accessing data which went out of scope.

Changes since v2:
- Rebased onto current master branch.
- Dropped first patch since it is not a real issue.

Phil Sutter (6):
ipaddress: Avoid accessing uninitialized variable lcl
iplink_can: Prevent overstepping array bounds
ipmaddr: Avoid accessing uninitialized data
ss: Use C99 initializer in netlink_show_one() netem/maketable: Check return value of fstat() tc/q_multiq: Don't pass garbage in TCA_OPTIONS

 ip/ipaddress.c    |  2 +-
 ip/iplink_can.c   |  4 ++--
 ip/ipmaddr.c      |  2 +-
 misc/ss.c         | 13 +++++++------
 netem/maketable.c |  4 ++--
 tc/q_multiq.c     |  2 +-
 6 files changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
2.13.1
#1 lore raw

PATCH v3 1/6 ipaddress: Avoid accessing uninitialized variable lcl

If no address was given, ipaddr_modify() accesses uninitialized data when assigning to req.ifa.ifa_prefixlen.

---
 ip/ipaddress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/ipaddress.c +1 -1
index 4d37c5e045071..c9312f06dbd4d 100644
@@ -1888,7 +1888,7 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
 	char  *lcl_arg = NULL;
 	char  *valid_lftp = NULL;
 	char  *preferred_lftp = NULL;
-	inet_prefix lcl;
+	inet_prefix lcl = {};
 	inet_prefix peer;
 	int local_len = 0;
 	int peer_len = 0;
Signature · 1 line
2.13.1
#2 lore raw

PATCH v3 2/6 iplink_can: Prevent overstepping array bounds

In reply to Phil Sutter

can_state_names array contains at most CAN_STATE_MAX fields, so allowing an index to it to be equal to that number is wrong. While here, also make sure the array is indeed that big so nothing bad happens if CAN_STATE_MAX ever increases.

---
 ip/iplink_can.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ip/iplink_can.c +2 -2
index 5df56b2bbbb3b..2954010fefa22 100644
@@ -251,7 +251,7 @@ static int can_parse_opt(struct link_util *lu, int argc, char **argv,
 	return 0;
 }
 
-static const char *can_state_names[] = {
+static const char *can_state_names[CAN_STATE_MAX] = {
 	[CAN_STATE_ERROR_ACTIVE] = "ERROR-ACTIVE",
 	[CAN_STATE_ERROR_WARNING] = "ERROR-WARNING",
 	[CAN_STATE_ERROR_PASSIVE] = "ERROR-PASSIVE",
@@ -275,7 +275,7 @@ static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 	if (tb[IFLA_CAN_STATE]) {
 		uint32_t state = rta_getattr_u32(tb[IFLA_CAN_STATE]);
 
-		fprintf(f, "state %s ", state <= CAN_STATE_MAX ?
+		fprintf(f, "state %s ", state < CAN_STATE_MAX ?
 			can_state_names[state] : "UNKNOWN");
 	}
 
Signature · 1 line
2.13.1
#3 lore raw

PATCH v3 3/6 ipmaddr: Avoid accessing uninitialized data

In reply to Phil Sutter

Looks like this can only happen if /proc/net/igmp is malformed, but better be sure.

---
 ip/ipmaddr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/ipmaddr.c +1 -1
index 4f726fdd976f1..85a69e779563d 100644
@@ -136,7 +136,7 @@ static void read_igmp(struct ma_info **result_p)
 
 	while (fgets(buf, sizeof(buf), fp)) {
 		struct ma_info *ma;
-		size_t len;
+		size_t len = 0;
 
 		if (buf[0] != '\t') {
 			sscanf(buf, "%d%s", &m.index, m.name);
Signature · 1 line
2.13.1
#4 lore raw

PATCH v3 4/6 ss: Use C99 initializer in netlink_show_one()

In reply to Phil Sutter

This has the additional benefit of initializing st.ino to zero which is used later in is_sctp_assoc() function.

---
 misc/ss.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
misc/ss.c +7 -6
index 10360e5a04ff8..63d12871dd826 100644
@@ -3480,17 +3480,18 @@ static int netlink_show_one(struct filter *f,
 				int rq, int wq,
 				unsigned long long sk, unsigned long long cb)
 {
-	struct sockstat st;
+	struct sockstat st = {
+		.state		= SS_CLOSE,
+		.rq		= rq,
+		.wq		= wq,
+		.local.family	= AF_NETLINK,
+		.remote.family	= AF_NETLINK,
+	};
 
 	SPRINT_BUF(prot_buf) = {};
 	const char *prot_name;
 	char procname[64] = {};
 
-	st.state = SS_CLOSE;
-	st.rq	 = rq;
-	st.wq	 = wq;
-	st.local.family = st.remote.family = AF_NETLINK;
-
 	if (f->f) {
 		st.rport = -1;
 		st.lport = pid;
Signature · 1 line
2.13.1
#5 lore raw

PATCH v3 5/6 netem/maketable: Check return value of fstat()

In reply to Phil Sutter

Otherwise info.st_size may contain garbage.

---
 netem/maketable.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
netem/maketable.c +2 -2
index 6aff927be7040..ad660e7d457f0 100644
@@ -24,8 +24,8 @@ readdoubles(FILE *fp, int *number)
 	int limit;
 	int n=0, i;
 
-	fstat(fileno(fp), &info);
-	if (info.st_size > 0) {
+	if (!fstat(fileno(fp), &info) &&
+	    info.st_size > 0) {
 		limit = 2*info.st_size/sizeof(double);	/* @@ approximate */
 	} else {
 		limit = 10000;
Signature · 1 line
2.13.1
#6 lore raw

PATCH v3 6/6 tc/q_multiq: Don't pass garbage in TCA_OPTIONS

In reply to Phil Sutter

multiq_parse_opt() doesn't change 'opt' at all. So at least make sure it doesn't fill TCA_OPTIONS attribute with garbage from stack.

---
 tc/q_multiq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tc/q_multiq.c +1 -1
index 7823931494563..9c09c9a7748f6 100644
@@ -43,7 +43,7 @@ static void explain(void)
 static int multiq_parse_opt(struct qdisc_util *qu, int argc, char **argv,
 			    struct nlmsghdr *n)
 {
-	struct tc_multiq_qopt opt;
+	struct tc_multiq_qopt opt = {};
 
 	if (argc) {
 		if (strcmp(*argv, "help") == 0) {
Signature · 1 line
2.13.1
#7 lore raw
In reply to Phil Sutter
Quoted 16 lines · On Mon, 21 Aug 2017 11:26:58 +0200 Phil Sutter <[email protected]> wrote:

This series collects patches from v1 which resolve situations where garbage might be read, either due to missing initialization of variables or accessing data which went out of scope.

Changes since v2:
- Rebased onto current master branch.
- Dropped first patch since it is not a real issue.

Phil Sutter (6):
ipaddress: Avoid accessing uninitialized variable lcl
iplink_can: Prevent overstepping array bounds
ipmaddr: Avoid accessing uninitialized data
ss: Use C99 initializer in netlink_show_one() netem/maketable: Check return value of fstat() tc/q_multiq: Don't pass garbage in TCA_OPTIONS

Quoted 8 lines
 ip/ipaddress.c    |  2 +-
 ip/iplink_can.c   |  4 ++--
 ip/ipmaddr.c      |  2 +-
 misc/ss.c         | 13 +++++++------
 netem/maketable.c |  4 ++--
 tc/q_multiq.c     |  2 +-
 6 files changed, 14 insertions(+), 13 deletions(-)

These look fine. Applied.