The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Ahmad Byagowi <ahmadexp@gmail.com>,
	linux-leds@vger.kernel.org, devicetree@vger.kernel.org,
	linux-i2c@vger.kernel.org, netdev@vger.kernel.org
Cc: Lee Jones <lee@kernel.org>, Pavel Machek <pavel@kernel.org>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Andi Shyti <andi.shyti@kernel.org>,
	Peter Rosin <peda@lysator.liu.se>,
	Nam Tran <trannamatk@gmail.com>,
	Richard Cochran <richardcochran@gmail.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [RFC net-next v2 4/6] ptp: ocp: Validate EEPROM board IDs
Date: Tue, 4 Aug 2026 14:55:17 +0100	[thread overview]
Message-ID: <4ef01fa0-969f-4ec0-997b-5f5f3f67e11a@linux.dev> (raw)
In-Reply-To: <20260803205011.1249-5-ahmadexp@gmail.com>

On 03/08/2026 21:50, Ahmad Byagowi wrote:
> The EEPROM board ID is a fixed 13-byte field. It is stored without room
> for a terminator and passed to devlink as a C string. An erased EEPROM
> therefore exposes 0xff bytes and can make devlink read beyond the field
> while formatting board.id.
> 
> Reserve a trailing byte, classify erased and malformed contents, and
> publish board.id only when the field contains printable text with valid
> padding. Continue reporting the serial number when the board ID is
> absent.
> 
> Fixes: 0cfcdd1ebcfe1a9b262f6ad8419580720dc843c4 ("ptp: ocp: add nvmem interface for accessing eeprom")

use short format of hash to provide commit to fix:

Fixes: 0cfcdd1ebcfe ("ptp: ocp: add nvmem interface for accessing eeprom")

And as this is a fix, it has to go to net rather than net-next

> Cc: stable@vger.kernel.org
> Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
> ---
>   drivers/ptp/ptp_ocp.c | 69 +++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 66 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
> index 35e911f1a..cec936bd1 100644
> --- a/drivers/ptp/ptp_ocp.c
> +++ b/drivers/ptp/ptp_ocp.c
> @@ -347,6 +347,13 @@ struct ptp_ocp_serial_port {
>   #define OCP_SIGNAL_NUM			4
>   #define OCP_FREQ_NUM			4
>   
> +enum ptp_ocp_board_id_state {
> +	OCP_BOARD_ID_UNREAD,
> +	OCP_BOARD_ID_VALID,
> +	OCP_BOARD_ID_ERASED,
> +	OCP_BOARD_ID_INVALID,
> +};
> +
>   enum {
>   	PORT_GNSS,
>   	PORT_GNSS2,
> @@ -401,8 +408,9 @@ struct ptp_ocp {
>   	bool			fw_loader;
>   	u8			fw_tag;
>   	u16			fw_version;
> -	u8			board_id[OCP_BOARD_ID_LEN];
> +	char			board_id[OCP_BOARD_ID_LEN + 1];
>   	u8			serial[OCP_SERIAL_LEN];
> +	enum ptp_ocp_board_id_state board_id_state;
>   	bool			has_eeprom_data;
>   	u32			pps_req_map;
>   	int			flash_start;
> @@ -472,18 +480,23 @@ struct ptp_ocp_eeprom_map {
>   	.len = sizeof_field(struct ptp_ocp, member),		\
>   	.bp_offset = offsetof(struct ptp_ocp, member)
>   
> +#define EEPROM_ENTRY_LEN(addr, member, entry_len)		\
> +	.off = addr,						\
> +	.len = entry_len,					\
> +	.bp_offset = offsetof(struct ptp_ocp, member)
> +
>   #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
>   	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
>   })
>   
>   static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
> -	{ EEPROM_ENTRY(0x43, board_id) },
> +	{ EEPROM_ENTRY_LEN(0x43, board_id, OCP_BOARD_ID_LEN) },
>   	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
>   	{ }
>   };
>   
>   static struct ptp_ocp_eeprom_map art_eeprom_map[] = {
> -	{ EEPROM_ENTRY(0x200 + 0x43, board_id) },
> +	{ EEPROM_ENTRY_LEN(0x200 + 0x43, board_id, OCP_BOARD_ID_LEN) },
>   	{ EEPROM_ENTRY(0x200 + 0x63, serial) },
>   	{ }
>   };
> @@ -1969,6 +1982,52 @@ ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
>   	*nvmemp = NULL;
>   }
>   
> +static enum ptp_ocp_board_id_state
> +ptp_ocp_classify_board_id(char *board_id)
> +{
> +	bool all_zero = true;
> +	bool all_ones = true;
> +	bool terminated = false;
> +	unsigned int i;
> +
> +	board_id[OCP_BOARD_ID_LEN] = '\0';
> +	for (i = 0; i < OCP_BOARD_ID_LEN; i++) {
> +		u8 value = board_id[i];
> +
> +		all_zero &= value == 0;
> +		all_ones &= value == 0xff;
> +	}
> +
> +	if (all_zero || all_ones) {
> +		board_id[0] = '\0';
> +		return OCP_BOARD_ID_ERASED;
> +	}
> +
> +	for (i = 0; i < OCP_BOARD_ID_LEN; i++) {
> +		u8 value = board_id[i];
> +
> +		if (terminated) {
> +			if (value)
> +				goto invalid;
> +			continue;
> +		}
> +
> +		if (!value) {
> +			terminated = true;
> +			continue;
> +		}
> +		if (value < 0x20 || value > 0x7e)
> +			goto invalid;
> +	}
> +
> +	if (board_id[0])
> +		return OCP_BOARD_ID_VALID;
> +
> +invalid:
> +	board_id[0] = '\0';
> +	return OCP_BOARD_ID_INVALID;
> +}
> +
>   static void
>   ptp_ocp_read_eeprom(struct ptp_ocp *bp)
>   {
> @@ -2001,6 +2060,7 @@ ptp_ocp_read_eeprom(struct ptp_ocp *bp)
>   			goto fail;
>   	}
>   
> +	bp->board_id_state = ptp_ocp_classify_board_id(bp->board_id);
>   	bp->has_eeprom_data = true;
>   
>   out:
> @@ -2177,6 +2237,9 @@ ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
>   	if (err)
>   		return err;
>   
> +	if (bp->board_id_state != OCP_BOARD_ID_VALID)
> +		return 0;
> +

why don't simply
snprintf(buf, OCP_BOARD_ID_LEN + 1, "%s", bp->board_id); ?

buf is already used in the function, let's reuse it

>   	err = devlink_info_version_fixed_put(req,
>   			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
>   			bp->board_id);


  reply	other threads:[~2026-08-04 13:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 20:50 [RFC net-next v2 0/6] ptp: ocp: Add R4006 and V9 I2C peripheral support Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 1/6] dt-bindings: leds: Add IS32FL3207 controller Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 2/6] leds: rgb: Add IS32FL3207 controller driver Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 3/6] i2c: mux: Propagate software nodes to channel adapters Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 4/6] ptp: ocp: Validate EEPROM board IDs Ahmad Byagowi
2026-08-04 13:55   ` Vadim Fedorenko [this message]
2026-08-04 21:07     ` Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 5/6] ptp: ocp: Add R4006 I2C peripheral topology Ahmad Byagowi
2026-08-03 20:50 ` [RFC net-next v2 6/6] ptp: ocp: Add Time Card V9 " Ahmad Byagowi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ef01fa0-969f-4ec0-997b-5f5f3f67e11a@linux.dev \
    --to=vadim.fedorenko@linux.dev \
    --cc=ahmadexp@gmail.com \
    --cc=andi.shyti@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=conor+dt@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@google.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pavel@kernel.org \
    --cc=peda@lysator.liu.se \
    --cc=richardcochran@gmail.com \
    --cc=robh@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=trannamatk@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox