Skip to content

Commit 41c6140

Browse files
paravmellanoxjgunthorpe
authored andcommitted
RDMA: Check net namespace access for uverbs, umad, cma and nldev
Introduce an API rdma_dev_access_netns() to check whether a rdma device can be accessed from the specified net namespace or not. Use rdma_dev_access_netns() while opening character uverbs, umad network device and also check while rdma cm_id binds to rdma device. Signed-off-by: Parav Pandit <parav@mellanox.com> Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent a56bc45 commit 41c6140

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎drivers/infiniband/core/cma.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ cma_validate_port(struct ib_device *device, u8 port,
616616
int dev_type = dev_addr->dev_type;
617617
struct net_device *ndev = NULL;
618618

619+
if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
620+
return ERR_PTR(-ENODEV);
621+
619622
if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
620623
return ERR_PTR(-ENODEV);
621624

‎drivers/infiniband/core/device.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ static bool ib_devices_shared_netns = true;
131131
module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444);
132132
MODULE_PARM_DESC(netns_mode,
133133
"Share device among net namespaces; default=1 (shared)");
134+
/**
135+
* rdma_dev_access_netns() - Return whether a rdma device can be accessed
136+
* from a specified net namespace or not.
137+
* @device: Pointer to rdma device which needs to be checked
138+
* @net: Pointer to net namesapce for which access to be checked
139+
*
140+
* rdma_dev_access_netns() - Return whether a rdma device can be accessed
141+
* from a specified net namespace or not. When
142+
* rdma device is in shared mode, it ignores the
143+
* net namespace. When rdma device is exclusive
144+
* to a net namespace, rdma device net namespace is
145+
* checked against the specified one.
146+
*/
147+
bool rdma_dev_access_netns(const struct ib_device *dev, const struct net *net)
148+
{
149+
return (ib_devices_shared_netns ||
150+
net_eq(read_pnet(&dev->coredev.rdma_net), net));
151+
}
152+
EXPORT_SYMBOL(rdma_dev_access_netns);
153+
134154
/*
135155
* xarray has this behavior where it won't iterate over NULL values stored in
136156
* allocated arrays. So we need our own iterator to see all values stored in

‎drivers/infiniband/core/user_mad.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,11 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
980980
goto out;
981981
}
982982

983+
if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
984+
ret = -EPERM;
985+
goto out;
986+
}
987+
983988
file = kzalloc(sizeof(*file), GFP_KERNEL);
984989
if (!file) {
985990
ret = -ENOMEM;
@@ -1073,6 +1078,11 @@ static int ib_umad_sm_open(struct inode *inode, struct file *filp)
10731078
}
10741079
}
10751080

1081+
if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
1082+
ret = -EPERM;
1083+
goto err_up_sem;
1084+
}
1085+
10761086
ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
10771087
if (ret)
10781088
goto err_up_sem;

‎drivers/infiniband/core/uverbs_main.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,11 @@ static int ib_uverbs_open(struct inode *inode, struct file *filp)
10451045
goto err;
10461046
}
10471047

1048+
if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
1049+
ret = -EPERM;
1050+
goto err;
1051+
}
1052+
10481053
/* In case IB device supports disassociate ucontext, there is no hard
10491054
* dependency between uverbs device and its low level device.
10501055
*/

‎include/rdma/ib_verbs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4381,4 +4381,7 @@ static inline struct ib_device *rdma_device_to_ibdev(struct device *device)
43814381
*/
43824382
#define rdma_device_to_drv_device(dev, drv_dev_struct, ibdev_member) \
43834383
container_of(rdma_device_to_ibdev(dev), drv_dev_struct, ibdev_member)
4384+
4385+
bool rdma_dev_access_netns(const struct ib_device *device,
4386+
const struct net *net);
43844387
#endif /* IB_VERBS_H */

0 commit comments

Comments
 (0)