From patchwork Wed Apr 6 11:43:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12803667 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25F04C433F5 for ; Wed, 6 Apr 2022 15:54:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236660AbiDFP4D (ORCPT ); Wed, 6 Apr 2022 11:56:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236661AbiDFPzy (ORCPT ); Wed, 6 Apr 2022 11:55:54 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63E2A3E9414; Wed, 6 Apr 2022 06:19:31 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 2369l3Gs004892; Wed, 6 Apr 2022 11:44:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=/DnWFwGYN/B4rwV2r1g3sFhTJw2RVllkQ4xzzzla0Y8=; b=e4ThAWNpWUcPA1gXwQVt7StK4zIPlGgKCJoKk4awqN/7BzA1JwyvIVLQlQeHdCQUgrzZ o6yw+CzFbjXLiLBXcifCSzBDOIuW/rDDh3N1RnsNntEY6eg9eHDYqbiBkzyOu80plOty wx+s5hYti9iHLIGkhE5X+8JU30YHae7vJsVUcjnEdNay6yUhiraFPgnH3U4FJrACZTF3 adkJcOduCeeh7ThtNrc+LNlNtF7QCW5h13809dEnUb7BXivUP49P40wRnyU5tZwWf5rr 8Como6DmOm57Z2RvMAsKTxo5I2rBlUcHTKBUqp/xQ73n/cwJ24mf5Ehusq/IckVU0nNJ BQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com with ESMTP id 3f6d930pc9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:01 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 236BfXCC009437; Wed, 6 Apr 2022 11:44:00 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2109.outbound.protection.outlook.com [104.47.58.109]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3f97trw9bj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aJVNYsLGrfOToOZh2rvTzWf5Pc+LbsCUT9L1WUqPSnMcVasMM0SWSpfCUfV8mlNFd5HfUjHMF9iBPvleZQGzH6zgc4fS/UoK+KsbkEKbv5GBNojdUI1xSqrELQpUVFy5IxCqEIJgva3M//3qoHxmM4l4VaVb40VRezD4ZGf903hEc6NpIF2nfh8k3E2CrqvyUBdZlKOfaPtTahB5V8Y/ndQV+NyIyy0ORuInFhVTMPAiPcmRO8ZiqGk5igJs8zadXP1E0rhXesVWekmRFL7oack5YH1IAS6pgy0CdBLnepwXA2WK1PmZigwYh7SzTIL5ZZEbVlnqoQb9yooyMtgc6Q== 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=/DnWFwGYN/B4rwV2r1g3sFhTJw2RVllkQ4xzzzla0Y8=; b=c42kTF/Xkd2kDiAoDhjyG+wDSIFiVvPRbl1KFwIx/nh3PPrD8/ozsfwNwZ3a8QNm6sNbp8vnvU/L6X/4XfiLCx1iV58SbNiu2CL/oFvtnE9qTnjUDx2phff3fqNKBrYSmw8xG00Syb/erxRLvG5DfR7OwJ3/ykKwF9wgbWabbpWiNYUV1hyd28aQ+eYViMsIAlGsGMpG8LKYbozOGfbxNrRsrO9XVLmkz9uFWEJ+/wGbg+etqzesOHMWnNQrkZRNp5AIJmD0sAZHYZft+TqfqR0CKVuO9NHEk62LEhFnBCQf22GAEJsC8G0FGx1dx0D6ZbF7ZV3Mv7c6bmdViZ3L3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/DnWFwGYN/B4rwV2r1g3sFhTJw2RVllkQ4xzzzla0Y8=; b=kQXmMLBS9CqZ2xMKvR+8g1pJHY87ghDwpHMIoLxOLqKokWClGnEPsvxD7sejNacx8d3ucw0hx+dASEnXsL8m3YvMjVk3B6QsjYvGsxZ8AdJVMGwpMyuRqXXq8n1jt7zvLwZZah6viSD63nkx6J2/z8/jdhEF7BEPRhkfeCOmZWk= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM5PR1001MB2409.namprd10.prod.outlook.com (2603:10b6:4:33::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Wed, 6 Apr 2022 11:43:58 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52%3]) with mapi id 15.20.5144.019; Wed, 6 Apr 2022 11:43:58 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 1/3] libbpf: improve library identification for uprobe binary path resolution Date: Wed, 6 Apr 2022 12:43:49 +0100 Message-Id: <1649245431-29956-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> References: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR01CA0073.eurprd01.prod.exchangelabs.com (2603:10a6:208:10e::14) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3fe0d0b7-5f23-4127-6771-08da17c2bcc5 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2409:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nyi5508tf6IEpLTEWL2rkxeva1js8/zhrQMYpv6g6pJZxiX+IXmuuhh0HN0QBF17C6FVX3mjdbIzOsFnESN0J2OTwIryPi2lMbnG0NFY22YjmDkRHtXc9D+Nc69hP4sGsvWNIxc35zaSVQaHqIeRzv4o5ZIVzGXZbCgGO12hG+UIWhRD8qj2u4cmWkjoQnrwvOf0zEsb5CdRgn2gurDCXuFiu3pzEAeQz6PatwM4KP8riYO2RNxMcDt3uRpqFKftWkuxKTh1W7XgpoGIGZw4uEuVn1WOOi1xc0q9F9zcKNyEVOD1HmPpjX4Lq/45xDS6fW8l094BD9xicIPQBup1fRI0N7gSenbOmGahYwSqXTi1urKEHqUdQ6ZNtgqCq6BZGmfmbiI3r/lL+ycgsX5CIV+UNGxgx3q3qtfkk+r49BMUnG2wG1FrNX+S7NcBrzB6YH4Fw4IMaQeFRhDggOmN1X9RNKk4mOkPCP2ieFf8IhAI4SnzJW/6VB8plK+kbIgQ+xprIOpi0T+7SnEvGbazvm7an0KuwotCoeSJnxD27ipR6emccrjxT+inrUwps0OI2zW6VwANe97PpoCVLKuwj3aewy/greo4pn+G+KOjpwy44PJ3nl4MS1cdwWAFkdRUrGrUxvNhHlOtGvriCvZesqrI84ht+4CNN+4N59vOzdpO7YJuBfpJdYOg7DqWKyYXdP1MF/ZbG/gzYK7r9b85wg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(38100700002)(38350700002)(86362001)(8676002)(4326008)(66476007)(66556008)(316002)(2906002)(66946007)(186003)(5660300002)(8936002)(7416002)(44832011)(2616005)(107886003)(26005)(83380400001)(508600001)(6486002)(6666004)(6506007)(6512007)(52116002)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NNWM2GVlQEcL2E6IhyaG8qjNEI2sw9PIssXa00BdVAlrFcN/GO4ZxE7d2PNwA5XnB5FhhfZKOam7+lyV3iDcBy7wawt9oteadZ7PrBclO7/+c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X7iIDKZwJUcIWUg09mcLx/+iGlTE8iVmyYqcLawadlV/vBP6G9ZdshVqqkT/XeCkTNn8ez0caMV75gG/n03zG7tyTCFqFyYBfksx8WFDLz510j0Vq7MDsKXaZmsrWvbP6ecfgj0xRNofvViF3aV6VxK1ILddzbUZoqkN3YemNLK8Yn9SeMTFozrRNlJ99kkAd0OkS5XVpN2/OhQYAlGgsEtcAkESg8dH/iw23QY4+OEbQXMRHiqVLExnmN1l/wDznvYN8M9vgt9gAytlpC20j68iuPGCW9Q7uQZVlNd5OdVq+1SKxgKH8wjer5vX+dBEejgyspxN15A527o5XOhnPNKI3bHC8sWVowV5yODnMOxt1D8eVjJIkD2mW8AiIZc5Omzju8kg/S3V0eW44qQleSa6Ik/UxaIC7bCZ/qV9ak03MG8gSvfZBrMamBtnsGNVEK5OpFe3HGfYrhHSQryMuf4I7Ly6ZVUhAq008p9sbvNSdECm+xwFi9PaqBwbjfpyBJ5RGRq9hZHzL4GfxwPK0B4Oe8xtiCyDVbFsGcTrihTMToRVhuUNVM8dyZ+9n1MdOUdUu5DJz2kOLg= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3fe0d0b7-5f23-4127-6771-08da17c2bcc5 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2022 11:43:58.6524 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2mrZ1ngg8z093ruYQiBLdfnYnI6YMsarorGO9ETiQuFDeWJh2248asJBBS2GkiPs5jXLWIBx9M0+KBvjEZJo0A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2409 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.850 definitions=2022-04-06_04:2022-04-06,2022-04-06 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 mlxscore=0 suspectscore=0 bulkscore=0 adultscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204060054 X-Proofpoint-ORIG-GUID: 9ZiXEZnxns8ePTOyVzxwuQFkRj0hN6HK X-Proofpoint-GUID: 9ZiXEZnxns8ePTOyVzxwuQFkRj0hN6HK Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In the process of doing path resolution for uprobe attach, libraries are identified by matching a ".so" substring in the binary_path. This matches a lot of patterns that do not conform to library.so[.version] format, so instead match a ".so" _suffix_, and if that fails match a ".so." substring for the versioned library case. Suggested-by: Andrii Nakryiko Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1111e9d..c92226a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10766,7 +10766,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz) const char *search_paths[3] = {}; int i; - if (strstr(file, ".so")) { + if (str_has_sfx(file, ".so") || strstr(file, ".so.")) { search_paths[0] = getenv("LD_LIBRARY_PATH"); search_paths[1] = "/usr/lib64:/usr/lib"; search_paths[2] = arch_specific_lib_paths(); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index dd0d4cc..0802724 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -103,6 +103,17 @@ #define str_has_pfx(str, pfx) \ (strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0) +/* suffix check */ +static inline bool str_has_sfx(const char *str, const char *sfx) +{ + size_t str_len = strlen(str); + size_t sfx_len = strlen(sfx); + + if (sfx_len <= str_len) + return strcmp(str + str_len - sfx_len, sfx); + return false; +} + /* Symbol versioning is different between static and shared library. * Properly versioned symbols are needed for shared library, but * only the symbol of the new version is needed for static library. From patchwork Wed Apr 6 11:43:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12803618 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4310C433F5 for ; Wed, 6 Apr 2022 15:23:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236034AbiDFPZS (ORCPT ); Wed, 6 Apr 2022 11:25:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236044AbiDFPYM (ORCPT ); Wed, 6 Apr 2022 11:24:12 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F10D2267C1D; Wed, 6 Apr 2022 05:24:09 -0700 (PDT) Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 2369Ynwx006381; Wed, 6 Apr 2022 11:44:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=LmR0RxbqaoJec/IDQ7baHoZLHWVDDdGvKQ2XWoY0PTA=; b=WlsNMhZWeEzWRWVbuQnhOae9/PNnopGKHSRsLhhccSVEoXfF6koRdwTSGSf9DWXBqt5J PZwDHRKF4cyYi1V9XUDMrY8GukqhGtGNSJlk5IPRwvZYwAs+KNaq4nMt+Dn5QTPrUvTH dJfwB+WHYSsEwTuuIr+1elIuoznnxRuGMiNYKTdwCGTE+5xQiz4/0nfM9qGzfZdyTRS3 bGBG7/gEz6iGqzZ1r2v1DVBggTfwFrwyVKPk2QQcXnsx/vklkpdtxYlcZcKBionlf1TL hvVbp0DTCaVKli24MNlk7JpCI1OzFhuk9Qpe8/UF07FpuxxJOv8oSZL4K/Ym5mWHaOqE /Q== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com with ESMTP id 3f6d31gqcb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:10 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 236BfW00009411; Wed, 6 Apr 2022 11:44:02 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2103.outbound.protection.outlook.com [104.47.58.103]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3f97trw9ck-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bTwLxJeyDHNZJAeTzWDqv3DeK8TS2c/YTxfVAFeUv8DFTs/xbrimTYTek9LjaE6j4Ok0pcFq48yaLFxfF/bL5+AHNcEmv6N1igrFeaOxPX5//wmBP+whflkj6B3qiDuCcouZP7rrwGp9Z5cwuzzlpqKbnCS4Q08n0t50WzrxXHoO677e2bFYa24esPJGGxa5Kk0AISvxfwsLHIL9wZ+9oMmJvoCuLPqLt9XMgT4fzDzDCit/K0DyS/KLejmYuxIpOR+dUBuc7FSB1Zip09T7KJk4sG937niKX9HO+pmwCH038N7t4CUONcXxIBTZwVFypKOG2XXM7A25x3OpoM1LsQ== 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=LmR0RxbqaoJec/IDQ7baHoZLHWVDDdGvKQ2XWoY0PTA=; b=Ev1+uOVCwQz5ipJGkzTC+1m7N5ypGWGRgSq+7sKeLFMRpU3Fbwb0GLh1vyiJPX9S3JXMyo+ZUymf8tU2TJre89ZrZDrBuz41sVgWV/yXx6o8/dOTj/m2gSbvfbgvefva5bd9WkHfRsBVjBsuPoJjrn9TzGGNWGYP079VEZ1usovv6YyhuLdnVpWwvWP3RYzGePdwuUy1VdHjcSCul+dJSdhH/IMimPVgzPQ6jx65iqFv1N9pLNP6kzs4iSYPLkxxtz+tmjbZjfgCu86Pi4Zx+ww8FbLMA7TmhtgonLSHvgIOIuxhJeH4wBM7buvFrV9dEkYJwnj6zG4zNQTGyhb5+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LmR0RxbqaoJec/IDQ7baHoZLHWVDDdGvKQ2XWoY0PTA=; b=KhfisuvoCAwE5azOfahf7X5LaDJaNFuEJuUjokUJNHsvUVGRq7s1swm0Z4ZL+FgZvCh8/TmcEMkQrNyXTVcC5Qkz0zwEtSCu5cuNpkzMTAGqLVeBj8XsNh+SaW3bjcF922WMROwDzBqPabZ1aPgnFZkzgFkx/7WU8r8BGljpqOI= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM5PR1001MB2409.namprd10.prod.outlook.com (2603:10b6:4:33::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Wed, 6 Apr 2022 11:44:00 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52%3]) with mapi id 15.20.5144.019; Wed, 6 Apr 2022 11:44:00 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 2/3] libbpf: improve string parsing for uprobe auto-attach Date: Wed, 6 Apr 2022 12:43:50 +0100 Message-Id: <1649245431-29956-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> References: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR01CA0073.eurprd01.prod.exchangelabs.com (2603:10a6:208:10e::14) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b61148ce-994d-4277-5543-08da17c2bdd5 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2409:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: h60YjEosDnQrPcDuxDVAIyhgts4LtJdSKqc/SHhRLUips4eDuxfocDsoxDorOdW/OWldGJvQovvYWc/0d0tggWmtUBpwb1wa7HvrfU6P48tmncmDQVtuDnxAIgix2ifjx7iJBCaJhe50JOoy80gQCEAjbb9E1JuU/mOcjOYyQPbnf/pKXFMyOfXiONvuzmM5MsMvgZrXUZ1cKh4+TtIjTKzsem4xbY4BFeW+l7dNnCjWtDyknkewoemEwlvWixfOIdF+17GWPv1KNyRs/w5YknHZ3P47Ko+EoiyjeHGlg4k9QiQSau8rLZmZERwiJbmshkCk0qBjNHG6tNg2OenrwXr+0WwoDSGNe3YXe4BY4O1/Keqk2Ea8T9TSZXFw/oDlyCa/vhtYlQFGA9Cuc0JRgAWYeNGeGbXR1gzFXqGbpVpVatnDC+LMYpw5c+4nuSAhLgAn3jNh0FOFYz7yuHUICTAbWa0mFGEMhO/R6BSTA4Yl10BVxCuNZsZZopKyEg1E3m1lGv/GhkZK8ZDiTHIsaxn8NtDqY5LjuwhPBXP7EuiR0sQZpzFd4xxEE2x0cq/6WPMskHhNLz7E5NUQzBrBsrrqigyK6h2MQ7XP2fQ5+ZFjFaONMRBN1pAUym9WHwqECJbiS3+MozyVdbWTNKln2g8Im+necUjnZr8W5jj2LWm+B7WNZD129cZDTvupbiN8/3UlLuYRApEo8xvJXTTZPw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(38100700002)(38350700002)(86362001)(8676002)(4326008)(66476007)(66556008)(316002)(2906002)(66946007)(186003)(5660300002)(8936002)(7416002)(44832011)(2616005)(107886003)(26005)(83380400001)(508600001)(6486002)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d9x9EzCNqLmQ7tgUecvygWddIGNTIWIP8jPDSLlH9HuHD9/fkvbYW6arWSSiRyCihynxnfYDsHR7eg5HA6/avysaJcitx/VKaVJacZOLpMc1f6yyDRkmxULDeShGi/ugNfC+LZ629mL1QR3poHqAEIWt44JGZ0BQCjLRHa43XndvDqjCWXuGknnHfBhdq/jDJsIZlbDLuow+EMGcpwtatcpaUq5AJ+hpV1KBZ2+nTQDRGaDjbT3LOOqE57cXhZ9lMZ8/iCt7Bj+J0/+iDunSjwJF7F7NwvCrF8mPUsVcMxQzetKs6Kgt99ELDiWPZjbPMHfwz1bj7Q0NL5uJyhbFXDkNLs/5AIhDVIBvQ8jmzPF9QPzziFbm1ZjpC5ghVqCwW1rdgWgHsiMCwPO0zu1jEMpDuKnpaK8/1Tcps7naIbCKdb8kNk86GeNdTC0a9YwBBItPvCwLWy9dvcKjTVy8m3PQAe6uWlzWrH6uXEa2K/UNX7K5lJusCucHLQQZkdeSbPBUW7TQxff2lkNbUuDoyTfhur0J+NBJfQ1dVmrcvFtbjr5hjO92irP5aAhg7dgyJtPBDhjp2oPPpsrldZVaFCMWTVsNCCNaWlyT13TMHLYDdCj2IvbiHsKet9CXIucAZHGhUZgpt1k4Ih0V21ijucNt29jr3LGfcJurUU7XY33KCQQ/iYryGomvZvfjabdyIHirlVokKmDuWJ/gyT2rCTHbyVbpnHggeWkrbyDX61LC1KS5Nimk2As2ifOQAKMLsQpV0fo5KSUIb/B5kGBFPyDMiL7ob+jk+rJrp0= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b61148ce-994d-4277-5543-08da17c2bdd5 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2022 11:44:00.4502 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QbQnTOJsp9yaxLmvQhxaHp7WfFKqA4W/ky5hiWDgAVr+LSYUP4z8gKzAUlpFDyxoU5Fl/k95SYgB/XUzHFp85g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2409 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.850 definitions=2022-04-06_04:2022-04-06,2022-04-06 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=936 malwarescore=0 mlxscore=0 suspectscore=0 bulkscore=0 adultscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204060054 X-Proofpoint-GUID: KXm7p2hsefhSVgpwXCCsD_OkqNHKhThv X-Proofpoint-ORIG-GUID: KXm7p2hsefhSVgpwXCCsD_OkqNHKhThv Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net For uprobe auto-attach, the parsing can be simplified for the SEC() name to a single sscanf(); the return value of the sscanf can then be used to distinguish between sections that simply specify "u[ret]probe" (and thus cannot auto-attach), those that specify "u[ret]probe/binary_path:function+offset" etc. Suggested-by: Andrii Nakryiko Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 83 +++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c92226a..707dcc3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10913,60 +10913,45 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz) static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) { DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); - char *func, *probe_name, *func_end; - char *func_name, binary_path[512]; - unsigned long long raw_offset; - size_t offset = 0; - int n; + char *probe_type = NULL, *binary_path = NULL, *func_name = NULL; + int n, ret = -EINVAL; + long offset = 0; *link = NULL; - opts.retprobe = str_has_pfx(prog->sec_name, "uretprobe"); - if (opts.retprobe) - probe_name = prog->sec_name + sizeof("uretprobe") - 1; - else - probe_name = prog->sec_name + sizeof("uprobe") - 1; - if (probe_name[0] == '/') - probe_name++; - - /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ - if (strlen(probe_name) == 0) - return 0; - - snprintf(binary_path, sizeof(binary_path), "%s", probe_name); - /* ':' should be prior to function+offset */ - func_name = strrchr(binary_path, ':'); - if (!func_name) { - pr_warn("section '%s' missing ':function[+offset]' specification\n", - prog->sec_name); - return -EINVAL; - } - func_name[0] = '\0'; - func_name++; - n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); - if (n < 1) { - pr_warn("uprobe name '%s' is invalid\n", func_name); - return -EINVAL; - } - if (opts.retprobe && offset != 0) { - free(func); - pr_warn("uretprobes do not support offset specification\n"); - return -EINVAL; - } - - /* Is func a raw address? */ - errno = 0; - raw_offset = strtoull(func, &func_end, 0); - if (!errno && !*func_end) { - free(func); - func = NULL; - offset = (size_t)raw_offset; + n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li", + &probe_type, &binary_path, &func_name, &offset); + switch (n) { + case 1: + /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ + ret = 0; + break; + case 2: + pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n", + prog->name, prog->sec_name); + break; + case 3: + case 4: + opts.retprobe = strcmp(probe_type, "uretprobe") == 0; + if (opts.retprobe && offset != 0) { + pr_warn("prog '%s': uretprobes do not support offset specification\n", + prog->name); + break; + } + opts.func_name = func_name; + *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); + ret = libbpf_get_error(*link); + break; + default: + pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, + prog->sec_name); + break; } - opts.func_name = func; + free(probe_type); + free(binary_path); + free(func_name); - *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); - free(func); - return libbpf_get_error(*link); + return ret; } struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, From patchwork Wed Apr 6 11:43:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12803657 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23DD8C433EF for ; Wed, 6 Apr 2022 15:44:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236372AbiDFPqX (ORCPT ); Wed, 6 Apr 2022 11:46:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236584AbiDFPqI (ORCPT ); Wed, 6 Apr 2022 11:46:08 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D656D4B0634; Wed, 6 Apr 2022 06:07:34 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 236BYAJv012570; Wed, 6 Apr 2022 11:44:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=AL/NSXeYLGcWzJzn0/AMrJAmZUBTyAc9EUli+hPhTJE=; b=FvqI/aBDO9ZYjQH03CSru+A34OXBP6QAYGKrcLUS5hf/Ab3AWaPuBsUTyRk/ASOO9fI6 ZYSTSLN0vi5SKsb2Edpzrb1gjRzuHzlwQESWfiqJ2QdzdLde1sG21QcsWQ5N7QjwE+H8 o86ABJ58ImfzBL0lyFlJEYyhSnmeSVS8iO4dr3shdDlS2/mTbMeJLmlTu2hITcSNJjIM 2nvq9tGLlMJE1Bw+DLuUlPDjTiz0PHgDc6UFgHleABwH1sVccP+Xrx923YXHJ0prsQw+ G6mrn0CraMmlB8+lqF2hcDi2I+V28jgbp7nCxdGE2jDbr6wbuzXgn76wEZbjy10yuxMT dw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com with ESMTP id 3f6cwcgfag-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:05 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 236BgNkh033516; Wed, 6 Apr 2022 11:44:04 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2109.outbound.protection.outlook.com [104.47.58.109]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3f97wpvq9d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Apr 2022 11:44:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JdA9/9qX0dOmorIkn77fBBs7BbsUhnKzVB3Tj5I5jSOf7oJtKGgklEx77SVLoSj/Zrtqr/ETj7GiVuvxNvb1UrYsUHz6yTPurggPTHujkJYOiG4YJS6XT8V7qA3gKqnvIYgnq1D4pgFZ+rbSYAemqXc9Sd8CvSaqolCuTcXg7YsgNsYRDnA5M+zxrgRPogIFaa32acGErtoXYjCOcsy1+UFOwX5jar77mJgrnX8IqOSSFq90QOQ7QN8MJTqpD/FgMn5YlArJ+zvS5/vBf4HbXKN6VzMFhaZdevsJ7RL7yMRtD9bM+2SyHD/xhvbAEaa9q5sdvjrBq+9bybakG20Lrw== 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=AL/NSXeYLGcWzJzn0/AMrJAmZUBTyAc9EUli+hPhTJE=; b=T3Nn0OWaHcMpus19XQjAyi6Af+HlZRKGqtQPYJfXXKHqZTRE2AkEPMU1f7X+lqf6+p52PaF8rCtoGJ+13PpJezX1mrhL8Uv+ewaIH3nDkaMfwO+5q4TccdDaF/EWFQMENjLNFZWwUgEwrCD9nc0tFf/i7AXT0Rfy1WiT+iBR9C/I3kuD1YmVQtoqRmLjZbCk7c3QEJWoIOj5aH5yVx++fokP3RnHq5kVLq+p0JuKd5KbMwdyHLMwFmUPnWogX9blBxwzJ7RAucK7RuOGHzSdiSin9t5FvAfAXxNh7QfVew9APP1rCzIKmNZUMVXX0ryuSgkq5eiTcZUbJ4eXsmaZ0Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AL/NSXeYLGcWzJzn0/AMrJAmZUBTyAc9EUli+hPhTJE=; b=Iq3zL1wOYxTtQA5DT7K0wgsH8cFvyPbug33yo5I/D2k1pOKpH+hkv0hxiTePcovUBLYpTYQS0o8kAjlpNkozeDZx6+dwmiCkMs7v2DLgStN1gFuIuQPshx7sw0BjCpjrr2/TW1N4UOAa8l0RX0iNhjkXfqWSRUv/l9mCfOlczN4= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM5PR1001MB2409.namprd10.prod.outlook.com (2603:10b6:4:33::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Wed, 6 Apr 2022 11:44:02 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::bc05:7970:d543:fd52%3]) with mapi id 15.20.5144.019; Wed, 6 Apr 2022 11:44:02 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v2 bpf-next 3/3] selftests/bpf: uprobe tests should verify param/return values Date: Wed, 6 Apr 2022 12:43:51 +0100 Message-Id: <1649245431-29956-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> References: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR01CA0073.eurprd01.prod.exchangelabs.com (2603:10a6:208:10e::14) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4ccf2e3e-387d-4a48-bd18-08da17c2bf06 X-MS-TrafficTypeDiagnostic: DM5PR1001MB2409:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gVtb2AYoeaMbogN+ymK1PXY3F2NI6MQQ7c/xBpWoJbSaczKlHXMenB+6fEBpplFiPl57mhUTvxy7rALJfLygmaapI4FzF/Hn5q17keUf2Z9LngSL1DCRpvhu/Rm06eVnJVOapBBsYMjWyuDDlbQy7oUkqg5zcPjSeWUN3XbiSQ8VyrId3DZsJZeeKzXa99DNT3ZZ9CGSsgNn7h3kuR5dWsLjCSkEV7Tf8Y7aOIswcLP0T/olkldTT0KZI5Vjn9E2w0JB/WLGcs2u8VDLHx12Ox4J0LMmaPo2eFt8WxG8evYgjbKQTRKj5rD2bovP5af/GV8A/hQSBjxmWZEUhz8koolaZ8nK5JboGowN4hySNGC9emQg56m4Y8nRe9ikCahCZlzUOkvozd5lAw91Vj/iFaB4KKIKLcIS62lAGprsx98H6qb9XQXfImKkjWuA0Xe0GOAyRUVO0rMQvVJ3A08e238eQenb5UfRCi6yUCNj1vuXvoBxHXwrLzcSpwYZB6G4eQmwDssgKlO0WvJUNovj1eip2PH04/3W6RwjQZQJDCXunIcx4Yfj1GwmX2seEdP9iE1jg6T8y+CDa/Hum3pV50oGKIxE0u5BvhZ6thTxahsVrdHVqapVBvGPzH0+kIJhLv5/RewsSxuiEz/nwKZb+StnuWJhOSsvotDLUHeDfpTkEYcyjlGpIPqpqw8pV8lP7hUwHW+tJz1bRhJvAwQMzA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(38100700002)(38350700002)(86362001)(8676002)(15650500001)(4326008)(66476007)(66556008)(316002)(2906002)(66946007)(186003)(5660300002)(8936002)(7416002)(44832011)(2616005)(107886003)(26005)(83380400001)(508600001)(6486002)(6666004)(6506007)(6512007)(52116002)(36756003);DIR:OUT;SFP:1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ePhGjnhjCZwYoAluH7kBQIPA7u9//qyUWhfeYnsCPHgbEJEHGNdcE89vwfKfva9VzDq6m56SOOSaOB0j9KQIr3zm7eqZo33os+u//yk+jKSvxrYvyQd61gT3rGz67p61HA2bd3B9zqxQjSGJ9WwSV1C6+hgxNnplRnKeAw1EtFgiU2SzlbsekZIoKzvxKDaJsp8GTZR9L/6QO0wVE16lUNM2gDFC22nq6j6Wp+UtusfrqAILwA1mdzuapGfqGup3lD4oJvuDYblwyDry3zpdVqGljR+10LKaALibdv2sRi/pnCbhFv/VLxjAPVOTwCCgKhWLB3pLvsqFIZBJN547wUI6eRP7Ogbpou2RCA0qfWSEZxS2TNNGu7DaTJw0ejOMhyttWPh10XLj5KTLwwNlJFV7CY0jQvrkvEStYW/AIBxnCCwNFTW87V4/Yg9D/OyJZgapWzXlEQ34h7Rqf7Y9S/Zd8v9+bYa2J8Ba7ahinQE/Q+rO23nFJ+E1TgyC/xQ2KEkhfw5VulVv8g5L6j0VirYed12M5ybdZEIHDOcUIrkPDwShDSts46PSqwX9y6EWyRt8irpIy8IINacn51IbHBORGPGf/DeXm1N8uhtmbHZQmTCXgLoy4sCwg7/yg/EdRhnKdrClglbj7iCMmCxLSE8Zuq0xbjkUenT+zI71N+ZzJvgT8bvJfjJ4ZpFfPNKWYwXbWNleahTo7O1v0oAj0VSx8MTkd8Xcx6IWePYUub8kncQDuoZfJROX4MY3cDmo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4ccf2e3e-387d-4a48-bd18-08da17c2bf06 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Apr 2022 11:44:02.4361 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +k9s6pw77yXWvKXflK04H1G0iUcVt2in/XMmMwEjXQqoptUT+jxsKLC+ohhFi/JkWKjGQWQf4tEtHy8XhB38kQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1001MB2409 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.850 definitions=2022-04-06_04:2022-04-06,2022-04-06 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204060054 X-Proofpoint-ORIG-GUID: iZofSm_IF_DyJ2shVcFcx1AverfAsca3 X-Proofpoint-GUID: iZofSm_IF_DyJ2shVcFcx1AverfAsca3 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net uprobe/uretprobe tests don't do any validation of arguments/return values, and without this we can't be sure we are attached to the right function, or that we are indeed attached to a uprobe or uretprobe. To fix this record argument and return value for auto-attached functions and ensure these match expectations. Also need to filter by pid to ensure we do not pick up stray malloc()s since auto-attach traces libc system-wide. Suggested-by: Andrii Nakryiko Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/uprobe_autoattach.c | 25 +++++++++---- .../selftests/bpf/progs/test_uprobe_autoattach.c | 43 ++++++++++++++++------ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c b/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c index 03b15d6..d6003dc 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c @@ -5,14 +5,17 @@ #include "test_uprobe_autoattach.skel.h" /* uprobe attach point */ -static void autoattach_trigger_func(void) +static noinline int autoattach_trigger_func(int arg) { asm volatile (""); + return arg + 1; } void test_uprobe_autoattach(void) { struct test_uprobe_autoattach *skel; + int trigger_val = 100, trigger_ret; + size_t malloc_sz = 1; char *mem; skel = test_uprobe_autoattach__open_and_load(); @@ -22,17 +25,25 @@ void test_uprobe_autoattach(void) if (!ASSERT_OK(test_uprobe_autoattach__attach(skel), "skel_attach")) goto cleanup; + skel->bss->test_pid = getpid(); + /* trigger & validate uprobe & uretprobe */ - autoattach_trigger_func(); + trigger_ret = autoattach_trigger_func(trigger_val); + + skel->bss->test_pid = getpid(); /* trigger & validate shared library u[ret]probes attached by name */ - mem = malloc(1); + mem = malloc(malloc_sz); free(mem); - ASSERT_EQ(skel->bss->uprobe_byname_res, 1, "check_uprobe_byname_res"); - ASSERT_EQ(skel->bss->uretprobe_byname_res, 2, "check_uretprobe_byname_res"); - ASSERT_EQ(skel->bss->uprobe_byname2_res, 3, "check_uprobe_byname2_res"); - ASSERT_EQ(skel->bss->uretprobe_byname2_res, 4, "check_uretprobe_byname2_res"); + ASSERT_EQ(skel->bss->uprobe_byname_parm1, trigger_val, "check_uprobe_byname_parm1"); + ASSERT_EQ(skel->bss->uprobe_byname_ran, 1, "check_uprobe_byname_ran"); + ASSERT_EQ(skel->bss->uretprobe_byname_rc, trigger_ret, "check_uretprobe_byname_rc"); + ASSERT_EQ(skel->bss->uretprobe_byname_ran, 2, "check_uretprobe_byname_ran"); + ASSERT_EQ(skel->bss->uprobe_byname2_parm1, malloc_sz, "check_uprobe_byname2_parm1"); + ASSERT_EQ(skel->bss->uprobe_byname2_ran, 3, "check_uprobe_byname2_ran"); + ASSERT_EQ(skel->bss->uretprobe_byname2_rc, mem, "check_uretprobe_byname2_rc"); + ASSERT_EQ(skel->bss->uretprobe_byname2_ran, 4, "check_uretprobe_byname2_ran"); cleanup: test_uprobe_autoattach__destroy(skel); } diff --git a/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c b/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c index b442fb5..ab75522 100644 --- a/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c +++ b/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c @@ -1,15 +1,22 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2022, Oracle and/or its affiliates. */ -#include -#include +#include "vmlinux.h" + +#include #include #include -int uprobe_byname_res = 0; -int uretprobe_byname_res = 0; -int uprobe_byname2_res = 0; -int uretprobe_byname2_res = 0; +int uprobe_byname_parm1 = 0; +int uprobe_byname_ran = 0; +int uretprobe_byname_rc = 0; +int uretprobe_byname_ran = 0; +size_t uprobe_byname2_parm1 = 0; +int uprobe_byname2_ran = 0; +char *uretprobe_byname2_rc = NULL; +int uretprobe_byname2_ran = 0; + +int test_pid; /* This program cannot auto-attach, but that should not stop other * programs from attaching. @@ -23,14 +30,16 @@ int handle_uprobe_noautoattach(struct pt_regs *ctx) SEC("uprobe//proc/self/exe:autoattach_trigger_func") int handle_uprobe_byname(struct pt_regs *ctx) { - uprobe_byname_res = 1; + uprobe_byname_parm1 = PT_REGS_PARM1_CORE(ctx); + uprobe_byname_ran = 1; return 0; } SEC("uretprobe//proc/self/exe:autoattach_trigger_func") int handle_uretprobe_byname(struct pt_regs *ctx) { - uretprobe_byname_res = 2; + uretprobe_byname_rc = PT_REGS_RC_CORE(ctx); + uretprobe_byname_ran = 2; return 0; } @@ -38,14 +47,26 @@ int handle_uretprobe_byname(struct pt_regs *ctx) SEC("uprobe/libc.so.6:malloc") int handle_uprobe_byname2(struct pt_regs *ctx) { - uprobe_byname2_res = 3; + int pid = bpf_get_current_pid_tgid() >> 32; + + /* ignore irrelevant invocations */ + if (test_pid != pid) + return 0; + uprobe_byname2_parm1 = PT_REGS_PARM1_CORE(ctx); + uprobe_byname2_ran = 3; return 0; } -SEC("uretprobe/libc.so.6:free") +SEC("uretprobe/libc.so.6:malloc") int handle_uretprobe_byname2(struct pt_regs *ctx) { - uretprobe_byname2_res = 4; + int pid = bpf_get_current_pid_tgid() >> 32; + + /* ignore irrelevant invocations */ + if (test_pid != pid) + return 0; + uretprobe_byname2_rc = (char *)PT_REGS_RC_CORE(ctx); + uretprobe_byname2_ran = 4; return 0; }