Re: [PATCH] net: 8390: Fix possible data races in __ei_get_stats

From: Eric Dumazet
Date: Mon May 07 2018 - 21:56:41 EST




On 05/07/2018 05:51 PM, Jia-Ju Bai wrote:
>
>
> On 2018/5/7 22:15, Eric Dumazet wrote:
>>
>> On 05/07/2018 07:08 AM, Jia-Ju Bai wrote:
>>> The write operations to "dev->stats" are protected by
>>> the spinlock on line 862-864, but the read operations to
>>> this data on line 858 and 867 are not protected by the spinlock.
>>> Thus, there may exist data races for "dev->stats".
>>>
>>> To fix the data races, the read operations to "dev->stats" are
>>> protected by the spinlock, and a local variable is used for return.
>>>
>>> Signed-off-by: Jia-Ju Bai <baijiaju1990@xxxxxxxxx>
>>> ---
>>> Â drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++----
>>> Â 1 file changed, 10 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/net/ethernet/8390/lib8390.c b/drivers/net/ethernet/8390/lib8390.c
>>> index c9c55c9eab9f..198952247d30 100644
>>> --- a/drivers/net/ethernet/8390/lib8390.c
>>> +++ b/drivers/net/ethernet/8390/lib8390.c
>>> @@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev)
>>> ÂÂÂÂÂ unsigned long ioaddr = dev->base_addr;
>>> ÂÂÂÂÂ struct ei_device *ei_local = netdev_priv(dev);
>>> ÂÂÂÂÂ unsigned long flags;
>>> +ÂÂÂ struct net_device_stats *stats;
>>> +
>>> +ÂÂÂ spin_lock_irqsave(&ei_local->page_lock, flags);
>>> Â ÂÂÂÂÂ /* If the card is stopped, just return the present stats. */
>>> -ÂÂÂ if (!netif_running(dev))
>>> -ÂÂÂÂÂÂÂ return &dev->stats;
>>> +ÂÂÂ if (!netif_running(dev)) {
>>> +ÂÂÂÂÂÂÂ stats = &dev->stats;
>>> +ÂÂÂÂÂÂÂ spin_unlock_irqrestore(&ei_local->page_lock, flags);
>>> +ÂÂÂÂÂÂÂ return stats;
>>> +ÂÂÂ }
>>> Â -ÂÂÂ spin_lock_irqsave(&ei_local->page_lock, flags);
>>> ÂÂÂÂÂ /* Read the counter registers, assuming we are in page 0. */
>>>  dev->stats.rx_frame_errors += ei_inb_p(ioaddr + EN0_COUNTER0);
>>> ÂÂÂÂÂ dev->stats.rx_crc_errorsÂÂÂ += ei_inb_p(ioaddr + EN0_COUNTER1);
>>> ÂÂÂÂÂ dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);
>>> +ÂÂÂ stats = &dev->stats;
>>> ÂÂÂÂÂ spin_unlock_irqrestore(&ei_local->page_lock, flags);
>>> Â -ÂÂÂ return &dev->stats;
>>> +ÂÂÂ return stats;
>>> Â }
>>> Â Â /*
>>>
>> dev->stats is not a pointer, it is an array embedded in the
>> struct net_device
>>
>> So this patch is not needed, since dev->stats can not change.
>
> Thanks for your reply :)
>
> I do not understand that why "dev->stats can not change".
> Its data is indeed changed by the code:
>  dev->stats.rx_frame_errors += ei_inb_p(ioaddr + EN0_COUNTER0);
> ÂÂÂÂ dev->stats.rx_crc_errorsÂÂÂ += ei_inb_p(ioaddr + EN0_COUNTER1);
> ÂÂÂÂ dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2);

So ?

>
> So I think a data race may occur when returning "dev->stats" without lock protection.

&dev->stats is a stable value.

It wont change over the lifetime of net_device object.

Adding a barrier before or after getting &dev->stats is useless, confusing and really not needed.



>
> By the way, I find this possible data race is similar to the previous commit 7b31b4deda76 for the tg3 driver.

Very different things really.

This does a copy of the whole stats, not the pointer :

*stats = tp->net_stats_prev;


I guess you are confusing simple C semantics about returning the address of a structure,
instead of returning a whole structure.

If __ei_get_stats(struct net_device *dev) prototype was :

struct net_device_stats __ei_get_stats(struct net_device *dev)

instead of :

struct net_device_stats *__ei_get_stats(struct net_device *dev)

Then sure, your patch might been needed.