Skip to content

Commit 6497ef8

Browse files
Prasanna Kumar Kaleveraxboe
authored andcommitted
nbd: provide a way for userspace processes to identify device backends
Problem: On reconfigure of device, there is no way to defend if the backend storage is matching with the initial backend storage. Say, if an initial connect request for backend "pool1/image1" got mapped to /dev/nbd0 and the userspace process is terminated. A next reconfigure request within NBD_ATTR_DEAD_CONN_TIMEOUT is allowed to use /dev/nbd0 for a different backend "pool1/image2" For example, an operation like below could be dangerous: $ sudo rbd-nbd map --try-netlink rbd-pool/ext4-image /dev/nbd0 $ sudo blkid /dev/nbd0 /dev/nbd0: UUID="bfc444b4-64b1-418f-8b36-6e0d170cfc04" TYPE="ext4" $ sudo pkill -9 rbd-nbd $ sudo rbd-nbd attach --try-netlink --device /dev/nbd0 rbd-pool/xfs-image /dev/nbd0 $ sudo blkid /dev/nbd0 /dev/nbd0: UUID="d29bf343-6570-4069-a9ea-2fa156ced908" TYPE="xfs" Solution: Provide a way for userspace processes to keep some metadata to identify between the device and the backend, so that when a reconfigure request is made, we can compare and avoid such dangerous operations. With this solution, as part of the initial connect request, backend path can be stored in the sysfs per device config, so that on a reconfigure request it's easy to check if the backend path matches with the initial connect backend path. Please note, ioctl interface to nbd will not have these changes, as there won't be any reconfigure. Signed-off-by: Prasanna Kumar Kalever <[email protected]> Reviewed-by: Xiubo Li <[email protected]> Reviewed-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 35efb59 commit 6497ef8

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

drivers/block/nbd.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct link_dead_args {
7979
#define NBD_RT_HAS_CONFIG_REF 4
8080
#define NBD_RT_BOUND 5
8181
#define NBD_RT_DISCONNECT_ON_CLOSE 6
82+
#define NBD_RT_HAS_BACKEND_FILE 7
8283

8384
#define NBD_DESTROY_ON_DISCONNECT 0
8485
#define NBD_DISCONNECT_REQUESTED 1
@@ -119,6 +120,8 @@ struct nbd_device {
119120

120121
struct completion *destroy_complete;
121122
unsigned long flags;
123+
124+
char *backend;
122125
};
123126

124127
#define NBD_CMD_REQUEUED 1
@@ -216,6 +219,20 @@ static const struct device_attribute pid_attr = {
216219
.show = pid_show,
217220
};
218221

222+
static ssize_t backend_show(struct device *dev,
223+
struct device_attribute *attr, char *buf)
224+
{
225+
struct gendisk *disk = dev_to_disk(dev);
226+
struct nbd_device *nbd = (struct nbd_device *)disk->private_data;
227+
228+
return sprintf(buf, "%s\n", nbd->backend ?: "");
229+
}
230+
231+
static const struct device_attribute backend_attr = {
232+
.attr = { .name = "backend", .mode = 0444},
233+
.show = backend_show,
234+
};
235+
219236
static void nbd_dev_remove(struct nbd_device *nbd)
220237
{
221238
struct gendisk *disk = nbd->disk;
@@ -1211,6 +1228,12 @@ static void nbd_config_put(struct nbd_device *nbd)
12111228
&config->runtime_flags))
12121229
device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
12131230
nbd->task_recv = NULL;
1231+
if (test_and_clear_bit(NBD_RT_HAS_BACKEND_FILE,
1232+
&config->runtime_flags)) {
1233+
device_remove_file(disk_to_dev(nbd->disk), &backend_attr);
1234+
kfree(nbd->backend);
1235+
nbd->backend = NULL;
1236+
}
12141237
nbd_clear_sock(nbd);
12151238
if (config->num_connections) {
12161239
int i;
@@ -1270,7 +1293,7 @@ static int nbd_start_device(struct nbd_device *nbd)
12701293

12711294
error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
12721295
if (error) {
1273-
dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
1296+
dev_err(disk_to_dev(nbd->disk), "device_create_file failed for pid!\n");
12741297
return error;
12751298
}
12761299
set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags);
@@ -1657,6 +1680,7 @@ static int nbd_dev_add(int index)
16571680
BLK_MQ_F_BLOCKING;
16581681
nbd->tag_set.driver_data = nbd;
16591682
nbd->destroy_complete = NULL;
1683+
nbd->backend = NULL;
16601684

16611685
err = blk_mq_alloc_tag_set(&nbd->tag_set);
16621686
if (err)
@@ -1743,6 +1767,7 @@ static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
17431767
[NBD_ATTR_SOCKETS] = { .type = NLA_NESTED},
17441768
[NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 },
17451769
[NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED},
1770+
[NBD_ATTR_BACKEND_IDENTIFIER] = { .type = NLA_STRING},
17461771
};
17471772

17481773
static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = {
@@ -1945,6 +1970,23 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
19451970
}
19461971
}
19471972
ret = nbd_start_device(nbd);
1973+
if (ret)
1974+
goto out;
1975+
if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
1976+
nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
1977+
GFP_KERNEL);
1978+
if (!nbd->backend) {
1979+
ret = -ENOMEM;
1980+
goto out;
1981+
}
1982+
}
1983+
ret = device_create_file(disk_to_dev(nbd->disk), &backend_attr);
1984+
if (ret) {
1985+
dev_err(disk_to_dev(nbd->disk),
1986+
"device_create_file failed for backend!\n");
1987+
goto out;
1988+
}
1989+
set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags);
19481990
out:
19491991
mutex_unlock(&nbd->config_lock);
19501992
if (!ret) {
@@ -2037,6 +2079,22 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
20372079
index);
20382080
return -EINVAL;
20392081
}
2082+
if (nbd->backend) {
2083+
if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
2084+
if (nla_strcmp(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
2085+
nbd->backend)) {
2086+
mutex_unlock(&nbd_index_mutex);
2087+
dev_err(nbd_to_dev(nbd),
2088+
"backend image doesn't match with %s\n",
2089+
nbd->backend);
2090+
return -EINVAL;
2091+
}
2092+
} else {
2093+
mutex_unlock(&nbd_index_mutex);
2094+
dev_err(nbd_to_dev(nbd), "must specify backend\n");
2095+
return -EINVAL;
2096+
}
2097+
}
20402098
if (!refcount_inc_not_zero(&nbd->refs)) {
20412099
mutex_unlock(&nbd_index_mutex);
20422100
printk(KERN_ERR "nbd: device at index %d is going down\n",

include/uapi/linux/nbd-netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum {
3535
NBD_ATTR_SOCKETS,
3636
NBD_ATTR_DEAD_CONN_TIMEOUT,
3737
NBD_ATTR_DEVICE_LIST,
38+
NBD_ATTR_BACKEND_IDENTIFIER,
3839
__NBD_ATTR_MAX,
3940
};
4041
#define NBD_ATTR_MAX (__NBD_ATTR_MAX - 1)

0 commit comments

Comments
 (0)