netdev->dev_addr should only be modified via helpers, but someone may be casting off the const. Add a runtime check to catch abuses.
Unified · Split
| @@ -1942,6 +1942,8 @@ enum netdev_ml_priv_type { · 1941 unmodified lines | |||
| 1942 | * @unlink_list: As netif_addr_lock() can be called recursively, | 1942 | * @unlink_list: As netif_addr_lock() can be called recursively, |
| 1943 | * keep a list of interfaces to be deleted. | 1943 | * keep a list of interfaces to be deleted. |
| 1944 | * | 1944 | * |
| 1945 | * @dev_addr_shadow: Copy of @dev_addr to catch direct writes. | ||
| 1946 | * | ||
| 1945 | * FIXME: cleanup struct net_device such that network protocol info | 1947 | * FIXME: cleanup struct net_device such that network protocol info |
| 1946 | * moves out. | 1948 | * moves out. |
| 1947 | */ | 1949 | */ |
| @@ -2268,6 +2270,8 @@ struct net_device { · 320 unmodified lines | |||
| 2268 | 2270 | ||
| 2269 | /* protected by rtnl_lock */ | 2271 | /* protected by rtnl_lock */ |
| 2270 | struct bpf_xdp_entity xdp_state[__MAX_XDP_MODE]; | 2272 | struct bpf_xdp_entity xdp_state[__MAX_XDP_MODE]; |
| 2273 | |||
| 2274 | u8 dev_addr_shadow[MAX_ADDR_LEN]; | ||
| 2271 | }; | 2275 | }; |
| 2272 | #define to_net_dev(d) container_of(d, struct net_device, dev) | 2276 | #define to_net_dev(d) container_of(d, struct net_device, dev) |
| 2273 | 2277 | ||
| @@ -4288,6 +4292,7 @@ int dev_addr_del(struct net_device *dev, const unsigned char *addr, · 2014 unmodified lines | |||
| 4288 | unsigned char addr_type); | 4292 | unsigned char addr_type); |
| 4289 | void dev_addr_flush(struct net_device *dev); | 4293 | void dev_addr_flush(struct net_device *dev); |
| 4290 | int dev_addr_init(struct net_device *dev); | 4294 | int dev_addr_init(struct net_device *dev); |
| 4295 | void dev_addr_check(struct net_device *dev); | ||
| 4291 | 4296 | ||
| 4292 | /* Functions used for unicast addresses handling */ | 4297 | /* Functions used for unicast addresses handling */ |
| 4293 | int dev_uc_add(struct net_device *dev, const unsigned char *addr); | 4298 | int dev_uc_add(struct net_device *dev, const unsigned char *addr); |
| @@ -1377,6 +1377,7 @@ static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) · 1376 unmodified lines | |||
| 1377 | int ret; | 1377 | int ret; |
| 1378 | 1378 | ||
| 1379 | ASSERT_RTNL(); | 1379 | ASSERT_RTNL(); |
| 1380 | dev_addr_check(dev); | ||
| 1380 | 1381 | ||
| 1381 | if (!netif_device_present(dev)) { | 1382 | if (!netif_device_present(dev)) { |
| 1382 | /* may be detached because parent is runtime-suspended */ | 1383 | /* may be detached because parent is runtime-suspended */ |
| @@ -498,6 +498,21 @@ EXPORT_SYMBOL(__hw_addr_init); · 497 unmodified lines | |||
| 498 | * Device addresses handling functions | 498 | * Device addresses handling functions |
| 499 | */ | 499 | */ |
| 500 | 500 | ||
| 501 | /* Check that netdev->dev_addr is not written to directly as this would | ||
| 502 | * break the rbtree layout. All changes should go thru dev_addr_set() and co. | ||
| 503 | * Remove this check in mid-2024. | ||
| 504 | */ | ||
| 505 | void dev_addr_check(struct net_device *dev) | ||
| 506 | { | ||
| 507 | if (!memcmp(dev->dev_addr, dev->dev_addr_shadow, MAX_ADDR_LEN)) | ||
| 508 | return; | ||
| 509 | |||
| 510 | netdev_warn(dev, "Current addr: %*ph\n", MAX_ADDR_LEN, dev->dev_addr); | ||
| 511 | netdev_warn(dev, "Expected addr: %*ph\n", | ||
| 512 | MAX_ADDR_LEN, dev->dev_addr_shadow); | ||
| 513 | netdev_WARN(dev, "Incorrect netdev->dev_addr\n"); | ||
| 514 | } | ||
| 515 | |||
| 501 | /** | 516 | /** |
| 502 | * dev_addr_flush - Flush device address list | 517 | * dev_addr_flush - Flush device address list |
| 503 | * @dev: device | 518 | * @dev: device |
| @@ -509,6 +524,7 @@ EXPORT_SYMBOL(__hw_addr_init); · 5 unmodified lines | |||
| 509 | void dev_addr_flush(struct net_device *dev) | 524 | void dev_addr_flush(struct net_device *dev) |
| 510 | { | 525 | { |
| 511 | /* rtnl_mutex must be held here */ | 526 | /* rtnl_mutex must be held here */ |
| 527 | dev_addr_check(dev); | ||
| 512 | 528 | ||
| 513 | __hw_addr_flush(&dev->dev_addrs); | 529 | __hw_addr_flush(&dev->dev_addrs); |
| 514 | dev->dev_addr = NULL; | 530 | dev->dev_addr = NULL; |
| @@ -552,8 +568,11 @@ void dev_addr_mod(struct net_device *dev, unsigned int offset, · 37 unmodified lines | |||
| 552 | { | 568 | { |
| 553 | struct netdev_hw_addr *ha; | 569 | struct netdev_hw_addr *ha; |
| 554 | 570 | ||
| 571 | dev_addr_check(dev); | ||
| 572 | |||
| 555 | ha = container_of(dev->dev_addr, struct netdev_hw_addr, addr[0]); | 573 | ha = container_of(dev->dev_addr, struct netdev_hw_addr, addr[0]); |
| 556 | memcpy(&ha->addr[offset], addr, len); | 574 | memcpy(&ha->addr[offset], addr, len); |
| 575 | memcpy(&dev->dev_addr_shadow[offset], addr, len); | ||
| 557 | } | 576 | } |
| 558 | EXPORT_SYMBOL(dev_addr_mod); | 577 | EXPORT_SYMBOL(dev_addr_mod); |
| 559 | 578 | ||