cxl/filter: reject trailing characters in identifiers
PATCH 1/1 cxl/filter: reject trailing characters in identifiers
From: yeeli <[email protected]>
The CXL filter helpers parse identifiers with sscanf("%d") and prefixed variants such as "mem%d". These conversions stop at the first non-digit, so a malformed identifier such as "0abc" or "mem0abc" still matches object 0.
Reproduce on a system with CXL memdevs and regions:
cxl list -M -m 0abc # matches mem0
cxl list -R -r region0abc # matches region0
A typo in a destructive command such as
"cxl disable-memdev -m mem0abc" can therefore operate on mem0 instead of failing.
Require the whole token to be consumed by adding a trailing %c and checking that the conversion returns only the expected fields.
Tested by building cxl locally and on the target test system. Valid forms (numeric ids, devnames, host names, "all" and lists) still match, while "0abc", "mem0abc", "region0abc", "0x0" and "0.0" now match nothing. The same matrix passes for the memdev, bus, port, endpoint, decoder and region filters.
AI disclosure: the analysis, reproducer and initial fix were produced with an AI coding assistant (DeepSeek V4.1 Flash, OpenCode). The submitter reviewed and verified the change and added the sign-off.
--- cxl/filter.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
cxl/filter.c +18 -12
@@ -53,6 +53,7 @@ struct cxl_endpoint *util_cxl_endpoint_filter(struct cxl_endpoint *endpoint,char *ident, *save;const char *arg;int endpoint_id; +char c;if (!__ident)return endpoint;
@@ -66,8 +67,8 @@ struct cxl_endpoint *util_cxl_endpoint_filter(struct cxl_endpoint *endpoint,if (strcmp (arg,"all" ) ==0 )break ; -if ((sscanf (arg,"%d" , &endpoint_id) ==1 || -sscanf (arg,"endpoint%d" , &endpoint_id) ==1 ) && +if ((sscanf (arg,"%d%c" , &endpoint_id, &c) ==1 || +sscanf (arg,"endpoint%d%c" , &endpoint_id, &c) ==1 ) &&cxl_endpoint_get_id (endpoint) == endpoint_id)break ;
@@ -90,6 +91,7 @@ static struct cxl_port *__util_cxl_port_filter(struct cxl_port *port,char *ident, *save;const char *arg;int port_id; +char c;if (!__ident)return port;
@@ -112,8 +114,8 @@ static struct cxl_port *__util_cxl_port_filter(struct cxl_port *port,if (strcmp (arg,"endpoint" ) ==0 &&cxl_port_is_endpoint (port))break ; -if ((sscanf (arg,"%d" , &port_id) ==1 || -sscanf (arg,"port%d" , &port_id) ==1 ) && +if ((sscanf (arg,"%d%c" , &port_id, &c) ==1 || +sscanf (arg,"port%d%c" , &port_id, &c) ==1 ) &&cxl_port_get_id (port) == port_id)break ;
@@ -213,6 +215,7 @@ struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus, const char *__ident)char *ident, *save;const char *arg;int bus_id; +char c;if (!__ident)return bus;
@@ -226,8 +229,8 @@ struct cxl_bus *util_cxl_bus_filter(struct cxl_bus *bus, const char *__ident)if (strcmp (arg,"all" ) ==0 )break ; -if ((sscanf (arg,"%d" , &bus_id) ==1 || -sscanf (arg,"root%d" , &bus_id) ==1 ) && +if ((sscanf (arg,"%d%c" , &bus_id, &c) ==1 || +sscanf (arg,"root%d%c" , &bus_id, &c) ==1 ) &&cxl_bus_get_id (bus) == bus_id)break ;
@@ -334,6 +337,7 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,char *ident, *save;const char *name;int memdev_id; +char c;if (!__ident)return util_cxl_memdev_serial_filter (memdev, serials);
@@ -347,8 +351,8 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,if (strcmp (name,"all" ) ==0 )break ; -if ((sscanf (name,"%d" , &memdev_id) ==1 || -sscanf (name,"mem%d" , &memdev_id) ==1 ) && +if ((sscanf (name,"%d%c" , &memdev_id, &c) ==1 || +sscanf (name,"mem%d%c" , &memdev_id, &c) ==1 ) &&cxl_memdev_get_id (memdev) == memdev_id)break ;
@@ -432,6 +436,7 @@ struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,int pid, did;char *ident, *save;const char *name; +char c;if (!__ident)return decoder;
@@ -452,8 +457,8 @@ struct cxl_decoder *util_cxl_decoder_filter(struct cxl_decoder *decoder,if (strcmp (name,"endpoint" ) ==0 &&cxl_port_is_endpoint (port))break ; -if ((sscanf (name,"%d.%d" , &pid, &did) ==2 || -sscanf (name,"decoder%d.%d" , &pid, &did) ==2 ) && +if ((sscanf (name,"%d.%d%c" , &pid, &did, &c) ==2 || +sscanf (name,"decoder%d.%d%c" , &pid, &did, &c) ==2 ) &&cxl_port_get_id (port) == pid &&cxl_decoder_get_id (decoder) == did)break ;
@@ -668,6 +673,7 @@ struct cxl_region *util_cxl_region_filter(struct cxl_region *region,char *ident, *save;const char *name;int id; +char c;if (!__ident)return region;
@@ -681,8 +687,8 @@ struct cxl_region *util_cxl_region_filter(struct cxl_region *region,if (strcmp (name,"all" ) ==0 )break ; -if ((sscanf (name,"%d" , &id) ==1 || -sscanf (name,"region%d" , &id) ==1 ) && +if ((sscanf (name,"%d%c" , &id, &c) ==1 || +sscanf (name,"region%d%c" , &id, &c) ==1 ) &&cxl_region_get_id (region) == id)break ;
Signature · 1 line
2.34.1