diff mbox

[v2,net-next,2/3] net: dsa: lan9303: define LAN9303_NUM_PORTS 3

Message ID 20170801111439.1143-3-privat@egil-hjelmeland.no
State Changes Requested, archived
Delegated to: David Miller
Headers show

Commit Message

Egil Hjelmeland Aug. 1, 2017, 11:14 a.m. UTC
Will be used instead of '3' in upcomming patches.

Signed-off-by: Egil Hjelmeland <privat@egil-hjelmeland.no>
---
 drivers/net/dsa/lan9303-core.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Juergen Borleis Aug. 1, 2017, 11:49 a.m. UTC | #1
Hi Egil,

On Tuesday 01 August 2017 13:14:38 Egil Hjelmeland wrote:
> Will be used instead of '3' in upcomming patches.
>
> Signed-off-by: Egil Hjelmeland <privat@egil-hjelmeland.no>
> ---
>  drivers/net/dsa/lan9303-core.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/drivers/net/dsa/lan9303-core.c
> b/drivers/net/dsa/lan9303-core.c index 4c514d3b9f68..2a3c6bf473dd 100644
> --- a/drivers/net/dsa/lan9303-core.c
> +++ b/drivers/net/dsa/lan9303-core.c
> @@ -20,6 +20,8 @@
>
>  #include "lan9303.h"
>
> +#define LAN9303_NUM_PORTS 3
> +
>  /* 13.2 System Control and Status Registers
>   * Multiply register number by 4 to get address offset.
>   */

Maybe we should put this macro into a shared location because 
in "net/dsa/tag_lan9303.c" there is already a "#define LAN9303_MAX_PORTS 
3".

jb
Egil Hjelmeland Aug. 1, 2017, 12:31 p.m. UTC | #2
On 01. aug. 2017 13:49, Juergen Borleis wrote:
> Hi Egil,
> 
> On Tuesday 01 August 2017 13:14:38 Egil Hjelmeland wrote:
>> Will be used instead of '3' in upcomming patches.
>>
>>
>> +#define LAN9303_NUM_PORTS 3
>> +
> 
> Maybe we should put this macro into a shared location because
> in "net/dsa/tag_lan9303.c" there is already a "#define LAN9303_MAX_PORTS
> 3".
> 
> jb
> 

Is there any suitable shared location for such driver specific
definitions?
I could change the name to LAN9303_MAX_PORTS so it the same.
Rhymes better with DSA_MAX_PORTS too.

Let's hear what other mean.

Egil
Andrew Lunn Aug. 1, 2017, 1:27 p.m. UTC | #3
On Tue, Aug 01, 2017 at 02:31:44PM +0200, Egil Hjelmeland wrote:
> On 01. aug. 2017 13:49, Juergen Borleis wrote:
> >Hi Egil,
> >
> >On Tuesday 01 August 2017 13:14:38 Egil Hjelmeland wrote:
> >>Will be used instead of '3' in upcomming patches.
> >>
> >>
> >>+#define LAN9303_NUM_PORTS 3
> >>+
> >
> >Maybe we should put this macro into a shared location because
> >in "net/dsa/tag_lan9303.c" there is already a "#define LAN9303_MAX_PORTS
> >3".
> >
> >jb
> >
> 
> Is there any suitable shared location for such driver specific
> definitions?
> I could change the name to LAN9303_MAX_PORTS so it the same.
> Rhymes better with DSA_MAX_PORTS too.

Hi Egil, Juergen

The other tag drivers do:

        if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
                return NULL;

or just

        if (!ds->ports[port].netdev)
                return NULL;

The first version is the safest, since a malicious switch could return
port 42, and you are accessing way off the end of ds->ports[]. It does
however require you call dsa_switch_alloc() with the correct number of
ports.

     Andrew
Egil Hjelmeland Aug. 1, 2017, 1:45 p.m. UTC | #4
On 01. aug. 2017 15:27, Andrew Lunn wrote:
> On Tue, Aug 01, 2017 at 02:31:44PM +0200, Egil Hjelmeland wrote:
>> On 01. aug. 2017 13:49, Juergen Borleis wrote:
>>> Hi Egil,
>>>
>>> On Tuesday 01 August 2017 13:14:38 Egil Hjelmeland wrote:
>>>> Will be used instead of '3' in upcomming patches.
>>>>
>>>>
>>>> +#define LAN9303_NUM_PORTS 3
>>>> +
>>>
>>> Maybe we should put this macro into a shared location because
>>> in "net/dsa/tag_lan9303.c" there is already a "#define LAN9303_MAX_PORTS
>>> 3".
>>>
>>> jb
>>>
>>
>> Is there any suitable shared location for such driver specific
>> definitions?
>> I could change the name to LAN9303_MAX_PORTS so it the same.
>> Rhymes better with DSA_MAX_PORTS too.
> 
> Hi Egil, Juergen
> 
> The other tag drivers do:
> 
>          if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
>                  return NULL;
> 
> or just
> 
>          if (!ds->ports[port].netdev)
>                  return NULL;
> 
> The first version is the safest, since a malicious switch could return
> port 42, and you are accessing way off the end of ds->ports[]. It does
> however require you call dsa_switch_alloc() with the correct number of
> ports.
> 

Sounds like a plan for a later patch, when changing to 
dsa_switch_alloc(LAN9303_NUM_PORTS)


>       Andrew
> 

Egil
Egil Hjelmeland Aug. 3, 2017, 9:53 a.m. UTC | #5
On 01. aug. 2017 15:27, Andrew Lunn wrote:
> On Tue, Aug 01, 2017 at 02:31:44PM +0200, Egil Hjelmeland wrote:
>> On 01. aug. 2017 13:49, Juergen Borleis wrote:
>>> Hi Egil,
>>>
>>> On Tuesday 01 August 2017 13:14:38 Egil Hjelmeland wrote:
>>>> Will be used instead of '3' in upcomming patches.
>>>>
>>>>
>>>> +#define LAN9303_NUM_PORTS 3
>>>> +
>>>
>>> Maybe we should put this macro into a shared location because
>>> in "net/dsa/tag_lan9303.c" there is already a "#define LAN9303_MAX_PORTS
>>> 3".
>>>
>>> jb
>>>
>>
>> Is there any suitable shared location for such driver specific
>> definitions?
>> I could change the name to LAN9303_MAX_PORTS so it the same.
>> Rhymes better with DSA_MAX_PORTS too.
> 
> Hi Egil, Juergen
> 
> The other tag drivers do:
> 
>          if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
>                  return NULL;
> 
> or just
> 
>          if (!ds->ports[port].netdev)
>                  return NULL;
> 
> The first version is the safest, since a malicious switch could return
> port 42, and you are accessing way off the end of ds->ports[]. It does
> however require you call dsa_switch_alloc() with the correct number of
> ports.
> 
>       Andrew
> 

Related question: If the driver does dsa_switch_alloc(3), can it then
trust that all "port" params passed in DSA methods will be between
0 and 2?

Egil
Andrew Lunn Aug. 3, 2017, 1:33 p.m. UTC | #6
> Related question: If the driver does dsa_switch_alloc(3), can it then
> trust that all "port" params passed in DSA methods will be between
> 0 and 2?

Yes.

	Andrew
diff mbox

Patch

diff --git a/drivers/net/dsa/lan9303-core.c b/drivers/net/dsa/lan9303-core.c
index 4c514d3b9f68..2a3c6bf473dd 100644
--- a/drivers/net/dsa/lan9303-core.c
+++ b/drivers/net/dsa/lan9303-core.c
@@ -20,6 +20,8 @@ 
 
 #include "lan9303.h"
 
+#define LAN9303_NUM_PORTS 3
+
 /* 13.2 System Control and Status Registers
  * Multiply register number by 4 to get address offset.
  */