Skip to contentSkip to conversation

misc: xilinx_sdfec: Fix a couple small information leaks

PATCH In review Dan Carpenter 3 participants 15 messages Aug 21, 2019 – Aug 23, 2019

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

PATCH 1/4 misc: xilinx_sdfec: Fix a couple small information leaks

These structs have holes in them so we end up disclosing a few bytes of uninitialized stack data.

drivers/misc/xilinx_sdfec.c:305 xsdfec_get_status() warn: check that 'status' doesn't leak information (struct has a hole after 'activity') drivers/misc/xilinx_sdfec.c:449 xsdfec_get_turbo() warn: check that 'turbo_params' doesn't leak information (struct has a hole after 'scale')

We need to zero out the holes with memset().

Fixes 6bd6a690c2e7 ("misc: xilinx_sdfec: Add stats & status ioctls")
---
 drivers/misc/xilinx_sdfec.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/xilinx_sdfec.c +2 -0
index 912e939dec62..dc1b8b412712 100644
@@ -295,6 +295,7 @@ static int xsdfec_get_status(struct xsdfec_dev *xsdfec, void __user *arg)
 	struct xsdfec_status status;
 	int err;
 
+	memset(&status, 0, sizeof(status));
 	spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
 	status.state = xsdfec->state;
 	xsdfec->state_updated = false;
@@ -440,6 +441,7 @@ static int xsdfec_get_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (xsdfec->config.code = XSDFEC_LDPC_CODE)
 		return -EIO;
 
+	memset(&turbo_params, 0, sizeof(turbo_params));
 	reg_value = xsdfec_regread(xsdfec, XSDFEC_TURBO_ADDR);
 
 	turbo_params.scale = (reg_value & XSDFEC_TURBO_SCALE_MASK) >>
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/4 misc: xilinx_sdfec: Return -EFAULT if copy_from_user() fails

The copy_from_user() funciton returns the number of bytes remaining to be copied but we want to return -EFAULT to the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/misc/xilinx_sdfec.c +3 -2
index dc1b8b412712..813b82c59360 100644
@@ -651,9 +651,10 @@ static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (!ldpc)
 		return -ENOMEM;
 
-	ret = copy_from_user(ldpc, arg, sizeof(*ldpc));
-	if (ret)
+	if (copy_from_user(ldpc, arg, sizeof(*ldpc))) {
+		ret = -EFAULT;
 		goto err_out;
+	}
 
 	if (xsdfec->config.code = XSDFEC_TURBO_CODE) {
 		ret = -EIO;
Signature · 1 line
2.20.1
Reviewed-by on v1 2/4
#2 lore raw

Re: [PATCH 2/4] misc: xilinx_sdfec: Return -EFAULT if copy_from_user() fails

Quoted 1 line · Dan Carpenter

The copy_from_user() funciton returns the number of bytes remaining to

typo here.

Quoted 16 lines

be copied but we want to return -EFAULT to the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/misc/xilinx_sdfec.c +0 -0
index dc1b8b412712..813b82c59360 100644
@@ -651,9 +651,10 @@ static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (!ldpc)
 		return -ENOMEM;

drivers/misc/xilinx_sdfec.c static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg) · line 660

Quoted 10 lines
drivers/misc/xilinx_sdfec.c +3 -2
 
-	ret = copy_from_user(ldpc, arg, sizeof(*ldpc));
-	if (ret)
+	if (copy_from_user(ldpc, arg, sizeof(*ldpc))) {
+		ret = -EFAULT;
 		goto err_out;
+	}
 
 	if (xsdfec->config.code = XSDFEC_TURBO_CODE) {
 		ret = -EIO;

When typo fixed feel free to add my

Thanks,
Michal

#3 lore raw

PATCH v2 2/4 misc: xilinx_sdfec: Return -EFAULT if copy_from_user() fails

In reply to Dan Carpenter

The copy_from_user() function returns the number of bytes remaining to be copied but we want to return -EFAULT to the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")

---
v2: Fix a typo in the commit message. funciton --> function

 drivers/misc/xilinx_sdfec.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/misc/xilinx_sdfec.c +3 -2
index dc1b8b412712..813b82c59360 100644
@@ -651,9 +651,10 @@ static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (!ldpc)
 		return -ENOMEM;
 
-	ret = copy_from_user(ldpc, arg, sizeof(*ldpc));
-	if (ret)
+	if (copy_from_user(ldpc, arg, sizeof(*ldpc))) {
+		ret = -EFAULT;
 		goto err_out;
+	}
 
 	if (xsdfec->config.code = XSDFEC_TURBO_CODE) {
 		ret = -EIO;
Signature · 1 line
2.20.1
Reviewed-by on v1 2/4
#4 lore raw

RE: [PATCH 2/4] misc: xilinx_sdfec: Return -EFAULT if copy_from_user() fails

In reply to Dan Carpenter

Hi Dan,

Quoted 35 lines

-----Original Message-----
From: Dan Carpenter [mailto:[email protected]]
Sent: Wednesday 21 August 2019 08:07
To: Derek Kiernan <[email protected]>; Dragan Cvetic <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Greg Kroah-Hartman <[email protected]>; Michal Simek <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH 2/4] misc: xilinx_sdfec: Return -EFAULT if copy_from_user() fails

The copy_from_user() funciton returns the number of bytes remaining to be copied but we want to return -EFAULT to the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/misc/xilinx_sdfec.c +3 -3
index dc1b8b412712..813b82c59360 100644
@@ -651,9 +651,10 @@ static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (!ldpc)
 		return -ENOMEM;
 
-	ret = copy_from_user(ldpc, arg, sizeof(*ldpc));
-	if (ret)
+	if (copy_from_user(ldpc, arg, sizeof(*ldpc))) {
+		ret = -EFAULT;
 		goto err_out;
+	}
 
 	if (xsdfec->config.code == XSDFEC_TURBO_CODE) {
 		ret = -EIO;
--
Quoted 1 line

2.20.1

Thanks,
Dragan

#5 lore raw

PATCH 3/4 misc: xilinx_sdfec: Prevent a divide by zero in xsdfec_reg0_write()

In reply to Dan Carpenter

The "psize" value comes from the user so we need to verify that it's non-zero before we check if "n % psize" or it will crash.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")

---
The parentheses in this condition are a no-op. They're just confusing. Perhaps something else was intended?

 drivers/misc/xilinx_sdfec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +1 -1
index 813b82c59360..3fc53d20abf3 100644
@@ -460,7 +460,7 @@ static int xsdfec_reg0_write(struct xsdfec_dev *xsdfec, u32 n, u32 k, u32 psize,
 {
 	u32 wdata;
 
-	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX ||
+	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX || psize = 0 ||
 	    (n > XSDFEC_REG0_N_MUL_P * psize) || n <= k || ((n % psize) != 0)) {
 		dev_dbg(xsdfec->dev, "N value is not in range");
 		return -EINVAL;
Signature · 1 line
2.20.1
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] misc: xilinx_sdfec: Prevent a divide by zero in xsdfec_reg0_write()

Quoted 18 lines · Dan Carpenter

The "psize" value comes from the user so we need to verify that it's non-zero before we check if "n % psize" or it will crash.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")

---
The parentheses in this condition are a no-op. They're just confusing. Perhaps something else was intended?

 drivers/misc/xilinx_sdfec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +0 -0
index 813b82c59360..3fc53d20abf3 100644

drivers/misc/xilinx_sdfec.c static int xsdfec_reg0_write(struct xsdfec_dev *xsdfec, u32 n, u32 k, u32 psize, · line 466

Quoted 8 lines
drivers/misc/xilinx_sdfec.c +1 -1
@@ -460,7 +460,7 @@ static int xsdfec_reg0_write(struct xsdfec_dev *xsdfec, u32 n, u32 k, u32 psize,
 {
 	u32 wdata;
 
-	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX ||
+	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX || psize = 0 ||
 	    (n > XSDFEC_REG0_N_MUL_P * psize) || n <= k || ((n % psize) != 0)) {
 		dev_dbg(xsdfec->dev, "N value is not in range");
 		return -EINVAL;

Thanks,
Michal

Reviewed-by on v1 3/4
#7 lore raw

RE: [PATCH 3/4] misc: xilinx_sdfec: Prevent a divide by zero in xsdfec_reg0_write()

In reply to Dan Carpenter

Hi Dan,

Quoted 34 lines

-----Original Message-----
From: Dan Carpenter [mailto:[email protected]]
Sent: Wednesday 21 August 2019 08:10
To: Derek Kiernan <[email protected]>; Dragan Cvetic <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Greg Kroah-Hartman <[email protected]>; Michal Simek <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH 3/4] misc: xilinx_sdfec: Prevent a divide by zero in xsdfec_reg0_write()

The "psize" value comes from the user so we need to verify that it's non-zero before we check if "n % psize" or it will crash.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")

---
The parentheses in this condition are a no-op. They're just confusing. Perhaps something else was intended?

 drivers/misc/xilinx_sdfec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +1 -2
index 813b82c59360..3fc53d20abf3 100644
@@ -460,7 +460,7 @@ static int xsdfec_reg0_write(struct xsdfec_dev *xsdfec, u32 n, u32 k, u32 psize,
 {
 	u32 wdata;
 
-	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX ||
+	if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX || psize == 0 ||
 	    (n > XSDFEC_REG0_N_MUL_P * psize) || n <= k || ((n % psize) != 0)) {
 		dev_dbg(xsdfec->dev, "N value is not in range");
 		return -EINVAL;
--
Quoted 1 line

2.20.1

Thanks,
Dragan

#8 lore raw

PATCH 4/4 misc: xilinx_sdfec: Prevent integer overflow in xsdfec_table_write()

In reply to Dan Carpenter

The checking here needs to handle integer overflows because "offset" and "len" come from the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +3 -1
index 3fc53d20abf3..0bf3bcc8e1ef 100644
@@ -611,7 +611,9 @@ static int xsdfec_table_write(struct xsdfec_dev *xsdfec, u32 offset,
 	 * Writes that go beyond the length of
 	 * Shared Scale(SC) table should fail
 	 */
-	if ((XSDFEC_REG_WIDTH_JUMP * (offset + len)) > depth) {
+	if (offset > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    len > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    offset + len > depth / XSDFEC_REG_WIDTH_JUMP) {
 		dev_dbg(xsdfec->dev, "Write exceeds SC table length");
 		return -EINVAL;
 	}
Signature · 1 line
2.20.1
Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] misc: xilinx_sdfec: Prevent integer overflow in xsdfec_table_write()

Quoted 15 lines · Dan Carpenter

The checking here needs to handle integer overflows because "offset" and "len" come from the user.

Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +0 -0
index 3fc53d20abf3..0bf3bcc8e1ef 100644

drivers/misc/xilinx_sdfec.c static int xsdfec_table_write(struct xsdfec_dev *xsdfec, u32 offset, · line 619

Quoted 10 lines
drivers/misc/xilinx_sdfec.c +3 -1
@@ -611,7 +611,9 @@ static int xsdfec_table_write(struct xsdfec_dev *xsdfec, u32 offset,
 	 * Writes that go beyond the length of
 	 * Shared Scale(SC) table should fail
 	 */
-	if ((XSDFEC_REG_WIDTH_JUMP * (offset + len)) > depth) {
+	if (offset > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    len > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    offset + len > depth / XSDFEC_REG_WIDTH_JUMP) {
 		dev_dbg(xsdfec->dev, "Write exceeds SC table length");
 		return -EINVAL;
 	}

Thanks,
Michal

Reviewed-by on v1 4/4
#10 lore raw

RE: [PATCH 4/4] misc: xilinx_sdfec: Prevent integer overflow in xsdfec_table_write()

In reply to Dan Carpenter

Hi Dan,

Quoted 10 lines

-----Original Message-----
From: Dan Carpenter [mailto:[email protected]]
Sent: Wednesday 21 August 2019 08:11
To: Derek Kiernan <[email protected]>; Dragan Cvetic <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Greg Kroah-Hartman <[email protected]>; Michal Simek <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH 4/4] misc: xilinx_sdfec: Prevent integer overflow in xsdfec_table_write()

The checking here needs to handle integer overflows because "offset" and "len" come from the user.

Good catch, thanks.

Quoted 23 lines
Fixes 20ec628e8007 ("misc: xilinx_sdfec: Add ability to configure LDPC")
---
 drivers/misc/xilinx_sdfec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/misc/xilinx_sdfec.c +3 -2
index 3fc53d20abf3..0bf3bcc8e1ef 100644
@@ -611,7 +611,9 @@ static int xsdfec_table_write(struct xsdfec_dev *xsdfec, u32 offset,
 	 * Writes that go beyond the length of
 	 * Shared Scale(SC) table should fail
 	 */
-	if ((XSDFEC_REG_WIDTH_JUMP * (offset + len)) > depth) {
+	if (offset > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    len > depth / XSDFEC_REG_WIDTH_JUMP ||
+	    offset + len > depth / XSDFEC_REG_WIDTH_JUMP) {
 		dev_dbg(xsdfec->dev, "Write exceeds SC table length");
 		return -EINVAL;
 	}
--
Quoted 1 line

2.20.1

Thanks
Dragan

Reviewed-by on v1 1/4
#11 lore raw
In reply to Dan Carpenter

Hi Dan,

Quoted 5 lines · Dan Carpenter

These structs have holes in them so we end up disclosing a few bytes of uninitialized stack data.

drivers/misc/xilinx_sdfec.c:305 xsdfec_get_status() warn: check that 'status' doesn't leak information (struct has a hole after 'activity') drivers/misc/xilinx_sdfec.c:449 xsdfec_get_turbo() warn: check that 'turbo_params' doesn't leak information (struct has a hole after 'scale')

Who is generating these warnings? Is this any new GCC or different tool? I see that 3byte padding but never seen these warnings.

Quoted 22 lines

We need to zero out the holes with memset().

Fixes 6bd6a690c2e7 ("misc: xilinx_sdfec: Add stats & status ioctls")
---
 drivers/misc/xilinx_sdfec.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/xilinx_sdfec.c +1 -0
index 912e939dec62..dc1b8b412712 100644
@@ -295,6 +295,7 @@ static int xsdfec_get_status(struct xsdfec_dev *xsdfec, void __user *arg)
 	struct xsdfec_status status;
 	int err;
 
+	memset(&status, 0, sizeof(status));
 	spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
 	status.state = xsdfec->state;
 	xsdfec->state_updated = false;

drivers/misc/xilinx_sdfec.c static int xsdfec_get_turbo(struct xsdfec_dev *xsdfec, void __user *arg) · line 447

Quoted 7 lines
drivers/misc/xilinx_sdfec.c +1 -0
@@ -440,6 +441,7 @@ static int xsdfec_get_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (xsdfec->config.code = XSDFEC_LDPC_CODE)
 		return -EIO;
 
+	memset(&turbo_params, 0, sizeof(turbo_params));
 	reg_value = xsdfec_regread(xsdfec, XSDFEC_TURBO_ADDR);
 
 	turbo_params.scale = (reg_value & XSDFEC_TURBO_SCALE_MASK) >>

Thanks,
Michal

#12 lore raw
Quoted 11 lines · Michal Simek

Hi Dan,

Quoted 5 lines · Dan Carpenter

These structs have holes in them so we end up disclosing a few bytes of uninitialized stack data.

drivers/misc/xilinx_sdfec.c:305 xsdfec_get_status() warn: check that 'status' doesn't leak information (struct has a hole after 'activity') drivers/misc/xilinx_sdfec.c:449 xsdfec_get_turbo() warn: check that 'turbo_params' doesn't leak information (struct has a hole after 'scale')

Who is generating these warnings? Is this any new GCC or different tool? I see that 3byte padding but never seen these warnings.

This is a Smatch check.

regards,
dan carpenter

#13 lore raw
Quoted 7 lines · Dan Carpenter
Quoted 6 lines · Michal Simek

Hi Dan,

Quoted 3 lines · Dan Carpenter

These structs have holes in them so we end up disclosing a few bytes of uninitialized stack data.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

drivers/misc/xilinx_sdfec.c:305 xsdfec_get_status() warn: check that 'status' doesn't leak information (struct has a hole after 'activity') drivers/misc/xilinx_sdfec.c:449 xsdfec_get_turbo() warn: check that 'turbo_params' doesn't leak information (struct has a hole after 'scale')

Who is generating these warnings? Is this any new GCC or different tool? I see that 3byte padding but never seen these warnings.

This is a Smatch check.

ok. It looks like I need to update it to latest version. My version is not showing these.

Anyway thanks for patches,
Michal

Reviewed-by on v1 1/4
#14 lore raw
In reply to Dan Carpenter

Hi Dan,

Quoted 44 lines

-----Original Message-----
From: Dan Carpenter [mailto:[email protected]]
Sent: Wednesday 21 August 2019 08:06
To: Derek Kiernan <[email protected]>; Dragan Cvetic <[email protected]>
Cc: Arnd Bergmann <[email protected]>; Greg Kroah-Hartman <[email protected]>; Michal Simek <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: [PATCH 1/4] misc: xilinx_sdfec: Fix a couple small information leaks

These structs have holes in them so we end up disclosing a few bytes of uninitialized stack data.

drivers/misc/xilinx_sdfec.c:305 xsdfec_get_status() warn: check that 'status' doesn't leak information (struct has a hole after 'activity') drivers/misc/xilinx_sdfec.c:449 xsdfec_get_turbo() warn: check that 'turbo_params' doesn't leak information (struct has a hole after 'scale')

We need to zero out the holes with memset().

Fixes 6bd6a690c2e7 ("misc: xilinx_sdfec: Add stats & status ioctls")
---
 drivers/misc/xilinx_sdfec.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/misc/xilinx_sdfec.c +2 -1
index 912e939dec62..dc1b8b412712 100644
@@ -295,6 +295,7 @@ static int xsdfec_get_status(struct xsdfec_dev *xsdfec, void __user *arg)
 	struct xsdfec_status status;
 	int err;
 
+	memset(&status, 0, sizeof(status));
 	spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
 	status.state = xsdfec->state;
 	xsdfec->state_updated = false;
@@ -440,6 +441,7 @@ static int xsdfec_get_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
 	if (xsdfec->config.code == XSDFEC_LDPC_CODE)
 		return -EIO;
 
+	memset(&turbo_params, 0, sizeof(turbo_params));
 	reg_value = xsdfec_regread(xsdfec, XSDFEC_TURBO_ADDR);
 
 	turbo_params.scale = (reg_value & XSDFEC_TURBO_SCALE_MASK) >>
--
Quoted 1 line

2.20.1

Thanks,
Dragan