Skip to contentSkip to conversation

net: ncsi: check for netlink-driven responses before requiring a handler

PATCH In review Jeremy Kerr 3 participants 4 messages Oct 28, 2024 – Nov 4, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ncsi: check for netlink-driven responses before requiring a handler

Currently, the NCSI response path will look up an opcode-specific handler for all incoming response messages. However, we may be receiving a response from a netlink-generated request, which may not have a corresponding in-kernel handler for that request opcode. In that case, we'll drop the response because we didn't find a opcode-specific handler.

Perform the lookup for the pending request (and hence for NETLINK_DRIVEN) before requiring an in-kernel handler, and defer the requirement for a corresponding kernel request until we know it's a kernel-driven command.

---
 net/ncsi/ncsi-rsp.c | 61 ++++++++++++++++++++++++-----------------------------
 1 file changed, 27 insertions(+), 34 deletions(-)
net/ncsi/ncsi-rsp.c +27 -34
index e28be33bdf2c487c0fbfe3a1b4de6f52c8f923cc..882767c138895eba5bd7e413b7edb6480d5807bf 100644
@@ -1205,12 +1205,6 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 		}
 	}
 
-	if (!nrh) {
-		netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
-			   hdr->type);
-		return -ENOENT;
-	}
-
 	/* Associate with the request */
 	spin_lock_irqsave(&ndp->lock, flags);
 	nr = &ndp->requests[hdr->id];
@@ -1228,43 +1222,42 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 
 	/* Validate the packet */
 	spin_unlock_irqrestore(&ndp->lock, flags);
-	payload = nrh->payload;
+	payload = nrh ? nrh->payload : -1;
 	if (payload < 0)
 		payload = ntohs(hdr->length);
+
 	ret = ncsi_validate_rsp_pkt(nr, payload);
-	if (ret) {
+	if (ret)
 		netdev_warn(ndp->ndev.dev,
 			    "NCSI: 'bad' packet ignored for type 0x%x\n",
 			    hdr->type);
 
-		if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
-			if (ret == -EPERM)
-				goto out_netlink;
-			else
-				ncsi_send_netlink_err(ndp->ndev.dev,
-						      nr->snd_seq,
-						      nr->snd_portid,
-						      &nr->nlhdr,
-						      ret);
-		}
-		goto out;
-	}
-
-	/* Process the packet */
-	ret = nrh->handler(nr);
-	if (ret)
-		netdev_err(ndp->ndev.dev,
-			   "NCSI: Handler for packet type 0x%x returned %d\n",
-			   hdr->type, ret);
-
-out_netlink:
 	if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
-		ret = ncsi_rsp_handler_netlink(nr);
-		if (ret) {
-			netdev_err(ndp->ndev.dev,
-				   "NCSI: Netlink handler for packet type 0x%x returned %d\n",
-				   hdr->type, ret);
+		/* netlink driven: forward response to netlink socket */
+		if (!ret || ret == -EPERM)
+			ncsi_rsp_handler_netlink(nr);
+		else
+			ncsi_send_netlink_err(ndp->ndev.dev,
+					      nr->snd_seq,
+					      nr->snd_portid,
+					      &nr->nlhdr,
+					      ret);
+	} else if (nrh) {
+		/* not netlink driven: process the packet in the kernel. We
+		 * need to have a handler for this
+		 */
+		if (!ret) {
+			ret = nrh->handler(nr);
+			if (ret)
+				netdev_err(ndp->ndev.dev,
+					   "NCSI: Handler for packet type 0x%x returned %d\n",
+					   hdr->type, ret);
 		}
+
+	} else {
+		netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
+			   hdr->type);
+		ret = -ENOENT;
 	}
 
 out:

---
base-commit: 03fc07a24735e0be8646563913abf5f5cb71ad19
change-id: 20241028-ncsi-arb-opcode-a346ddb88862

Best regards,

Signature · 1 line
Jeremy Kerr <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jeremy Kerr

Currently, the NCSI response path will look up an opcode-specific handler for all incoming response messages. However, we may be receiving a response from a netlink-generated request, which may not have a corresponding in-kernel handler for that request opcode. In that case, we'll drop the response because we didn't find a opcode-specific handler.

Quoted 6 lines

Perform the lookup for the pending request (and hence for NETLINK_DRIVEN) before requiring an in-kernel handler, and defer the requirement for a corresponding kernel request until we know it's a kernel-driven command.

Hi Jeremy,

Not strictly related to this patch, but I do wonder if the log messages should be rate limited, which doesn't seem to be the case at this time.

Regardless, this looks good to me.

...

maintainer
#2 lore raw
In reply to Jeremy Kerr
Quoted 8 lines · On Mon, 28 Oct 2024 15:08:34 +0800 Jeremy Kerr wrote:

Subject: [PATCH net-next] net: ncsi: check for netlink-driven responses before requiring a handler

Currently, the NCSI response path will look up an opcode-specific handler for all incoming response messages. However, we may be receiving a response from a netlink-generated request, which may not have a corresponding in-kernel handler for that request opcode. In that case, we'll drop the response because we didn't find a opcode-specific handler.

This makes it sound like the code is written this way unintentionally, which I doubt. A better description of the patch would be "allow userspace to issue commands unknown to the kernel". And then it'd be great to get some examples of commands you'd like to issue..

Quoted 4 lines

Perform the lookup for the pending request (and hence for NETLINK_DRIVEN) before requiring an in-kernel handler, and defer the requirement for a corresponding kernel request until we know it's a kernel-driven command.

As for the code - delaying handling ret != 0 makes me worried that someone will insert code in between and clobber it. Can you split the handling so that all the ret != 0 (or EPERM for netlink) are still handled in the if (ret) {} ?

Signature · 1 line
pw-bot: cr
#3 lore raw

Hi Jakub,

Quoted 11 lines
Quoted 8 lines

Currently, the NCSI response path will look up an opcode-specific handler for all incoming response messages. However, we may be receiving
a response from a netlink-generated request, which may not have a corresponding in-kernel handler for that request opcode. In that case,
we'll drop the response because we didn't find a opcode-specific handler.

This makes it sound like the code is written this way unintentionally, which I doubt.

This seems like an oversight in the response path, failing on a missing in-kernel handler even if we don't later use it. If we were intentionally enforcing only known commands, then we'd also be checking on the request side.

But yes, I can certainly word this in terms of what this change now enables, and provide examples.

Quoted 10 lines
Quoted 5 lines

Perform the lookup for the pending request (and hence for NETLINK_DRIVEN) before requiring an in-kernel handler, and defer the
requirement for a corresponding kernel request until we know it's a kernel-driven command.

As for the code - delaying handling ret != 0 makes me worried that someone will insert code in between and clobber it. Can you split the handling so that all the ret != 0 (or EPERM for netlink) are still handled in the if (ret) {} ?

Can do! The -EPERM case can probably be simplified a little too, as it just indicates we didn't get a success response from the remote NCSI device.

Will work on a v2 soon.

Cheers,

Jeremy