Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754046AbdFMXvI (ORCPT ); Tue, 13 Jun 2017 19:51:08 -0400 Received: from mail-dm3nam03on0070.outbound.protection.outlook.com ([104.47.41.70]:9568 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753239AbdFMXtr (ORCPT ); Tue, 13 Jun 2017 19:49:47 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=David.Daney@cavium.com; From: David Daney To: Alexei Starovoitov , Daniel Borkmann , "David S. Miller" , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: linux-mips@linux-mips.org, ralf@linux-mips.org, David Daney Subject: [PATCH 1/4] tools: bpf_jit_disasm: Handle large images. Date: Tue, 13 Jun 2017 16:49:35 -0700 Message-Id: <20170613234938.4823-2-david.daney@cavium.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170613234938.4823-1-david.daney@cavium.com> References: <20170613234938.4823-1-david.daney@cavium.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [50.233.148.156] X-ClientProxiedBy: BY2PR07CA0024.namprd07.prod.outlook.com (10.166.107.19) To MWHPR07MB3501.namprd07.prod.outlook.com (10.164.192.28) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWHPR07MB3501: X-MS-Office365-Filtering-Correlation-Id: a3948b36-119a-4f93-5111-08d4b2b6de16 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(201703131423075)(201703031133081);SRVR:MWHPR07MB3501; X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3501;3:E7Oc+sfWibVzlZJcmNQRdp8df8OiApLrdee+pedfkDjTUgAtJ7LKb6H5ope951uDhTx+/Ed63T0DJ5OuyUmjcX3NO9dmQ7Edf/C25JzIHnoynHgrjgsONKGqh2CFirMrfRmZvmKTSW991scbuOJax9nf8vAmiA1Sa8B7RM80Ur3q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JKNtuXBr+460usQUGCSxUfllk/IGgr91tDZ3xoeWbvS4i816vDKk95BLEVsvN4mzDzbPPeijhI0qwt3JaiMPaeaUBLu2zTrLFf3FA5V+DIQ5OxKl3qKUREAznAA6tXqKcG/L2rNu6JLCbsTgFl9WlxF7QdvniKkq0vLiXm0A68gH0FF3cb9L3tfw9p0UrBhEqC2MMmP5s0uMwZ+vusDXXvE9evoVZ++VK4SaO0UNedt+uUUxcUCTNMxahEVeFO2Dysb3BGWMLTFicqsTAy6rYCKT5FUXAphWo0kkcmn+mSTtg7xnf2r463oqoSbSkOPNm1bgI2Xoq2/R7O4VkKNBx/lkuGWmSmzO4yv0owMy X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(3002001)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123562025)(20161123555025)(20161123558100)(20161123560025)(6072148)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:MWHPR07MB3501;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:MWHPR07MB3501; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;MWHPR07MB3501;4:UbNLWpaQJ1TsP87rxWQ1ersI/FvcQUvNgVekxdQ07u?= =?us-ascii?Q?LZg+ad3yIzRyBlsVMj7xsQgs0uLMC82HblDEOehSNFjQ98mvXltE3fPIjzUI?= =?us-ascii?Q?kwSxKkeJ0imciG4msx0IlHipawipW1Vkwez7H90u3DbNFXsxbOUSpJw3k/d1?= =?us-ascii?Q?qyu80uFpu7ZHtqpvCQbAj968mkFMuSopOL0xn2+/L9Kh2jSdYGkrzD/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ENmMDgNUYSnwPwsUygNsgdydjx111a88OmQgB1rpjBu?= =?us-ascii?Q?JaYPTKdVESsYV/BcnIPfYhjh7mQlEo?= X-Forefront-PRVS: 0337AFFE9A X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39840400002)(39450400003)(39410400002)(39400400002)(39850400002)(189002)(199003)(6512007)(105586002)(53936002)(50226002)(53416004)(2950100002)(106356001)(4326008)(189998001)(6506006)(25786009)(42186005)(6666003)(33646002)(50466002)(69596002)(5003940100001)(6486002)(48376002)(76176999)(47776003)(7736002)(305945005)(1076002)(478600001)(101416001)(2906002)(81156014)(8676002)(81166006)(72206003)(107886003)(36756003)(5660300001)(66066001)(38730400002)(3846002)(68736007)(97736004)(6116002)(86362001)(50986999);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR07MB3501;H:ddl.caveonetworks.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;MWHPR07MB3501;23:lompLCigV68H/HBr75TxeQm2I/h9cDU1BaQqCEqjc?= =?us-ascii?Q?CsqVPL+Pf27HNNoA6+5/sOgIaJgalPO57H5OPDBKp/UmxTV2KjAkSfBksLhN?= =?us-ascii?Q?qG7ajrxIiHlVxGw3uHKCNRuANUqtrgWFqrjwqYgxXt29RvBXBe1Tqv4hq4mF?= =?us-ascii?Q?BXFVGGKAj5zZt47A0B9bmtQ0FulDHgH9sSg5v/M3uR8jelKj+2EHKeeDWDRM?= =?us-ascii?Q?3v4yFNQXc2nHtz9ECVYxOu2MUC9axhfHvQvZEe/tUHWDa2VHFrrVAmyu8mTg?= =?us-ascii?Q?WkOPprrdVAq9kyvgjZMmpI6hq05GdVJwhBaS5SNn5Pjp/7GiiWzTvEUFsX9C?= =?us-ascii?Q?hLYyTiTGsFkddYBRGFWvnyUpiwOEfVcMrhorK8j0BA0RxpX/9/LDkOLK0ZsJ?= =?us-ascii?Q?9orcrqmMwJdbiO/XyeFsoX5+Bzpu/oBT1of8IZWiwydWjR/Qi2fcZT/2IqoH?= =?us-ascii?Q?xDahAUDroKMF+Sa+KYPx6F27N2fRvs4ho3rkthWZaw+bhs0+3pqxAF/dlZd/?= =?us-ascii?Q?2MruxStETpXiMua42q10AavQR5BIfDC8KIsmzW7XBjHKj4qrCxB7QVx3mQDI?= =?us-ascii?Q?GrFo85/Hky2nXTDUa8ByxtJtYeRnwlD48EbbQgXIHKkDv+zinNqs2zw9PM+1?= =?us-ascii?Q?8lC2E11BYpVgfWVutw4ueuA3VT0eCuWBcEXuGAIDv6J7LVExaOvu902Sqt8c?= =?us-ascii?Q?x8oy8fanuKFS8Azd7393bgaLwLsr5b8o0RzCMa4vGyOZszOVvVgRYU4oEiq4?= =?us-ascii?Q?wZSxg4xb1R0l78vHfLBYe0WaSRwOo0mW9tXwvG5ylCsqaDm5ub+rqi7Nq58C?= =?us-ascii?Q?RpADMWoBm7o1y03azlwgu0hisvykCOhz2jmx+TRCPdl602VnH074CgZWuXF6?= =?us-ascii?Q?/JO0pUHg6MD5myIxNvnzV/1oV9XEKpOReOUxrpWRjzTQ/iaiyIHB81YcQxQz?= =?us-ascii?Q?ybM8RW2h9PvKRuhd3ED6wdw4oqoW0ycNSnbZe7MQhrcr+0owuXSdlGKQNxHz?= =?us-ascii?Q?1OOZykvo3adILCMpQ2q/UDe0eSiXp/eY2KrarXKUwrXMW7qx9kg+XMvUzf6U?= =?us-ascii?Q?kYNdoC70rdHNo+8Kmn+0QGpDR8sV+9t/LJEDqvtl5S5aCfV4x/KJ/tTrn905?= =?us-ascii?Q?EsmS7PasJfi/bH964n24b+0lV4pSBu6ckYVb2l1oZ+HY1rAAGKegsnuIMHed?= =?us-ascii?Q?6ov6NnLFzBC85WYXiU3G8XookMwzLv5G4l7y8/uO8tDd0Zr+w9LeYe15LmvC?= =?us-ascii?Q?Q2Y/LG4wxj84uzw/Ag=3D?= X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3501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muOgISh1Am0p;24:eoyeY7eyVS09f2GAQg40yvc2SEQiA6RnbQWqH3Rrc20dSniEuajKtFJVzICAMChE+0VswYav5vAeQUxBYnDDjr6n564KOwU39MiTf+Cacew= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;MWHPR07MB3501;7:U3KiekeKzT755YFM2lfIDLYCwLGHGBpjWO9OszeJrxPVC26Pv4ciMNn4EQI2re20zAnEE28mFW8PbEj8SFhfqwltBXlE0WmjJtWK8xLElPkDC+ZQwcctpoQtAHIPk2wd1kBrdhWVb305Pp/E/n0tZ1B+nt4zTaWhVEJhySgIIvpiJedzOTcK1P+yphlan7LFvrcMJhBNMLh2TOcpYnwiTOibrIktZLm845TvOyB+4/AKOGT2c+L6vxhapBBCNRkgrRf/M9CSQ5rIeXGVPhAcnoCub7lirB5en9Dfe/niC9NS1fZiDoo/royk0C3d5ZlKZjaz30W1LGAPVWZFNEarXw== X-OriginatorOrg: cavium.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jun 2017 23:49:44.3379 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR07MB3501 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3256 Lines: 126 Dynamically allocate memory so that JIT images larger than the size of the statically allocated array can be handled. Signed-off-by: David Daney --- tools/net/bpf_jit_disasm.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tools/net/bpf_jit_disasm.c b/tools/net/bpf_jit_disasm.c index ad572e6..422d9abd 100644 --- a/tools/net/bpf_jit_disasm.c +++ b/tools/net/bpf_jit_disasm.c @@ -159,8 +159,8 @@ static void put_log_buff(char *buff) free(buff); } -static unsigned int get_last_jit_image(char *haystack, size_t hlen, - uint8_t *image, size_t ilen) +static uint8_t *get_last_jit_image(char *haystack, size_t hlen, + unsigned int *ilen) { char *ptr, *pptr, *tmp; off_t off = 0; @@ -168,9 +168,10 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, regmatch_t pmatch[1]; unsigned long base; regex_t regex; + uint8_t *image; if (hlen == 0) - return 0; + return NULL; ret = regcomp(®ex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ " "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED); @@ -194,11 +195,22 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, &flen, &proglen, &pass, &base); if (ret != 4) { regfree(®ex); - return 0; + return NULL; + } + if (proglen > 1000000) { + printf("proglen of %d too big, stopping\n", proglen); + return NULL; } + image = malloc(proglen); + if (!image) { + printf("Out of memory\n"); + return NULL; + } + memset(image, 0, proglen); + tmp = ptr = haystack + off; - while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) { + while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) { tmp = NULL; if (!strstr(ptr, "JIT code")) continue; @@ -208,10 +220,12 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, ptr = pptr; do { image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16); - if (ptr == pptr || ulen >= ilen) { + if (ptr == pptr) { ulen--; break; } + if (ulen >= proglen) + break; ptr = pptr; } while (1); } @@ -222,7 +236,8 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, printf("%lx + :\n", base); regfree(®ex); - return ulen; + *ilen = ulen; + return image; } static void usage(void) @@ -237,12 +252,12 @@ static void usage(void) int main(int argc, char **argv) { unsigned int len, klen, opt, opcodes = 0; - static uint8_t image[32768]; char *kbuff, *file = NULL; char *ofile = NULL; int ofd; ssize_t nr; uint8_t *pos; + uint8_t *image = NULL; while ((opt = getopt(argc, argv, "of:O:")) != -1) { switch (opt) { @@ -262,7 +277,6 @@ int main(int argc, char **argv) } bfd_init(); - memset(image, 0, sizeof(image)); kbuff = get_log_buff(file, &klen); if (!kbuff) { @@ -270,8 +284,8 @@ int main(int argc, char **argv) return -1; } - len = get_last_jit_image(kbuff, klen, image, sizeof(image)); - if (len <= 0) { + image = get_last_jit_image(kbuff, klen, &len); + if (!image) { fprintf(stderr, "No JIT image found!\n"); goto done; } @@ -301,5 +315,6 @@ int main(int argc, char **argv) done: put_log_buff(kbuff); + free(image); return 0; } -- 2.9.4