From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 111CF455C1; Mon, 8 Jul 2024 06:41:40 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E2E8740A75; Mon, 8 Jul 2024 06:41:39 +0200 (CEST) Received: from TY3P286CU002.outbound.protection.outlook.com (mail-japaneastazolkn19010002.outbound.protection.outlook.com [52.103.43.2]) by mails.dpdk.org (Postfix) with ESMTP id 1CD77402C5; Mon, 8 Jul 2024 06:41:38 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K+A9K1hmwrs977Spk+8n/JxKk81f+vb93Juo53vWOYV42+v32I7/FYW/OCiTuDmWrhWlN5bei7KTmyFnVuftOx43JbV+pOM4FOx7kSdf4Mls1Q1OJLGgVkDUGqTnGS8Dn7M0qlEQyUektB+CMqrhlD5qV+OTGitNcIY1oWSUKr98KvsWu5kfPmVIva2fgPR7JanhSgqkytgg+NrJu3OxaFh+O+JWSN2j9ZXouUYbJIGrSP+Cndphtqi1QKhEHWyYP8Ip6BfV7Nw8EI98vBuZ/BGRapx3lk3oRadb7A+jOCGHeiBiYNEjCbbS+nYWxwy9GRYiustBThvZ036hP23/ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5b3JWAKSUCWabpT4C6n55fft+6orGU0hS6Vvf7ysI4o=; b=MVNorxSN1dWgrJhFBFPG+7REwdCHGCChdFycP6U3NNXA3qd2hzLTrhkCaEghQEF96wJlnXXo4mGM+sxkcnO/PpDAU5mixp9LFgtpElgaLMvWR0ir3X/hgCSu/ilkH8AzmIO88qEKdXClFMGDuE99+l+JtvilFDoQHbmt8bs19qjDlbxwjVEVKR+TytAaGwbws3wTWFloG3T6Cmp+Q6o1UVlrGLBcwb6gsfddcLvJkAXO25T47vnqw135av33oHj/RNkxS+CzoCYgjGzm+1i1/jiSXQgO1Nf3CyHvElMsywD38utaYQ2CroI5wNDvCLQ6sHjgoA7IpnkO2C3xXDsFMQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5b3JWAKSUCWabpT4C6n55fft+6orGU0hS6Vvf7ysI4o=; b=EA3UcWTEQL4IiG9oRYAEL6F59khQkJ4kiyxVebnz1J/tFVOKk9uanWHb3vN17qu23GbD9cHPmYXSl7hEz8kG17ZvPV+9Jj9TSqnVcBEhEmRNBpXdWIUXgCxEL/WMf6I84O7QYF3t6I+VvUEydIx8W+RaHJj3qZ5EYzjmMVPyCZYvXVN2lRCAW/Mzu0X9npuSGTTuYc13Rjl6+Wz7lO93n419ipW1BwChSLKRF5BbnwwOtaKssd2JpPR33wZ945P0BBdPqtEFvyYCaqwtlIjftNmKcCzFUHDyTeA2DNoKi3bFbpOu0a+E0nSf9z/S+b5wt0qEMe18dCeF8qycicDDpQ== Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) by TYCP286MB2049.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:139::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Mon, 8 Jul 2024 04:41:35 +0000 Received: from TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::a92e:74bb:6df2:7ea2]) by TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM ([fe80::a92e:74bb:6df2:7ea2%5]) with mapi id 15.20.7741.033; Mon, 8 Jul 2024 04:41:34 +0000 From: Gongming Chen To: maxime.coquelin@redhat.com, chenbox@nvidia.com, chengongming1900@outlook.com Cc: dev@dpdk.org, Gongming Chen , stable@dpdk.org Subject: [PATCH v4] vhost: fix crash caused by accessing a freed vsocket Date: Mon, 8 Jul 2024 12:41:03 +0800 Message-ID: X-Mailer: git-send-email 2.32.1 (Apple Git-133) In-Reply-To: References: Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [bZpQfxuMLv92a6iPzVvRApzTwEeyo+Ll] X-ClientProxiedBy: SI2PR01CA0027.apcprd01.prod.exchangelabs.com (2603:1096:4:192::7) To TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM (2603:1096:402:38::13) X-Microsoft-Original-Message-ID: <20240708044103.12753-1-chengongming1900@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYAP286MB0649:EE_|TYCP286MB2049:EE_ X-MS-Office365-Filtering-Correlation-Id: 6bdc8f49-8c94-4d80-59ff-08dc9f083f1e X-Microsoft-Antispam: BCL:0; ARA:14566002|461199028|19110799003|8060799006|3430499032|3412199025|440099028|1710799026; X-Microsoft-Antispam-Message-Info: t/oRHPDoJN0LbA+mm7u7dEwGuDfrw2abLPgQ6tuyGBo8OqAbgI6AUIRRvD62iJE41qJ0ZChdciaiWbtZDXYORSJLXFLoEtRQgcgbGdS1ddtgfB/EkiSI9q1CaHfDYDtrFnLdu5VhM/DadOFEUVYLbJ6poCS7RhAaxSlY+ildKm/oC7wTFGmBhRzM9tseq4UdFI7p5itMpJC2aIuVedoX6bhKmd0YECcMb/qECBDPo4viTsH3ju+KLXwV9lSpS1RjFUdr0Oy5h8/U1FIbccwbaAtwzvYNswqrRGUjDveUDjLqMRZUq2+A2Isg3F/G+NI1NkAv0j0jeNy1T6U7nUHXlXpb5VSQUSI4wH2WQHwmnVFPLbNVukrEwqzdtat631QSYwCaaRtpo0hW4dtdUA7PqAh0flE4O8nQB4mnjZBm5cydT04IFe8TchWEa2/3wlRaJWBgnGTegQE2jfrQYgTT+eaCvWEuondyVM1UOicgu5R9cFTlIMRWM5f22X4BEY1ER+LNM83b+hUSDB/tZ32GyaLdaoif9/+VCjiGjEaAw8lS5Krrb4Lx/zjr7A0oEpAJjHj9IsWA8PW8foF+4M8vz5zy0HQs20T5HQDZl4qyevWkEMPhB66T4ZdfNGti7S/o8HRqwk1fX2hLCBpXHW8s+IM88lLYVG7M2YA5Mk1F9bI= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?vv2pUxxIrTRUkd3q1aD4gcdnyrwejWCUVk2r8Cf1kSssoiE34o49GC8OqPTg?= =?us-ascii?Q?TYb2m/ZBtMR3eMvTSwqcMO3aMQA+QaMWQca+1zouzDaiAqeBxfbJoo8v6436?= =?us-ascii?Q?Ejn3KUjeyNVSWdD2uLPoI27UxEMFXL6jfWmnPNFUOps20G9fmSw5PlvFqIdE?= =?us-ascii?Q?iRbSvQ4QZh4vdwNozs39CxIa1JvaZJQl4vF8V3wvShLFL6vmyEDPnrVJ43Sk?= =?us-ascii?Q?rkGMu2ljRgziCDG88dxj/JH3FfI98G0TTHU/4/OPIDoRIioMFud2gxhXXCLq?= =?us-ascii?Q?fuUAhY1y64nG85oEASRg2ar86shN+QjyX/rpfL3fRgxwoPZrxqAQUrFMY/jB?= =?us-ascii?Q?8GwmwpKrEv1eRHPqyiEvt6cc66uApEh0458+a0iu9ABMFSrAA8VLs1tibjLn?= =?us-ascii?Q?CCh5hcg7ZnzWBO+6uEYF2SJhZDx7WtbFDKROeH1hQdlnRLBlCrBQAVMm6TN1?= =?us-ascii?Q?KsKBIFQfjBaft9D/A1Bj5WQx2gIv1YsJxrIrP65qClN+lXx9OiAqJBqA3KtG?= =?us-ascii?Q?D6pSGQUhdlHYNWn5+EP5NhH8L9kuh0eqTr8YgYAVpgL6432iIJX7EPf71bKe?= =?us-ascii?Q?znbPNUWlXB1Ugd74gRcGuzt0ZM2ICW/YT7qNVdpYJH+xXA81LeGhkKEicaes?= =?us-ascii?Q?ieDw6nc7M4Cf52NaV0g/PBKsBqEUHS4vD2MeMCcK2GGJRtHSXlUELU3PufoR?= =?us-ascii?Q?TCrvc5KvdRphQ/Ko3zWf+j6vcl928DnIkA6N5s0InvzUEzcKXGS09IAXPvY7?= =?us-ascii?Q?+tsE0ANZJqHxnPMkW3k5E9PWVpYMH+wTjUrt1gCAwlUd8vZx/Pw/oJDP+MD9?= =?us-ascii?Q?AQAbCEh7R6WUNc1/swPaESzjjH9dmJI+YTFrr3sSiP6pPX/zbXWkPkFT2lm2?= =?us-ascii?Q?2Mrn3z0FoCpWwwMoK4q1YhIoaOgTgKYcwHmNyXaRUBJXzSl7IemF7REQaCjl?= =?us-ascii?Q?tEdKPtsAP9hx6RWDCYSTspiG64i6Ic9ec3eEH9bkdbQgEF4gMlHUkbVzlPgx?= =?us-ascii?Q?Mi0VARqmf8idFpqYAs4MqRSyGMTHZ+swdFShjuyhQaaPL8zNPbjIV3DSCix/?= =?us-ascii?Q?GuGSz80f1HT9rE4ITVbKkbiw7/aFI08N2XDYGG/9SeIFkkVTrOLvsNgfCuk1?= =?us-ascii?Q?zZppqJnGVpE4K9CP9Vxq58XUJb5V5f8itOPgL0mJ07cDde0c7Wr+EYxa3Zgu?= =?us-ascii?Q?Fwza5t0QZUpN+TGGpUpow5xyj933uzB121mAzgA58hvzpgN4r1dESzO+fra3?= =?us-ascii?Q?o93GJ4Y7K4wUkQbBcjyf?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6bdc8f49-8c94-4d80-59ff-08dc9f083f1e X-MS-Exchange-CrossTenant-AuthSource: TYAP286MB0649.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2024 04:41:34.8786 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYCP286MB2049 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Gongming Chen When a vhost user message handling error in the event dispatch thread, vsocket reconn is added to the reconnection list of the reconnection thread. Since the reconnection, event dispatching and app configuration thread do not have common thread protection restrictions, the app config thread freed vsocket in the rte_vhost_driver_unregister process, but vsocket reconn can still exist in the reconn_list through this mechanism. Then in the reconnection thread, the vsocket is connected again and conn is added to the dispatch thread. Finally, the vsocket that has been freed by rte_vhost_driver_unregister is accessed again in the event dispatch thread, resulting in a use-after-free error. This patch adds a vhost threads read-write lock to restrict reconnection, event dispatching and app configuration threads. When the vhost driver unregisters, it exclusively holds the lock to safely free the vsocket. #0 0x0000000000000025 in ?? () #1 0x0000000003ed7ca0 in vhost_user_read_cb at lib/vhost/socket.c:323 #2 0x0000000003ed625f in fdset_event_dispatch at lib/vhost/fd_man.c:365 Fixes: e623e0c6d8a5 ("vhost: add vhost-user client mode") Cc: stable@dpdk.org Signed-off-by: Gongming Chen --- lib/vhost/fd_man.c | 3 +++ lib/vhost/meson.build | 1 + lib/vhost/socket.c | 30 ++++++++++++------------------ lib/vhost/vhost_thread.c | 33 +++++++++++++++++++++++++++++++++ lib/vhost/vhost_thread.h | 12 ++++++++++++ 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 lib/vhost/vhost_thread.c create mode 100644 lib/vhost/vhost_thread.h diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c index 87a8dc3f3e..32472d5ed7 100644 --- a/lib/vhost/fd_man.c +++ b/lib/vhost/fd_man.c @@ -15,6 +15,7 @@ #include #include "fd_man.h" +#include "vhost_thread.h" RTE_LOG_REGISTER_SUFFIX(vhost_fdset_logtype, fdset, INFO); #define RTE_LOGTYPE_VHOST_FDMAN vhost_fdset_logtype @@ -342,6 +343,7 @@ fdset_event_dispatch(void *arg) if (numfds < 0) continue; + vhost_thread_read_lock(); for (i = 0; i < numfds; i++) { pthread_mutex_lock(&pfdset->fd_mutex); @@ -379,6 +381,7 @@ fdset_event_dispatch(void *arg) if (remove1 || remove2) fdset_del(pfdset, fd); } + vhost_thread_read_unlock(); if (pfdset->destroy) break; diff --git a/lib/vhost/meson.build b/lib/vhost/meson.build index 41b622a9be..7bc1840ed0 100644 --- a/lib/vhost/meson.build +++ b/lib/vhost/meson.build @@ -25,6 +25,7 @@ sources = files( 'vdpa.c', 'vhost.c', 'vhost_crypto.c', + 'vhost_thread.c', 'vhost_user.c', 'virtio_net.c', 'virtio_net_ctrl.c', diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c index a75728a2e4..c9c295c2a3 100644 --- a/lib/vhost/socket.c +++ b/lib/vhost/socket.c @@ -20,6 +20,7 @@ #include "fd_man.h" #include "vduse.h" #include "vhost.h" +#include "vhost_thread.h" #include "vhost_user.h" @@ -456,6 +457,7 @@ vhost_user_client_reconnect(void *arg __rte_unused) struct vhost_user_reconnect *reconn, *next; while (1) { + vhost_thread_read_lock(); pthread_mutex_lock(&reconn_list.mutex); /* @@ -487,6 +489,7 @@ vhost_user_client_reconnect(void *arg __rte_unused) } pthread_mutex_unlock(&reconn_list.mutex); + vhost_thread_read_unlock(); sleep(1); } @@ -1067,7 +1070,7 @@ rte_vhost_driver_unregister(const char *path) if (path == NULL) return -1; -again: + vhost_thread_write_lock(); pthread_mutex_lock(&vhost_user.mutex); for (i = 0; i < vhost_user.vsocket_cnt; i++) { @@ -1079,14 +1082,10 @@ rte_vhost_driver_unregister(const char *path) vduse_device_destroy(path); } else if (vsocket->is_server) { /* - * If r/wcb is executing, release vhost_user's - * mutex lock, and try again since the r/wcb - * may use the mutex lock. + * The vhost thread write lock has been acquired, + * and fd must be deleted from fdset. */ - if (fdset_try_del(vhost_user.fdset, vsocket->socket_fd) == -1) { - pthread_mutex_unlock(&vhost_user.mutex); - goto again; - } + fdset_del(vhost_user.fdset, vsocket->socket_fd); } else if (vsocket->reconnect) { vhost_user_remove_reconnect(vsocket); } @@ -1098,17 +1097,10 @@ rte_vhost_driver_unregister(const char *path) next = TAILQ_NEXT(conn, next); /* - * If r/wcb is executing, release vsocket's - * conn_mutex and vhost_user's mutex locks, and - * try again since the r/wcb may use the - * conn_mutex and mutex locks. + * The vhost thread write lock has been acquired, + * and fd must be deleted from fdset. */ - if (fdset_try_del(vhost_user.fdset, - conn->connfd) == -1) { - pthread_mutex_unlock(&vsocket->conn_mutex); - pthread_mutex_unlock(&vhost_user.mutex); - goto again; - } + fdset_del(vhost_user.fdset, conn->connfd); VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd); close(conn->connfd); @@ -1130,9 +1122,11 @@ rte_vhost_driver_unregister(const char *path) vhost_user.vsockets[i] = vhost_user.vsockets[count]; vhost_user.vsockets[count] = NULL; pthread_mutex_unlock(&vhost_user.mutex); + vhost_thread_write_unlock(); return 0; } pthread_mutex_unlock(&vhost_user.mutex); + vhost_thread_write_unlock(); return -1; } diff --git a/lib/vhost/vhost_thread.c b/lib/vhost/vhost_thread.c new file mode 100644 index 0000000000..f3ff182976 --- /dev/null +++ b/lib/vhost/vhost_thread.c @@ -0,0 +1,33 @@ +#include + +#include "vhost_thread.h" + +static rte_rwlock_t vhost_thread_lock = RTE_RWLOCK_INITIALIZER; + +void +vhost_thread_read_lock(void) + __rte_no_thread_safety_analysis +{ + rte_rwlock_read_lock(&vhost_thread_lock); +} + +void +vhost_thread_read_unlock(void) + __rte_no_thread_safety_analysis +{ + rte_rwlock_read_unlock(&vhost_thread_lock); +} + +void +vhost_thread_write_lock(void) + __rte_no_thread_safety_analysis +{ + rte_rwlock_write_lock(&vhost_thread_lock); +} + +void +vhost_thread_write_unlock(void) + __rte_no_thread_safety_analysis +{ + rte_rwlock_write_unlock(&vhost_thread_lock); +} diff --git a/lib/vhost/vhost_thread.h b/lib/vhost/vhost_thread.h new file mode 100644 index 0000000000..3c44b1c030 --- /dev/null +++ b/lib/vhost/vhost_thread.h @@ -0,0 +1,12 @@ +#ifndef _VHOST_THREAD_H_ +#define _VHOST_THREAD_H_ + +void vhost_thread_read_lock(void); + +void vhost_thread_read_unlock(void); + +void vhost_thread_write_lock(void); + +void vhost_thread_write_unlock(void); + +#endif -- 2.32.1 (Apple Git-133)