Message ID | bce65520-7f11-49f5-c806-59e508001321@the2masters.de (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
> But: On the 27th block is a large file (1,5kB) without 0 bytes inside. > The code in qnap_tsx09_find_mac_addr() maps 1kB into memory (not a whole > file or the whole 384kB) and then calls qnap_tsx09_check_mac_addr() -> > mac_pton() -> strlen() on this memory block. as there is no 0 byte in the > file on the 27th block, strlen runs into bad memory and the machine panics. > The old code had no strlen(). Yes, that sounds look a good explanation. > I changed mac_pton() to use strnlen(), and now the panic is gone. I don't > know why strlen is actually needed in mac_pton. The string is checked in the > following loop, if there is a zero byte somewhere, the loop will be returned > immediately. So I think the strlen() superfluous. Is the following patch > correct? The patch has been corrupted by you email client. But otherwise, yes. Please take a look at: https://www.kernel.org/doc/html/v4.12/process/submitting-patches.html It will give you hits about correctly formatting the patch. In addition it should have: Fixes: 4cd5773a2ae6 ("net: core: move mac_pton() to lib/net_utils.c") before the --- line, to indicate what it is fixing. This patch should be against git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git, since it is a fix, and sent to <netdev@vger.kernel.org>. Thanks Andrew
On Fri, 2018-02-23 at 00:34 +0100, Andrew Lunn wrote: > > But: On the 27th block is a large file (1,5kB) without 0 bytes > > inside. > > The code in qnap_tsx09_find_mac_addr() maps 1kB into memory (not a > > whole > > file or the whole 384kB) and then calls qnap_tsx09_check_mac_addr() > > -> > > mac_pton() -> strlen() on this memory block. as there is no 0 byte > > in the > > file on the 27th block, strlen runs into bad memory and the machine > > panics. > > The old code had no strlen(). > > Yes, that sounds look a good explanation. > > > I changed mac_pton() to use strnlen(), and now the panic is gone. I > > don't > > know why strlen is actually needed in mac_pton. The string is > > checked in the > > following loop, if there is a zero byte somewhere, the loop will be > > returned > > immediately. So I think the strlen() superfluous. Is the following > > patch > > correct? > > The patch has been corrupted by you email client. But otherwise, yes. > > Please take a look at: > > https://www.kernel.org/doc/html/v4.12/process/submitting-patches.html > > It will give you hits about correctly formatting the patch. In > addition it should have: > > Fixes: 4cd5773a2ae6 ("net: core: move mac_pton() to lib/net_utils.c") > > before the --- line, to indicate what it is fixing. > > This patch should be against > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git, > since it is a fix, and sent to <netdev@vger.kernel.org>. Guys, consider this one instead: https://patchwork.ozlabs.org/patch/851008/
> > The patch has been corrupted by you email client. But otherwise, yes. > > > > Please take a look at: > > > > https://www.kernel.org/doc/html/v4.12/process/submitting-patches.html > > > > It will give you hits about correctly formatting the patch. In > > addition it should have: > > > > Fixes: 4cd5773a2ae6 ("net: core: move mac_pton() to lib/net_utils.c") > > > > before the --- line, to indicate what it is fixing. > > > > This patch should be against > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git, > > since it is a fix, and sent to <netdev@vger.kernel.org>. > > Guys, consider this one instead: > https://patchwork.ozlabs.org/patch/851008/ Hi Andy Thanks for pointing this patch out. What is the advantage of doing to the strnlen()? As Stefan says, the code which follows will detect a short string, in that a NULL is not in [0-9a-f], nor a : . Thanks Andrew
+Cc Alexey On Fri, 2018-02-23 at 16:01 +0100, Andrew Lunn wrote: > > > The patch has been corrupted by you email client. But otherwise, > > > yes. > > > > > > Please take a look at: > > > > > > https://www.kernel.org/doc/html/v4.12/process/submitting-patches.h > > > tml > > > > > > It will give you hits about correctly formatting the patch. In > > > addition it should have: > > > > > > Fixes: 4cd5773a2ae6 ("net: core: move mac_pton() to > > > lib/net_utils.c") > > > > > > before the --- line, to indicate what it is fixing. > > > > > > This patch should be against > > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git, > > > since it is a fix, and sent to <netdev@vger.kernel.org>. > > > > Guys, consider this one instead: > > https://patchwork.ozlabs.org/patch/851008/ > > Hi Andy > > Thanks for pointing this patch out. > > What is the advantage of doing to the strnlen()? As Stefan says, the > code which follows will detect a short string, in that a NULL is not > in [0-9a-f], nor a : . I'm not sure, but my understanding is that, the strchr() call in the original code or isxdigit() in the follow up change will trash a cache a bit. Besides that some of the users are (often?) supplying empty strings to convert from, and in this case makes sense to bail out fast. Alexey, can you shed a light here?
> > Hi Andy > > > > Thanks for pointing this patch out. > > > > What is the advantage of doing to the strnlen()? As Stefan says, the > > code which follows will detect a short string, in that a NULL is not > > in [0-9a-f], nor a : . > > I'm not sure, but my understanding is that, the strchr() call in the > original code or isxdigit() in the follow up change will trash a cache a > bit. Besides that some of the users are (often?) supplying empty strings > to convert from, and in this case makes sense to bail out fast. Is this function being called on a hot path? In the case which is crashing, it is during early boot, and it gets called ~ 40 times, in quick succession. The first call to isxdigit() will need to fetch part of the _ctype array into cache, but since the caller is only walking memory, i hope it is still in cache for the next call to mac_pton(). Andrew
2018-02-23 16:51 GMT+01:00 Andrew Lunn <andrew@lunn.ch>: >> > Hi Andy >> > >> > Thanks for pointing this patch out. >> > >> > What is the advantage of doing to the strnlen()? As Stefan says, the >> > code which follows will detect a short string, in that a NULL is not >> > in [0-9a-f], nor a : . >> >> I'm not sure, but my understanding is that, the strchr() call in the >> original code or isxdigit() in the follow up change will trash a cache a >> bit. Besides that some of the users are (often?) supplying empty strings >> to convert from, and in this case makes sense to bail out fast. > > Is this function being called on a hot path? In the case which is > crashing, it is during early boot, and it gets called ~ 40 times, in > quick succession. The first call to isxdigit() will need to fetch part > of the _ctype array into cache, but since the caller is only walking > memory, i hope it is still in cache for the next call to mac_pton(). > > Andrew In my case mac_pton is not called on a hot path, the slow sata disks in the NAS dominate the boot time. For me code size matters, and on my device it's rarely called on a zero string. It's probably slower with the strnlen as without it. I think if there are users out there calling it in a hot path on a zero string, they should check the zero string themselves, this is not the common use case. Should I send a patch to netdev, can I get some Acked-by: ? Stefan
On Fri, 2018-02-23 at 17:36 +0100, Stefan Hellermann wrote: > 2018-02-23 16:51 GMT+01:00 Andrew Lunn <andrew@lunn.ch>: > > > > Hi Andy > > > > > > > > Thanks for pointing this patch out. > > > > > > > > What is the advantage of doing to the strnlen()? As Stefan says, > > > > the > > > > code which follows will detect a short string, in that a NULL is > > > > not > > > > in [0-9a-f], nor a : . > > > > > > I'm not sure, but my understanding is that, the strchr() call in > > > the > > > original code or isxdigit() in the follow up change will trash a > > > cache a > > > bit. Besides that some of the users are (often?) supplying empty > > > strings > > > to convert from, and in this case makes sense to bail out fast. > > > > Is this function being called on a hot path? In the case which is > > crashing, it is during early boot, and it gets called ~ 40 times, in > > quick succession. The first call to isxdigit() will need to fetch > > part > > of the _ctype array into cache, but since the caller is only walking > > memory, i hope it is still in cache for the next call to mac_pton(). > > > > Andrew > > In my case mac_pton is not called on a hot path, the slow sata disks > in the NAS dominate the boot time. For me code size matters, and on my > device it's rarely called on a zero string. It's probably slower with > the strnlen as without it. I think if there are users out there > calling it in a hot path on a zero string, they should check the zero > string themselves, this is not the common use case. > > Should I send a patch to netdev, can I get some Acked-by: ? Not from me, sorry. Alexey would be best person to give a such. Please, Cc him.
> Should I send a patch to netdev, can I get some Acked-by: ?
Please do. I will ack it, if it follows all the usual rules, not
corrupt, etc.
Andrew
On Fri, Feb 23, 2018 at 05:18:48PM +0200, Andy Shevchenko wrote: > +Cc Alexey > > On Fri, 2018-02-23 at 16:01 +0100, Andrew Lunn wrote: > > > > The patch has been corrupted by you email client. But otherwise, > > > > yes. > > > > > > > > Please take a look at: > > > > > > > > https://www.kernel.org/doc/html/v4.12/process/submitting-patches.h > > > > tml > > > > > > > > It will give you hits about correctly formatting the patch. In > > > > addition it should have: > > > > > > > > Fixes: 4cd5773a2ae6 ("net: core: move mac_pton() to > > > > lib/net_utils.c") > > > > > > > > before the --- line, to indicate what it is fixing. > > > > > > > > This patch should be against > > > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git, > > > > since it is a fix, and sent to <netdev@vger.kernel.org>. > > > > > > Guys, consider this one instead: > > > https://patchwork.ozlabs.org/patch/851008/ > > > > Hi Andy > > > > Thanks for pointing this patch out. > > > > What is the advantage of doing to the strnlen()? As Stefan says, the > > code which follows will detect a short string, in that a NULL is not > > in [0-9a-f], nor a : . > > I'm not sure, but my understanding is that, the strchr() call in the > original code or isxdigit() in the follow up change will trash a cache a > bit. Besides that some of the users are (often?) supplying empty strings > to convert from, and in this case makes sense to bail out fast. > > Alexey, can you shed a light here? I went for the simplest code. map_pton() is never on the fastpath, so code size matters more. In this sense, conversion to mac_pton() should be done, and strnlen() probably should not.
diff --git a/lib/net_utils.c b/lib/net_utils.c index 148fc6e99ef6..e7785cf20f59 100644 --- a/lib/net_utils.c +++ b/lib/net_utils.c @@ -7,10 +7,6 @@ bool mac_pton(const char *s, u8 *mac) { int i; - /* XX:XX:XX:XX:XX:XX */ - if (strlen(s) < 3 * ETH_ALEN - 1) - return false; - /* Don't dirty result unless string is valid MAC. */ for (i = 0; i < ETH_ALEN; i++) { if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))