Skip to contentSkip to conversation

new PTP ioctl fixes

PATCH Superseded Jacob Keller 2 participants 4 messages Sep 25, 2019 – Sep 26, 2019

Subsystems (13)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover new PTP ioctl fixes

I noticed recently that Filip added new versions of the PTP ioctls which correctly honor the reserved fields (making it so that we can safely extend them in the future).

Unfortunately, this breaks the old ioctls for a couple of reasons. First, the flags for the old ioctls get cleared. This means that the external timestamp request can never be enabled. Further, it means future new flags will *not* be cleared, and thus old ioctl will potentially send non-zero data and be mis-interpreted.

Additionally, new flags are not protected against in-driver, because no current driver verifies that the flags are only one of the supported ones. This means new flags will be mis-interpreted by the drivers.

This series provides patches to fix drivers to verify and reject unsupported flags, as well as to fix the ioctls to clear flags on the old version of the ioctl properly.

Cc Richard Cochran <[email protected]>
Cc Felipe Balbi <[email protected]>
Cc David S. Miller <[email protected]>
Cc Christopher Hall <[email protected]>

Jacob Keller (2):
ptp: correctly disable flags on old ioctls
net: reject ptp requests with unsupported flags

 drivers/net/dsa/mv88e6xxx/ptp.c               |  5 +++++
 drivers/net/ethernet/broadcom/tg3.c           |  4 ++++
 drivers/net/ethernet/intel/igb/igb_ptp.c      |  8 +++++++
 .../ethernet/mellanox/mlx5/core/lib/clock.c   | 10 +++++++++
 drivers/net/ethernet/microchip/lan743x_ptp.c  |  4 ++++
 drivers/net/ethernet/renesas/ravb_ptp.c       |  9 ++++++++
 .../net/ethernet/stmicro/stmmac/stmmac_ptp.c  |  4 ++++
 drivers/net/phy/dp83640.c                     |  8 +++++++
 drivers/ptp/ptp_chardev.c                     |  4 ++--
 include/uapi/linux/ptp_clock.h                | 22 +++++++++++++++++++
 10 files changed, 76 insertions(+), 2 deletions(-)
Signature · 1 line
2.23.0.245.gf157bbb9169d
#1 lore raw

PATCH 1/2 ptp: correctly disable flags on old ioctls

Commit 415606588c61 ("PTP: introduce new versions of IOCTLs", 2019-09-13) introduced new versions of the PTP ioctls which actually validate that the flags are acceptable values.

As part of this, it cleared the flags value using a bitwise and+negation, in an attempt to prevent the old ioctl from accidentally enabling new features.

This is incorrect for a couple of reasons. First, it results in accidentally preventing previously working flags on the request ioctl. By clearing the "valid" flags, we now no longer allow setting the enable, rising edge, or falling edge flags.

Second, if we add new additional flags in the future, they must not be set by the old ioctl. (Since the flag wasn't checked before, we could potentially break userspace programs which sent garbage flag data.

The correct way to resolve this is to check for and clear all but the originally valid flags.

Create defines indicating which flags are correctly checked and interpreted by the original ioctls. Use these to clear any bits which will not be correctly interpreted by the original ioctls.

In the future, new flags must be added to the VALID_FLAGS macros, but *not* to the V1_VALID_FLAGS macros. In this way, new features may be exposed over the v2 ioctls, but without breaking previous userspace which happened to not clear the flags value properly. The old ioctl will continue to behave the same way, while the new ioctl gains the benefit of using the flags fields.

Cc Richard Cochran <[email protected]>
Cc Felipe Balbi <[email protected]>
Cc David S. Miller <[email protected]>
Cc Christopher Hall <[email protected]>
---
 drivers/ptp/ptp_chardev.c      |  4 ++--
 include/uapi/linux/ptp_clock.h | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
drivers/ptp/ptp_chardev.c +2 -2
index 9c18476d8d10..67d0199840fd 100644
@@ -155,7 +155,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_EXTTS_REQUEST) {
-			req.extts.flags &= ~PTP_EXTTS_VALID_FLAGS;
+			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
 			req.extts.rsv[0] = 0;
 			req.extts.rsv[1] = 0;
 		}
@@ -184,7 +184,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_PEROUT_REQUEST) {
-			req.perout.flags &= ~PTP_PEROUT_VALID_FLAGS;
+			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
 			req.perout.rsv[0] = 0;
 			req.perout.rsv[1] = 0;
 			req.perout.rsv[2] = 0;
include/uapi/linux/ptp_clock.h +22 -0
index f16301015949..59e89a1bc3bb 100644
@@ -31,15 +31,37 @@
 #define PTP_ENABLE_FEATURE (1<<0)
 #define PTP_RISING_EDGE    (1<<1)
 #define PTP_FALLING_EDGE   (1<<2)
+
+/*
+ * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
+ */
 #define PTP_EXTTS_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
 				 PTP_RISING_EDGE |	\
 				 PTP_FALLING_EDGE)
 
+/*
+ * flag fields valid for the original PTP_EXTTS_REQUEST ioctl.
+ * DO NOT ADD NEW FLAGS HERE.
+ */
+#define PTP_EXTTS_V1_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
+					 PTP_RISING_EDGE |	\
+					 PTP_FALLING_EDGE)
+
 /*
  * Bits of the ptp_perout_request.flags field:
  */
 #define PTP_PEROUT_ONE_SHOT (1<<0)
+
+/*
+ * flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl.
+ */
 #define PTP_PEROUT_VALID_FLAGS	(PTP_PEROUT_ONE_SHOT)
+
+/*
+ * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
+ */
+#define PTP_PEROUT_V1_VALID_FLAGS	(0)
+
 /*
  * struct ptp_clock_time - represents a time value
  *
Signature · 1 line
2.23.0.245.gf157bbb9169d
#2 lore raw

PATCH 2/2 net: reject ptp requests with unsupported flags

In reply to Jacob Keller

Fix all of the drivers which implement support for the periodic output or external timestamp requests to reject unsupported flags.

This is important for forward compatibility: if a new flag is introduced, the driver should reject requests to enable the flag until it has been modified to actually support the flag in question.

This patch may not be correct for individual drivers, especially regarding the rising vs falling edge flags. I interpreted the default behavior to be to timestamp the rising edge of a pin transition.

Cc Richard Cochran <[email protected]>
Cc Felipe Balbi <[email protected]>
Cc David S. Miller <[email protected]>
Cc Christopher Hall <[email protected]>
---
 drivers/net/dsa/mv88e6xxx/ptp.c                     |  5 +++++
 drivers/net/ethernet/broadcom/tg3.c                 |  4 ++++
 drivers/net/ethernet/intel/igb/igb_ptp.c            |  8 ++++++++
 drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 10 ++++++++++
 drivers/net/ethernet/microchip/lan743x_ptp.c        |  4 ++++
 drivers/net/ethernet/renesas/ravb_ptp.c             |  9 +++++++++
 drivers/net/ethernet/stmicro/stmmac/stmmac_ptp.c    |  4 ++++
 drivers/net/phy/dp83640.c                           |  8 ++++++++
 8 files changed, 52 insertions(+)
drivers/net/dsa/mv88e6xxx/ptp.c +5 -0
index 073cbd0bb91b..2364b6b67a7b 100644
@@ -273,6 +273,11 @@ static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
 	int pin;
 	int err;
 
+	/* Reject requests with unsupported flags */
+	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
+				PTP_RISING_EDGE))
+		return -EOPNOTSUPP;
+
 	pin = ptp_find_pin(chip->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
 
 	if (pin < 0)
drivers/net/ethernet/broadcom/tg3.c +4 -0
index 77f3511b97de..ca3aa1250dd1 100644
@@ -6280,6 +6280,10 @@ static int tg3_ptp_enable(struct ptp_clock_info *ptp,
 
 	switch (rq->type) {
 	case PTP_CLK_REQ_PEROUT:
+		/* Reject requests with unsupported flags */
+		if (rq->perout.flags)
+			return -EOPNOTSUPP;
+
 		if (rq->perout.index != 0)
 			return -EINVAL;
 
drivers/net/ethernet/intel/igb/igb_ptp.c +8 -0
index fd3071f55bd3..2867a2581a36 100644
@@ -521,6 +521,10 @@ static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
 
 	switch (rq->type) {
 	case PTP_CLK_REQ_EXTTS:
+		/* Reject requests with unsupported flags */
+		if (rq->extts.flags & ~(PTP_ENABLE_FEATURE | PTP_RISING_EDGE))
+			return -EOPNOTSUPP;
+
 		if (on) {
 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
 					   rq->extts.index);
@@ -551,6 +555,10 @@ static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
 		return 0;
 
 	case PTP_CLK_REQ_PEROUT:
+		/* Reject requests with unsupported flags */
+		if (rq->perout.flags)
+			return -EOPNOTSUPP;
+
 		if (on) {
 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
 					   rq->perout.index);
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +10 -0
index 0059b290e095..9a40f24e3193 100644
@@ -236,6 +236,12 @@ static int mlx5_extts_configure(struct ptp_clock_info *ptp,
 	if (!MLX5_PPS_CAP(mdev))
 		return -EOPNOTSUPP;
 
+	/* Reject requests with unsupported flags */
+	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
+				PTP_RISING_EDGE |
+				PTP_FALLING_EDGE))
+		return -EOPNOTSUPP;
+
 	if (rq->extts.index >= clock->ptp_info.n_pins)
 		return -EINVAL;
 
@@ -290,6 +296,10 @@ static int mlx5_perout_configure(struct ptp_clock_info *ptp,
 	if (!MLX5_PPS_CAP(mdev))
 		return -EOPNOTSUPP;
 
+	/* Reject requests with unsupported flags */
+	if (rq->perout.flags)
+		return -EOPNOTSUPP;
+
 	if (rq->perout.index >= clock->ptp_info.n_pins)
 		return -EINVAL;
 
drivers/net/ethernet/microchip/lan743x_ptp.c +4 -0
index 57b26c2acf87..e8fe9a90fe4f 100644
@@ -429,6 +429,10 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
 	int pulse_width = 0;
 	int perout_bit = 0;
 
+	/* Reject requests with unsupported flags */
+	if (perout->flags)
+		return -EOPNOTSUPP;
+
 	if (!on) {
 		lan743x_ptp_perout_off(adapter);
 		return 0;
drivers/net/ethernet/renesas/ravb_ptp.c +9 -0
index 9a42580693cb..fe66697aafec 100644
@@ -182,6 +182,11 @@ static int ravb_ptp_extts(struct ptp_clock_info *ptp,
 	struct net_device *ndev = priv->ndev;
 	unsigned long flags;
 
+	/* Reject requests with unsupported flags */
+	if (req->flags & ~(PTP_ENABLE_FEATURE |
+			   PTP_RISING_EDGE))
+		return -EOPNOTSUPP;
+
 	if (req->index)
 		return -EINVAL;
 
@@ -211,6 +216,10 @@ static int ravb_ptp_perout(struct ptp_clock_info *ptp,
 	unsigned long flags;
 	int error = 0;
 
+	/* Reject requests with unsupported flags */
+	if (req->flags)
+		return -EOPNOTSUPP;
+
 	if (req->index)
 		return -EINVAL;
 
drivers/net/ethernet/stmicro/stmmac/stmmac_ptp.c +4 -0
index 173493db038c..352dc4c68625 100644
@@ -140,6 +140,10 @@ static int stmmac_enable(struct ptp_clock_info *ptp,
 
 	switch (rq->type) {
 	case PTP_CLK_REQ_PEROUT:
+		/* Reject requests with unsupported flags */
+		if (rq->perout.flags)
+			return -EOPNOTSUPP;
+
 		cfg = &priv->pps[rq->perout.index];
 
 		cfg->start.tv_sec = rq->perout.start.sec;
drivers/net/phy/dp83640.c +8 -0
index 6580094161a9..eba0569b1928 100644
@@ -469,6 +469,11 @@ static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
 
 	switch (rq->type) {
 	case PTP_CLK_REQ_EXTTS:
+		/* Reject requests with unsupported flags */
+		if (rq->extts.flags & ~(PTP_FEATURE_ENABLE |
+					PTP_RISING_EDGE |
+					PTP_FALLING_EDGE))
+			return -EOPNOTSUPP;
 		index = rq->extts.index;
 		if (index >= N_EXT_TS)
 			return -EINVAL;
@@ -491,6 +496,9 @@ static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
 		return 0;
 
 	case PTP_CLK_REQ_PEROUT:
+		/* Reject requests with unsupported flags */
+		if (rq->perout.flags)
+			return -EOPNOTSUPP;
 		if (rq->perout.index >= N_PER_OUT)
 			return -EINVAL;
 		return periodic_output(clock, rq, on, rq->perout.index);
Signature · 1 line
2.23.0.245.gf157bbb9169d
#3 lore raw

Re: [Intel-wired-lan] [net-next 2/2] net: reject ptp requests with unsupported flags

Thank you for the patch! Yet something to improve:

Hi Jacob,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on net/master]
[cannot apply to v5.3 next-20190924]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Jacob-Keller/ptp-correctly-disable-flags-on-old-ioctls/20190926-073546
config: x86_64-allyesconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-13) 7.4.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/net/phy/dp83640.c: In function 'ptp_dp83640_enable':

>> drivers/net/phy/dp83640.c:473:27: error: 'PTP_FEATURE_ENABLE' undeclared (first use in this function); did you mean 'DCB_FEATCFG_ENABLE'?
      if (rq->extts.flags & ~(PTP_FEATURE_ENABLE |
                              ^~~~~~~~~~~~~~~~~~
                              DCB_FEATCFG_ENABLE

drivers/net/phy/dp83640.c:473:27: note: each undeclared identifier is reported only once for each function it appears in

vim +473 drivers/net/phy/dp83640.c

460
461 static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
462 struct ptp_clock_request *rq, int on)
463 {
464 struct dp83640_clock *clock =
465 container_of(ptp, struct dp83640_clock, caps);
466 struct phy_device *phydev = clock->chosen->phydev;
467 unsigned int index;
468 u16 evnt, event_num, gpio_num;
469
470 switch (rq->type) {
471 case PTP_CLK_REQ_EXTTS:
472 /* Reject requests with unsupported flags */

 > 473			if (rq->extts.flags & ~(PTP_FEATURE_ENABLE |

474 PTP_RISING_EDGE |
475 PTP_FALLING_EDGE))
476 return -EOPNOTSUPP;
477 index = rq->extts.index;
478 if (index >= N_EXT_TS)
479 return -EINVAL;
480 event_num = EXT_EVENT + index;
481 evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
482 if (on) {
483 gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
484 PTP_PF_EXTTS, index);
485 if (gpio_num < 1)
486 return -EINVAL;
487 evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
488 if (rq->extts.flags & PTP_FALLING_EDGE)
489 evnt |= EVNT_FALL;
490 else
491 evnt |= EVNT_RISE;
492 }
493 mutex_lock(&clock->extreg_lock);
494 ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
495 mutex_unlock(&clock->extreg_lock);
496 return 0;
497
498 case PTP_CLK_REQ_PEROUT:
499 /* Reject requests with unsupported flags */
500 if (rq->perout.flags)
501 return -EOPNOTSUPP;
502 if (rq->perout.index >= N_PER_OUT)
503 return -EINVAL;
504 return periodic_output(clock, rq, on, rq->perout.index);
505
506 default:
507 break;
508 }
509
510 return -EOPNOTSUPP;
511 }
512

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 69878 bytes
Desc: not available
URL: <http://lists.osuosl.org/pipermail/intel-wired-lan/attachments/20190926/080fb7fd/attachment-0001.bin>