Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp31845pxb; Fri, 15 Jan 2021 05:10:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJzeYVnqjtduV/dLtY2tUt8e2qUb9Ik8ZgS1L3nSs08mBoaxmRc4J5iNdPF4us+hn80VH+fj X-Received: by 2002:a05:6402:3495:: with SMTP id v21mr1297762edc.323.1610716205334; Fri, 15 Jan 2021 05:10:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610716205; cv=none; d=google.com; s=arc-20160816; b=ySuuRvqM8YQTN0RKqM655Z/jK/fslJWjmIoLEMFMdiB070N0Q8Bu3YTr9W/lV+1IXS Ua1mJZpA6MG2g4JEtjiLKjeST+K/GfjiwsbTUweh3f+sJHHRNyRrakuh/JlvkWz5s/OC T5It+FpAbzkL2OYFctyazSTCN3CGna3jWCQ//VGNmTRP5d51GNiHTA2SOcS553cc3l+3 RtI0vla+kftNdAdTJL5SRO2hiku8l1H8Y5xL1GmzSECFyjj3zo+j62Wb3gf0BPN+Tnbp +AhmhUttki3oarcflE3SSmM/b9/wtH+ctnEuzsFnRgWX113/LScaqOolDhs44uSBIs4t egzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wA1GJtfjb2u4y8Dt2x2G/NXImr8bUFn7e5xm1zx+OvU=; b=sdcnxvr9xDGk4dx96DB7E4qkECLcr9W68Dv/mD5g2I7ug6O2MMaDIfUtWWylugiCCr fEyQsn2qaYxjCgf4INnr2EVd4SDqsQ4XkuR3d+7ZrA6yXafC4oy3B58IQDFJmKEDcD0L iFNtY8HASb/fOSibDsbsIQNpaxZQnKNElOQzQjcbtHc5vLVh7ipajFzNZSMs6PSDdRQL ra0At1DBDfIzUO0LBNX2JfP1AgzusOxdljZgoZw8f7b4oubIazjd69O9k8NPpL7nif2o IfIIlfOyUsTaW85GX2UUAGc06swVbxW5BkWjMGJDmYcAQXDgjPbM48yI/PW29MSlEsaf S3Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=c9pirNs+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z9si4182795edr.149.2021.01.15.05.09.41; Fri, 15 Jan 2021 05:10:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=c9pirNs+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388435AbhAONGV (ORCPT + 99 others); Fri, 15 Jan 2021 08:06:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:37326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732584AbhAOMbW (ORCPT ); Fri, 15 Jan 2021 07:31:22 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 20EA223884; Fri, 15 Jan 2021 12:30:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610713806; bh=rTDdodoFS6VeSOmu9ygZvIE0Lxa6/vIcqvRYcAPLnlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c9pirNs+uFq13N7fCfPBWySds/vWTc1Bge2TmuQmBDHnmvpppWiYFayA2z3VHL5Aw rA5SjZ1knwb1wyyqhH0uw/Su7knaC3Aa9TqJya7fQxBlwhRf/VFpY3xuROkDiYkyHh K44YVppolvsCjP8RpUTGo7j/o7NvrF03Q7hP4WM0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Richard Weinberger , Zhihao Cheng , Sudip Mukherjee Subject: [PATCH 4.9 12/25] ubifs: wbuf: Dont leak kernel memory to flash Date: Fri, 15 Jan 2021 13:27:43 +0100 Message-Id: <20210115121957.285357494@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210115121956.679956165@linuxfoundation.org> References: <20210115121956.679956165@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Richard Weinberger commit 20f1431160c6b590cdc269a846fc5a448abf5b98 upstream Write buffers use a kmalloc()'ed buffer, they can leak up to seven bytes of kernel memory to flash if writes are not aligned. So use ubifs_pad() to fill these gaps with padding bytes. This was never a problem while scanning because the scanner logic manually aligns node lengths and skips over these gaps. Cc: Fixes: 1e51764a3c2ac05a2 ("UBIFS: add new flash file system") Signed-off-by: Richard Weinberger Reviewed-by: Zhihao Cheng Signed-off-by: Richard Weinberger [sudip: adjust context] Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman --- fs/ubifs/io.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) --- a/fs/ubifs/io.c +++ b/fs/ubifs/io.c @@ -331,7 +331,7 @@ void ubifs_pad(const struct ubifs_info * { uint32_t crc; - ubifs_assert(pad >= 0 && !(pad & 7)); + ubifs_assert(pad >= 0); if (pad >= UBIFS_PAD_NODE_SZ) { struct ubifs_ch *ch = buf; @@ -721,6 +721,10 @@ int ubifs_wbuf_write_nolock(struct ubifs * write-buffer. */ memcpy(wbuf->buf + wbuf->used, buf, len); + if (aligned_len > len) { + ubifs_assert(aligned_len - len < 8); + ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len); + } if (aligned_len == wbuf->avail) { dbg_io("flush jhead %s wbuf to LEB %d:%d", @@ -813,13 +817,18 @@ int ubifs_wbuf_write_nolock(struct ubifs } spin_lock(&wbuf->lock); - if (aligned_len) + if (aligned_len) { /* * And now we have what's left and what does not take whole * max. write unit, so write it to the write-buffer and we are * done. */ memcpy(wbuf->buf, buf + written, len); + if (aligned_len > len) { + ubifs_assert(aligned_len - len < 8); + ubifs_pad(c, wbuf->buf + len, aligned_len - len); + } + } if (c->leb_size - wbuf->offs >= c->max_write_size) wbuf->size = c->max_write_size;