netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next v2 0/2] ptp: Check timespec64 before call settime64()
@ 2024-09-06  3:48 Jinjie Ruan
  2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
  2024-09-06  3:48 ` [PATCH -next v2 2/2] net: lan743x: Remove duplicate check Jinjie Ruan
  0 siblings, 2 replies; 8+ messages in thread
From: Jinjie Ruan @ 2024-09-06  3:48 UTC (permalink / raw)
  To: bryan.whitehead, davem, edumazet, kuba, pabeni, richardcochran,
	UNGLinuxDriver, andrew, netdev
  Cc: ruanjinjie

Check timespec64 before call settime64() to make sence.

Jinjie Ruan (2):
  ptp: Check timespec64 before call settime64()
  net: lan743x: Remove duplicate check

 drivers/net/ethernet/microchip/lan743x_ptp.c | 35 ++++++++------------
 drivers/ptp/ptp_clock.c                      | 10 ++++++
 2 files changed, 24 insertions(+), 21 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  3:48 [PATCH -next v2 0/2] ptp: Check timespec64 before call settime64() Jinjie Ruan
@ 2024-09-06  3:48 ` Jinjie Ruan
  2024-09-06  4:15   ` Richard Cochran
                     ` (2 more replies)
  2024-09-06  3:48 ` [PATCH -next v2 2/2] net: lan743x: Remove duplicate check Jinjie Ruan
  1 sibling, 3 replies; 8+ messages in thread
From: Jinjie Ruan @ 2024-09-06  3:48 UTC (permalink / raw)
  To: bryan.whitehead, davem, edumazet, kuba, pabeni, richardcochran,
	UNGLinuxDriver, andrew, netdev
  Cc: ruanjinjie

As Andrew pointed out, it will make sence that the PTP core
checked timespec64 struct's tv_sec and tv_nsec range before calling
ptp->info->settime64(), so check it ahead.

There are some drivers that use tp->tv_sec and tp->tv_nsec directly to
write registers without validity checks and assume that the PTP core has
been checked, which is dangerous and will benefit from this, such as
hclge_ptp_settime(), igb_ptp_settime_i210(), _rcar_gen4_ptp_settime(),
and some drivers can remove the checks of itself.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Suggested-by: Andrew Lunn <andrew@lunn.ch>
---
 drivers/ptp/ptp_clock.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c
index c56cd0f63909..cf75899a6681 100644
--- a/drivers/ptp/ptp_clock.c
+++ b/drivers/ptp/ptp_clock.c
@@ -100,6 +100,16 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp
 		return -EBUSY;
 	}
 
+	if (!tp) {
+		pr_warn("ptp: tp == NULL\n");
+		return -EINVAL;
+	}
+
+	if (!timespec64_valid(tp)) {
+		pr_warn("ptp: tv_sec or tv_usec out of range\n");
+		return -ERANGE;
+	}
+
 	return  ptp->info->settime64(ptp->info, tp);
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH -next v2 2/2] net: lan743x: Remove duplicate check
  2024-09-06  3:48 [PATCH -next v2 0/2] ptp: Check timespec64 before call settime64() Jinjie Ruan
  2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
@ 2024-09-06  3:48 ` Jinjie Ruan
  1 sibling, 0 replies; 8+ messages in thread
From: Jinjie Ruan @ 2024-09-06  3:48 UTC (permalink / raw)
  To: bryan.whitehead, davem, edumazet, kuba, pabeni, richardcochran,
	UNGLinuxDriver, andrew, netdev
  Cc: ruanjinjie

Since timespec64_valid() has been checked before ptp->info->settime64(),
the duplicate check in lan743x_ptpci_settime64() can be removed.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
v2:
- Check it in ptp core instead of using NSEC_PER_SEC macro.
---
 drivers/net/ethernet/microchip/lan743x_ptp.c | 35 ++++++++------------
 1 file changed, 14 insertions(+), 21 deletions(-)

diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c b/drivers/net/ethernet/microchip/lan743x_ptp.c
index dcea6652d56d..4a777b449ecd 100644
--- a/drivers/net/ethernet/microchip/lan743x_ptp.c
+++ b/drivers/net/ethernet/microchip/lan743x_ptp.c
@@ -401,28 +401,21 @@ static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
 	u32 nano_seconds = 0;
 	u32 seconds = 0;
 
-	if (ts) {
-		if (ts->tv_sec > 0xFFFFFFFFLL ||
-		    ts->tv_sec < 0) {
-			netif_warn(adapter, drv, adapter->netdev,
-				   "ts->tv_sec out of range, %lld\n",
-				   ts->tv_sec);
-			return -ERANGE;
-		}
-		if (ts->tv_nsec >= 1000000000L ||
-		    ts->tv_nsec < 0) {
-			netif_warn(adapter, drv, adapter->netdev,
-				   "ts->tv_nsec out of range, %ld\n",
-				   ts->tv_nsec);
-			return -ERANGE;
-		}
-		seconds = ts->tv_sec;
-		nano_seconds = ts->tv_nsec;
-		lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
-	} else {
-		netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
-		return -EINVAL;
+	if (ts->tv_sec > 0xFFFFFFFFLL) {
+		netif_warn(adapter, drv, adapter->netdev,
+			   "ts->tv_sec out of range, %lld\n",
+			   ts->tv_sec);
+		return -ERANGE;
+	}
+	if (ts->tv_nsec < 0) {
+		netif_warn(adapter, drv, adapter->netdev,
+			   "ts->tv_nsec out of range, %ld\n",
+			   ts->tv_nsec);
+		return -ERANGE;
 	}
+	seconds = ts->tv_sec;
+	nano_seconds = ts->tv_nsec;
+	lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
@ 2024-09-06  4:15   ` Richard Cochran
  2024-09-06  4:27   ` Richard Cochran
  2024-09-06 11:51   ` Andrew Lunn
  2 siblings, 0 replies; 8+ messages in thread
From: Richard Cochran @ 2024-09-06  4:15 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: bryan.whitehead, davem, edumazet, kuba, pabeni, UNGLinuxDriver,
	andrew, netdev

On Fri, Sep 06, 2024 at 11:48:05AM +0800, Jinjie Ruan wrote:
> As Andrew pointed out, it will make sence that the PTP core

s/sence/sense/

Thanks,
Richard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
  2024-09-06  4:15   ` Richard Cochran
@ 2024-09-06  4:27   ` Richard Cochran
  2024-09-06  6:37     ` Jinjie Ruan
  2024-09-06 11:51   ` Andrew Lunn
  2 siblings, 1 reply; 8+ messages in thread
From: Richard Cochran @ 2024-09-06  4:27 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: bryan.whitehead, davem, edumazet, kuba, pabeni, UNGLinuxDriver,
	andrew, netdev

On Fri, Sep 06, 2024 at 11:48:05AM +0800, Jinjie Ruan wrote:

> diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c
> index c56cd0f63909..cf75899a6681 100644
> --- a/drivers/ptp/ptp_clock.c
> +++ b/drivers/ptp/ptp_clock.c
> @@ -100,6 +100,16 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp
>  		return -EBUSY;
>  	}
>  
> +	if (!tp) {
> +		pr_warn("ptp: tp == NULL\n");
> +		return -EINVAL;
> +	}

This check is pointless because `tp` cannot be null.

See SYSCALL_DEFINE2(clock_settime, ...)

> +	if (!timespec64_valid(tp)) {
> +		pr_warn("ptp: tv_sec or tv_usec out of range\n");
> +		return -ERANGE;
> +	}

Shouldn't this be done at the higher layer, in clock_settime() ?

Thanks,
Richard

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  4:27   ` Richard Cochran
@ 2024-09-06  6:37     ` Jinjie Ruan
  2024-09-06 14:07       ` Richard Cochran
  0 siblings, 1 reply; 8+ messages in thread
From: Jinjie Ruan @ 2024-09-06  6:37 UTC (permalink / raw)
  To: Richard Cochran
  Cc: bryan.whitehead, davem, edumazet, kuba, pabeni, UNGLinuxDriver,
	andrew, netdev



On 2024/9/6 12:27, Richard Cochran wrote:
> On Fri, Sep 06, 2024 at 11:48:05AM +0800, Jinjie Ruan wrote:
> 
>> diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c
>> index c56cd0f63909..cf75899a6681 100644
>> --- a/drivers/ptp/ptp_clock.c
>> +++ b/drivers/ptp/ptp_clock.c
>> @@ -100,6 +100,16 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp
>>  		return -EBUSY;
>>  	}
>>  
>> +	if (!tp) {
>> +		pr_warn("ptp: tp == NULL\n");
>> +		return -EINVAL;
>> +	}
> 
> This check is pointless because `tp` cannot be null.

Yes, this one is unnecessary and it is also unnecessary in the
lan743x_ptpci_settime64().

> 
> See SYSCALL_DEFINE2(clock_settime, ...)
> 
>> +	if (!timespec64_valid(tp)) {
>> +		pr_warn("ptp: tv_sec or tv_usec out of range\n");
>> +		return -ERANGE;
>> +	}
> 
> Shouldn't this be done at the higher layer, in clock_settime() ?

Maybe it is more reasonable?

> 
> Thanks,
> Richard

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
  2024-09-06  4:15   ` Richard Cochran
  2024-09-06  4:27   ` Richard Cochran
@ 2024-09-06 11:51   ` Andrew Lunn
  2 siblings, 0 replies; 8+ messages in thread
From: Andrew Lunn @ 2024-09-06 11:51 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: bryan.whitehead, davem, edumazet, kuba, pabeni, richardcochran,
	UNGLinuxDriver, netdev

On Fri, Sep 06, 2024 at 11:48:05AM +0800, Jinjie Ruan wrote:
> As Andrew pointed out, it will make sence that the PTP core
> checked timespec64 struct's tv_sec and tv_nsec range before calling
> ptp->info->settime64(), so check it ahead.
> 
> There are some drivers that use tp->tv_sec and tp->tv_nsec directly to
> write registers without validity checks and assume that the PTP core has
> been checked, which is dangerous and will benefit from this, such as
> hclge_ptp_settime(), igb_ptp_settime_i210(), _rcar_gen4_ptp_settime(),
> and some drivers can remove the checks of itself.
> 
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> Suggested-by: Andrew Lunn <andrew@lunn.ch>

FYI: Your Signed-off-by: should be last. Please fix this when you
respin as requested by Richard.


    Andrew

---
pw-bot: cr

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH -next v2 1/2] ptp: Check timespec64 before call settime64()
  2024-09-06  6:37     ` Jinjie Ruan
@ 2024-09-06 14:07       ` Richard Cochran
  0 siblings, 0 replies; 8+ messages in thread
From: Richard Cochran @ 2024-09-06 14:07 UTC (permalink / raw)
  To: Jinjie Ruan
  Cc: bryan.whitehead, davem, edumazet, kuba, pabeni, UNGLinuxDriver,
	andrew, netdev

On Fri, Sep 06, 2024 at 02:37:58PM +0800, Jinjie Ruan wrote:

> > See SYSCALL_DEFINE2(clock_settime, ...)
> > 
> >> +	if (!timespec64_valid(tp)) {
> >> +		pr_warn("ptp: tv_sec or tv_usec out of range\n");
> >> +		return -ERANGE;
> >> +	}
> > 
> > Shouldn't this be done at the higher layer, in clock_settime() ?
> 
> Maybe it is more reasonable?

I think so.  If you code that up, please include lkml and the time
keeping folks (Miroslav, John Stultz, tglx) on CC.

Thanks,
Richard

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-09-06 14:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-06  3:48 [PATCH -next v2 0/2] ptp: Check timespec64 before call settime64() Jinjie Ruan
2024-09-06  3:48 ` [PATCH -next v2 1/2] " Jinjie Ruan
2024-09-06  4:15   ` Richard Cochran
2024-09-06  4:27   ` Richard Cochran
2024-09-06  6:37     ` Jinjie Ruan
2024-09-06 14:07       ` Richard Cochran
2024-09-06 11:51   ` Andrew Lunn
2024-09-06  3:48 ` [PATCH -next v2 2/2] net: lan743x: Remove duplicate check Jinjie Ruan

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).