netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Decotigny <ddecotig@gmail.com>
To: netdev@vger.kernel.org
Cc: Jeff Garzik <jgarzik@pobox.com>,
	Ben Hutchings <ben@decadent.org.uk>,
	David Miller <davem@redhat.com>,
	Vidya Sagar Ravipati <vidya@cumulusnetworks.com>,
	Joe Perches <joe@perches.com>,
	David Decotigny <decot@googlers.com>
Subject: [ethtool PATCH v3 04/12] ethtool.c: do_seeprom checks for params & stdin sanity
Date: Fri,  4 Mar 2016 16:42:05 -0800	[thread overview]
Message-ID: <1457138533-2417-5-git-send-email-ddecotig@gmail.com> (raw)
In-Reply-To: <1457138533-2417-1-git-send-email-ddecotig@gmail.com>

From: David Decotigny <decot@googlers.com>

Tested:
  On qemu e1000:
  $ dd if=/dev/zero bs=2 count=5 | /mnt/ethtool -E eth0 length 9
  too much data from stdin
  $ dd if=/dev/zero bs=2 count=5 | /mnt/ethtool -E eth0 length 11
  not enough data from stdin
  $ dd if=/dev/zero bs=2 count=5 | /mnt/ethtool -E eth0 length 10
  Cannot set EEPROM data: Bad address


Signed-off-by: David Decotigny <decot@googlers.com>
---
 ethtool.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/ethtool.c b/ethtool.c
index c64b962..19f479c 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -2828,8 +2828,14 @@ static int do_seeprom(struct cmd_context *ctx)
 	if (seeprom_length == -1)
 		seeprom_length = drvinfo.eedump_len;
 
-	if (drvinfo.eedump_len < seeprom_offset + seeprom_length)
-		seeprom_length = drvinfo.eedump_len - seeprom_offset;
+	if (drvinfo.eedump_len < seeprom_offset + seeprom_length) {
+		if (drvinfo.eedump_len > seeprom_offset)
+			seeprom_length = drvinfo.eedump_len - seeprom_offset;
+		else {
+			fprintf(stderr, "offset & length out of bounds\n");
+			return 75;
+		}
+	}
 
 	eeprom = calloc(1, sizeof(*eeprom)+seeprom_length);
 	if (!eeprom) {
@@ -2844,8 +2850,18 @@ static int do_seeprom(struct cmd_context *ctx)
 	eeprom->data[0] = seeprom_value;
 
 	/* Multi-byte write: read input from stdin */
-	if (!seeprom_value_seen)
-		eeprom->len = fread(eeprom->data, 1, eeprom->len, stdin);
+	if (!seeprom_value_seen) {
+		if (1 != fread(eeprom->data, eeprom->len, 1, stdin)) {
+			fprintf(stderr, "not enough data from stdin\n");
+			free(eeprom);
+			return 75;
+		}
+		if ((EOF != fgetc(stdin)) || !feof(stdin)) {
+			fprintf(stderr, "too much data from stdin\n");
+			free(eeprom);
+			return 75;
+		}
+	}
 
 	err = send_ioctl(ctx, eeprom);
 	if (err < 0) {
-- 
2.7.0.rc3.207.g0ac5344

  parent reply	other threads:[~2016-03-05  0:42 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-05  0:42 [ethtool PATCH v3 00/12] add support for new ETHTOOL_xLINKSETTINGS ioctls David Decotigny
2016-03-05  0:42 ` [ethtool PATCH v3 01/12] internal.h: change to new sane powerpc64 kernel headers David Decotigny
2016-03-07 15:53   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 02/12] ethtool.c: don't ignore fread() return value David Decotigny
2016-03-07 15:55   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 03/12] ethtool.c: fix dump_regs heap corruption David Decotigny
2016-03-07 16:02   ` Ben Hutchings
2016-03-05  0:42 ` David Decotigny [this message]
2016-03-07 16:05   ` [ethtool PATCH v3 04/12] ethtool.c: do_seeprom checks for params & stdin sanity Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 05/12] marvell.c: fix strict alias warnings David Decotigny
2016-03-07 16:06   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 06/12] test-common.c: fix test_realloc(NULL, ...) David Decotigny
2016-03-07 16:07   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 07/12] test-features.c: add braces around array initialization David Decotigny
2016-03-07 16:08   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 08/12] ethtool-copy.h: sync with net-next David Decotigny
2016-03-07 16:10   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 09/12] internal.h: fix build for latest ethtool-copy.h David Decotigny
2016-03-07 16:11   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 10/12] internal.h: TRUE/FALSE macros David Decotigny
2016-03-07 16:12   ` Ben Hutchings
2016-03-05  0:42 ` [ethtool PATCH v3 11/12] ethtool.c: add support for ETHTOOL_xLINKSETTINGS ioctls David Decotigny
2016-03-05  0:42 ` [ethtool PATCH v3 12/12] ethtool.c: use v6 socket when v4 is not available David Decotigny
2016-03-07 16:13   ` Ben Hutchings

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1457138533-2417-5-git-send-email-ddecotig@gmail.com \
    --to=ddecotig@gmail.com \
    --cc=ben@decadent.org.uk \
    --cc=davem@redhat.com \
    --cc=decot@googlers.com \
    --cc=jgarzik@pobox.com \
    --cc=joe@perches.com \
    --cc=netdev@vger.kernel.org \
    --cc=vidya@cumulusnetworks.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).