Skip to content

Commit e0fa3e5

Browse files
vittyvkgregkh
authored andcommitted
Drivers: hv: utils: fix a race on userspace daemons registration
Background: userspace daemons registration protocol for Hyper-V utilities drivers has two steps: 1) daemon writes its own version to kernel 2) kernel reads it and replies with module version at this point we consider the handshake procedure being completed and we do hv_poll_channel() transitioning the utility device to HVUTIL_READY state. At this point we're ready to handle messages from kernel. When hvutil_transport is in HVUTIL_TRANSPORT_CHARDEV mode we have a single buffer for outgoing message. hvutil_transport_send() puts to this buffer and till the buffer is cleared with hvt_op_read() returns -EFAULT to all consequent calls. Host<->guest protocol guarantees there is no more than one request at a time and we will not get new requests till we reply to the previous one so this single message buffer is enough. Now to the race. When we finish negotiation procedure and send kernel module version to userspace with hvutil_transport_send() it goes into the above mentioned buffer and if the daemon is slow enough to read it from there we can get a collision when a request from the host comes, we won't be able to put anything to the buffer so the request will be lost. To solve the issue we need to know when the negotiation is really done (when the version message is read by the daemon) and transition to HVUTIL_READY state after this happens. Implement a callback on read to support this. Old style netlink communication is not affected by the change, we don't really know when these messages are delivered but we don't have a single message buffer there. Reported-by: Barry Davis <[email protected]> Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: K. Y. Srinivasan <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 396e287 commit e0fa3e5

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

drivers/hv/hv_fcopy.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ static void fcopy_timeout_func(struct work_struct *dummy)
8383
hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
8484
}
8585

86+
static void fcopy_register_done(void)
87+
{
88+
pr_debug("FCP: userspace daemon registered\n");
89+
hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
90+
}
91+
8692
static int fcopy_handle_handshake(u32 version)
8793
{
8894
u32 our_ver = FCOPY_CURRENT_VERSION;
@@ -94,7 +100,8 @@ static int fcopy_handle_handshake(u32 version)
94100
break;
95101
case FCOPY_VERSION_1:
96102
/* Daemon expects us to reply with our own version */
97-
if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver)))
103+
if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
104+
fcopy_register_done))
98105
return -EFAULT;
99106
dm_reg_value = version;
100107
break;
@@ -107,8 +114,7 @@ static int fcopy_handle_handshake(u32 version)
107114
*/
108115
return -EINVAL;
109116
}
110-
pr_debug("FCP: userspace daemon ver. %d registered\n", version);
111-
hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
117+
pr_debug("FCP: userspace daemon ver. %d connected\n", version);
112118
return 0;
113119
}
114120

@@ -161,7 +167,7 @@ static void fcopy_send_data(struct work_struct *dummy)
161167
}
162168

163169
fcopy_transaction.state = HVUTIL_USERSPACE_REQ;
164-
rc = hvutil_transport_send(hvt, out_src, out_len);
170+
rc = hvutil_transport_send(hvt, out_src, out_len, NULL);
165171
if (rc) {
166172
pr_debug("FCP: failed to communicate to the daemon: %d\n", rc);
167173
if (cancel_delayed_work_sync(&fcopy_timeout_work)) {

drivers/hv/hv_kvp.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ static void kvp_poll_wrapper(void *channel)
102102
hv_kvp_onchannelcallback(channel);
103103
}
104104

105+
static void kvp_register_done(void)
106+
{
107+
/*
108+
* If we're still negotiating with the host cancel the timeout
109+
* work to not poll the channel twice.
110+
*/
111+
pr_debug("KVP: userspace daemon registered\n");
112+
cancel_delayed_work_sync(&kvp_host_handshake_work);
113+
hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
114+
}
115+
105116
static void
106117
kvp_register(int reg_value)
107118
{
@@ -116,7 +127,8 @@ kvp_register(int reg_value)
116127
kvp_msg->kvp_hdr.operation = reg_value;
117128
strcpy(version, HV_DRV_VERSION);
118129

119-
hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg));
130+
hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg),
131+
kvp_register_done);
120132
kfree(kvp_msg);
121133
}
122134
}
@@ -158,17 +170,10 @@ static int kvp_handle_handshake(struct hv_kvp_msg *msg)
158170
/*
159171
* We have a compatible daemon; complete the handshake.
160172
*/
161-
pr_debug("KVP: userspace daemon ver. %d registered\n",
162-
KVP_OP_REGISTER);
173+
pr_debug("KVP: userspace daemon ver. %d connected\n",
174+
msg->kvp_hdr.operation);
163175
kvp_register(dm_reg_value);
164176

165-
/*
166-
* If we're still negotiating with the host cancel the timeout
167-
* work to not poll the channel twice.
168-
*/
169-
cancel_delayed_work_sync(&kvp_host_handshake_work);
170-
hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
171-
172177
return 0;
173178
}
174179

@@ -455,7 +460,7 @@ kvp_send_key(struct work_struct *dummy)
455460
}
456461

457462
kvp_transaction.state = HVUTIL_USERSPACE_REQ;
458-
rc = hvutil_transport_send(hvt, message, sizeof(*message));
463+
rc = hvutil_transport_send(hvt, message, sizeof(*message), NULL);
459464
if (rc) {
460465
pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
461466
if (cancel_delayed_work_sync(&kvp_timeout_work)) {

drivers/hv/hv_snapshot.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ static void vss_timeout_func(struct work_struct *dummy)
9595
hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
9696
}
9797

98+
static void vss_register_done(void)
99+
{
100+
hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
101+
pr_debug("VSS: userspace daemon registered\n");
102+
}
103+
98104
static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
99105
{
100106
u32 our_ver = VSS_OP_REGISTER1;
@@ -105,16 +111,16 @@ static int vss_handle_handshake(struct hv_vss_msg *vss_msg)
105111
dm_reg_value = VSS_OP_REGISTER;
106112
break;
107113
case VSS_OP_REGISTER1:
108-
/* Daemon expects us to reply with our own version*/
109-
if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver)))
114+
/* Daemon expects us to reply with our own version */
115+
if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
116+
vss_register_done))
110117
return -EFAULT;
111118
dm_reg_value = VSS_OP_REGISTER1;
112119
break;
113120
default:
114121
return -EINVAL;
115122
}
116-
hv_poll_channel(vss_transaction.recv_channel, vss_poll_wrapper);
117-
pr_debug("VSS: userspace daemon ver. %d registered\n", dm_reg_value);
123+
pr_debug("VSS: userspace daemon ver. %d connected\n", dm_reg_value);
118124
return 0;
119125
}
120126

@@ -168,7 +174,7 @@ static void vss_send_op(struct work_struct *dummy)
168174
vss_msg->vss_hdr.operation = op;
169175

170176
vss_transaction.state = HVUTIL_USERSPACE_REQ;
171-
rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg));
177+
rc = hvutil_transport_send(hvt, vss_msg, sizeof(*vss_msg), NULL);
172178
if (rc) {
173179
pr_warn("VSS: failed to communicate to the daemon: %d\n", rc);
174180
if (cancel_delayed_work_sync(&vss_timeout_work)) {

drivers/hv/hv_utils_transport.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ static ssize_t hvt_op_read(struct file *file, char __user *buf,
7272
hvt->outmsg = NULL;
7373
hvt->outmsg_len = 0;
7474

75+
if (hvt->on_read)
76+
hvt->on_read();
77+
hvt->on_read = NULL;
78+
7579
out_unlock:
7680
mutex_unlock(&hvt->lock);
7781
return ret;
@@ -219,7 +223,8 @@ static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
219223
mutex_unlock(&hvt->lock);
220224
}
221225

222-
int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
226+
int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
227+
void (*on_read_cb)(void))
223228
{
224229
struct cn_msg *cn_msg;
225230
int ret = 0;
@@ -237,6 +242,13 @@ int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
237242
memcpy(cn_msg->data, msg, len);
238243
ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
239244
kfree(cn_msg);
245+
/*
246+
* We don't know when netlink messages are delivered but unlike
247+
* in CHARDEV mode we're not blocked and we can send next
248+
* messages right away.
249+
*/
250+
if (on_read_cb)
251+
on_read_cb();
240252
return ret;
241253
}
242254
/* HVUTIL_TRANSPORT_CHARDEV */
@@ -255,6 +267,7 @@ int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
255267
if (hvt->outmsg) {
256268
memcpy(hvt->outmsg, msg, len);
257269
hvt->outmsg_len = len;
270+
hvt->on_read = on_read_cb;
258271
wake_up_interruptible(&hvt->outmsg_q);
259272
} else
260273
ret = -ENOMEM;

drivers/hv/hv_utils_transport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct hvutil_transport {
3636
struct list_head list; /* hvt_list */
3737
int (*on_msg)(void *, int); /* callback on new user message */
3838
void (*on_reset)(void); /* callback when userspace drops */
39+
void (*on_read)(void); /* callback on message read */
3940
u8 *outmsg; /* message to the userspace */
4041
int outmsg_len; /* its length */
4142
wait_queue_head_t outmsg_q; /* poll/read wait queue */
@@ -46,7 +47,8 @@ struct hvutil_transport *hvutil_transport_init(const char *name,
4647
u32 cn_idx, u32 cn_val,
4748
int (*on_msg)(void *, int),
4849
void (*on_reset)(void));
49-
int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len);
50+
int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
51+
void (*on_read_cb)(void));
5052
void hvutil_transport_destroy(struct hvutil_transport *hvt);
5153

5254
#endif /* _HV_UTILS_TRANSPORT_H */

0 commit comments

Comments
 (0)