From patchwork Wed Aug 22 13:22:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 960965 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="c1XxSt2o"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41wSrp2Kklz9s5c for ; Wed, 22 Aug 2018 23:22:34 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729054AbeHVQrU (ORCPT ); Wed, 22 Aug 2018 12:47:20 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:32811 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728803AbeHVQrU (ORCPT ); Wed, 22 Aug 2018 12:47:20 -0400 Received: by mail-lf1-f66.google.com with SMTP id c7-v6so1395835lfe.0 for ; Wed, 22 Aug 2018 06:22:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id; bh=AZbPCJSbuo+klDBiBLb68ajVcTgBIaGryLAqoz1zbFg=; b=c1XxSt2o2MKrhLyRBXaEtIfAcbI419co9oJDsRtm/1ugtcaVk1NmSKqrTq1jUJjn1h 8miGzVkWXgpsmSKqx7XshYhc/cCao2Qb0Sm6qaG/YjOhJGRHCdp3jKv4Wu/GbwST7OAE Ko5WAhrifPAUcuB7fvHb/Rwa6lxNT5BUQyC+I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=AZbPCJSbuo+klDBiBLb68ajVcTgBIaGryLAqoz1zbFg=; b=n1Q7KXoKMiOA31wNGI9pz17zf8EY95DDIiDMgkSfQUFfsPd4XEpg05E4aX9xTzFafz YOKTDKqKzG3V5nadctodbahya6IE/AoWL7CBax1dHv/iEARPR62lFgIlz2wCkcMSBB/z WeBQ7VHzYTW6XDlwgm9km06XdNteiqTFcdb5AOdlmOGQvNgsDmFaDf8XhW5tdT4OHrBO WolhPbLfFSw8aMDDkjFs3MNgU6wHL/RIWoTCWSvmdJZ3WaHogEaOWmB85TAQ8TibFOVM g2hBHm1oV+4H2CFgwthCcyIJIfpjWFfiaNjxJRhURlSMVltLZ/QS0wEFilmY1P1pOs3V qB8Q== X-Gm-Message-State: APzg51Af10aupNbkXg1e9l+EB53DujOxMzXAPp3lbTpYPm8w3Aj4ZPRd FVOJNzgkbDcOptZ72KSur0I2zQ== X-Google-Smtp-Source: ANB0VdZ3jPavtSuzCCpfqzFJ8USRyKgm/35Ri6ufCmVAlYn9DU87oCfj5otS4UyDdVAh1YM1XF0P+Q== X-Received: by 2002:a19:e593:: with SMTP id i19-v6mr654385lfk.18.1534944144439; Wed, 22 Aug 2018 06:22:24 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id i2-v6sm314600lfa.78.2018.08.22.06.22.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 22 Aug 2018 06:22:24 -0700 (PDT) From: Rasmus Villemoes To: Kalle Valo , "David S. Miller" Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-wireless@vger.kernel.org, Rasmus Villemoes Subject: [PATCH] brcmfmac: fix wrong strnchr usage Date: Wed, 22 Aug 2018 15:22:15 +0200 Message-Id: <20180822132215.27669-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.16.4 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org strnchr takes arguments in the order of its name: string, max bytes to read, character to search for. Here we're passing '\n' aka 10 as the buffer size, and searching for sizeof(buf) aka BRCMF_DCMD_SMLEN aka 256 (aka '\0', since it's implicitly converted to char) within those 10 bytes. Just interchanging the last two arguments would still leave a bug, because if we've been successful once, there are not sizeof(buf) characters left after the new value of p. Since clmver is immediately afterwards passed as a %s argument, I assume that it is actually a properly nul-terminated string. For that case, we have strreplace(). Signed-off-by: Rasmus Villemoes --- Incidentally, I found this because I was looking at our strnchr implementation and noticed a corner case: strchr() is specified by C and POSIX standards to consider the trailing '\0' to be part of the string, so strchr(foo, '\0') must be equivalent to "foo + strlen(foo)". strnchr() is not specified by anyone. Our implementation returns NULL as soon as count is depleted or hitting a '\0' byte, so since we were here looking for a '\0' byte, the first strnchr() call was guaranteed to return NULL. Had strnchr() instead returned a pointer to the '\0' if such was found within the first count (in this case 10) bytes, we'd be overwriting that with a ' ', and continue until no '\0' was found within the next 10 bytes. drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c index cd3651069d0c..94044a7a6021 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/common.c @@ -296,9 +296,7 @@ int brcmf_c_preinit_dcmds(struct brcmf_if *ifp) /* Replace all newline/linefeed characters with space * character */ - ptr = clmver; - while ((ptr = strnchr(ptr, '\n', sizeof(buf))) != NULL) - *ptr = ' '; + strreplace(clmver, '\n', ' '); brcmf_dbg(INFO, "CLM version = %s\n", clmver); }