Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2999995pxb; Fri, 5 Nov 2021 08:09:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7ViXRVFYM2mPlYiNR24rIQY59NN7aHz1QZZBwHEClgTmXXmdhUYNovRYZ0h+K2x7JOBBL X-Received: by 2002:aa7:d4d4:: with SMTP id t20mr16352986edr.374.1636124973528; Fri, 05 Nov 2021 08:09:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636124973; cv=none; d=google.com; s=arc-20160816; b=o3dOC+ZXuj1KD06sPXG05CYuYw0XVCvEBNv371I/w6CeQIII20U9vT96p5stIGpjN8 DsKPuDAbbNbkiQJdmPyT/vgT7duESYDPxsTKN3oQnwQQ0Oy8ouLKu7pzuKt5MTxQJnMD 3YPWci6wQR0n5oyGgtUmQ+0pPYvuRHXvpM7CRj7OfnDsY5v53soAJ5RsUZQbYyaG3buV gOcc84gf98JGaGEBaMgl5QZ5lMy6ALkk6BI40t174l54D+RuZJkj2k2R1bA9ZB6vXB8U e6SbB1MiF9r4stdRtmiZnu9+aHivFW++sjnLzwbGcoRs8vAAyWQvjhe9ojLk2/3vEr5f 71CQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=AMaBSs4QaGn7q0+DdTvTOHBM1/9cehbfR6xMtGSpL0M=; b=leK8StlWbpYv/r2qti2GocPbmAmvUQfDfqhsdSgHFc+OBRDvEAn8w1efzX6GdQPPEk CL+USmHQFsmYTlUf1X5ca7RfsJ+M1bcGs57HaMGs+ljIsUjOQE2c/eMfboUTeZnwwGxo OF/V2vdNP78fMIS0EjeM4MIWhe1aVu3ixyF/pz0f2j9F3w2uHm2OsqB2sB5PVoTAc/a1 RQzmyjU0A4Bhf/1P1eSH46ymvUZkxjA6Q5tFQFR3diuwpMhOKCaTpd74+g/7+MZSYyeh eCWU7L1OmwA5J+ex2e15Ce0gd2tDMiqVEpLP1FArsxw4/xucqEzTSrxNDMUS/3I6prHl Vo1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=0eqejjXp; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=gSgRN3Bg; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dr2si2600628ejc.435.2021.11.05.08.09.08; Fri, 05 Nov 2021 08:09:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=0eqejjXp; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=gSgRN3Bg; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233423AbhKEPJu (ORCPT + 67 others); Fri, 5 Nov 2021 11:09:50 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:41072 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233385AbhKEPJt (ORCPT ); Fri, 5 Nov 2021 11:09:49 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id BABE21FD36; Fri, 5 Nov 2021 15:07:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1636124828; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AMaBSs4QaGn7q0+DdTvTOHBM1/9cehbfR6xMtGSpL0M=; b=0eqejjXpz+3Ix5Wg7oNyqR5PZ+l741fE9XMdmTko3h9pjEI1xkYPJhX0fIQyD+MiEZvgA+ QGdEEWOSCPT9jZPdQHxgVUhvPotTni69wC9vMq2st6ln0sEkDbE62zpijN1DsML9Ve1RGa kWhXYec8Vcvljtq86/7xg1mHch+VuwA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1636124828; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AMaBSs4QaGn7q0+DdTvTOHBM1/9cehbfR6xMtGSpL0M=; b=gSgRN3BgoOSFYiW+DYlQMXcqjkw1jgz6LH1AkrG5wkRm/26rgSgDay6WL4k0moFQ01lYwO OB0QdFPCkt4BOGAQ== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id ABC1B2C150; Fri, 5 Nov 2021 15:07:08 +0000 (UTC) Date: Fri, 05 Nov 2021 16:07:08 +0100 Message-ID: From: Takashi Iwai To: Pkshih Cc: "kvalo@codeaurora.org" , "linux-wireless@vger.kernel.org" , "Larry.Finger@gmail.com" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] rtw89: Fix crash by loading compressed firmware file In-Reply-To: References: <20211105071725.31539-1-tiwai@suse.de> <87zgqjqaae.fsf@codeaurora.org> <87v917q8hw.fsf@codeaurora.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org On Fri, 05 Nov 2021 15:28:39 +0100, Pkshih wrote: > > On Fri, 2021-11-05 at 11:03 +0200, Kalle Valo wrote: > > Takashi Iwai writes: > > > > > On Fri, 05 Nov 2021 09:25:13 +0100, > > > Kalle Valo wrote: > > > > Takashi Iwai writes: > > > > > > > > > On Fri, 05 Nov 2021 08:17:25 +0100, > > > > > Takashi Iwai wrote: > > > > > > When a firmware is loaded in the compressed format or via user-mode > > > > > > helper, it's mapped in read-only, and the rtw89 driver crashes at > > > > > > rtw89_fw_download() when it tries to modify some data. > > > > > > > > > > > > This patch is an attemp to avoid the crash by re-allocating the data > > > > > > via vmalloc() for the data modification. > > > > > > > > > > Alternatively, we may drop the code that modifies the loaded firmware > > > > > data? At least SET_FW_HDR_PART_SIZE() in rtw89_fw_hdr_parser() looks > > > > > writing it, and I have no idea why this overwrite is needed. > > > > > > > > Strange, isn't the firmware data marked as const just to avoid this kind > > > > of problem? Does rtw89 have wrong casts somewhere which removes the > > > > const? > > > > > > Yes. SET_FW_HDR_PART_SIZE() does the cast, dropping the const. > > > > Oh man, all of GET and SET macros in fw.h have those casts: > > > > #define GET_FW_HDR_MAJOR_VERSION(fwhdr) \ > > le32_get_bits(*((__le32 *)(fwhdr) + 1), GENMASK(7, 0)) > > #define GET_FW_HDR_MINOR_VERSION(fwhdr) \ > > le32_get_bits(*((__le32 *)(fwhdr) + 1), GENMASK(15, 8)) > > #define GET_FW_HDR_SUBVERSION(fwhdr) \ > > le32_get_bits(*((__le32 *)(fwhdr) + 1), GENMASK(23, 16)) > > > > I don't know how I missed those during my review :( But this is exactly > > why I prefer having a proper struct for commands and events, instead of > > u8 buf used with these macros. > > > > > I can use a struct to access firmware header, becuase their fields > are multiple of 8 bits. > > But, the "firmware section header" that is additional header followed > by firmware header, and it contains bit fields, likes: > > #define GET_FWSECTION_HDR_SEC_SIZE(fwhdr) \ > le32_get_bits(*((__le32 *)(fwhdr) + 1), GENMASK(23, 0)) > #define GET_FWSECTION_HDR_CHECKSUM(fwhdr) \ > le32_get_bits(*((__le32 *)(fwhdr) + 1), BIT(28)) > #define GET_FWSECTION_HDR_REDL(fwhdr) \ > le32_get_bits(*((__le32 *)(fwhdr) + 1), BIT(29)) > #define GET_FWSECTION_HDR_DL_ADDR(fwhdr) \ > le32_get_bits(*((__le32 *)(fwhdr)), GENMASK(31, 0)) > > If we use a struct, it needs big-/little- endians parts. > > Then, we will access firmware header with two methods; is > it reasonable? You should put const in the cast in le32_get_bits() invocations, at least. For the le32_replace_bits(), ideally it should be rewritten in some better way the compiler can catch. e.g. use an inline function to take a void * argument without const, static inline void RTW89_SET_FWCMD_CXRFK_TYPE(void *cmd, unsigned int val) { le32p_replace_bits((__le32 *)((u8 *)(cmd) + 2), val, GENMASK(17, 10)); } Then the compiler will warn when you pass a const pointer there. BTW, while reading your reply, I noticed that it's an unaligned access to a 32bit value, which is another potential breakage on some architectures. So the whole stuff has to be rewritten in anyway... > The macro SET_FW_HDR_PART_SIZE() is used to set the firmware > partition size we are going to download, and it is only used > by rtw89_fw_download_hdr(). So, I will set the partition size > after copying constant firmware header into skb->data. Sounds good. thanks, Takashi