From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71B54C433E2 for ; Mon, 14 Sep 2020 12:05:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DBFEB20829 for ; Mon, 14 Sep 2020 12:05:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="G1oJRVfA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DBFEB20829 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 55FA38E0001; Mon, 14 Sep 2020 08:05:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E87C6B0037; Mon, 14 Sep 2020 08:05:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 389398E0001; Mon, 14 Sep 2020 08:05:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id 1CC066B0003 for ; Mon, 14 Sep 2020 08:05:46 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B0719180374CB for ; Mon, 14 Sep 2020 12:05:45 +0000 (UTC) X-FDA: 77261537850.15.edge94_191137b27108 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 47CFC180481BB for ; Mon, 14 Sep 2020 12:05:41 +0000 (UTC) X-HE-Tag: edge94_191137b27108 X-Filterd-Recvd-Size: 6015 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Mon, 14 Sep 2020 12:05:40 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id n14so12451150pff.6 for ; Mon, 14 Sep 2020 05:05:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GsOIVPKx7/GKwttOx4gkrBI8E8JMmQp3RWxUAc5wfcY=; b=G1oJRVfApka43Z3M51QifLTqy6wQ/iKgboQET0cTsUGhqa2VvUp8zDM8KlmGoMSTn2 4Nrv8KQBR0s4z0UdeuHvXlON0PxsuBLT2bR5RsbPH5Ds+0mk4ZSITL47v4bUSSrYxRf+ 3JbtyFmqXOdHv5OcDFVVpHHmldqYsBpLMfDiqL+XHgcz8n+Rw6WA48PDWy7xI28TERZO OX3Ivrdkp571bX+Qwd4tBKr+MUvOr2dhIor2S9JPGrGrcy3jqr9YXy3I4nvalNuVLHrq pXLybs29hC36HQhOnDUctrx6f05OvpzbtddhDB62gOnd0DXz75rX20WGUGWvRC2TiNCL 7h3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GsOIVPKx7/GKwttOx4gkrBI8E8JMmQp3RWxUAc5wfcY=; b=AwFkSnSBdbD9vYhApbGhn7UfvJomzoI48Tj0NUH8GGVV+toIQxarst+yDeQXZ4qbu2 53h5NEMBgZODVYAQDvNHLQhfNZdwVuwJ+0CcTFp/hqlB76A28xyEcuvFlWWMfv0BH+wp O4MCRabUOMmZhHNjbvMnjrTe3Pr0bVT8+a4q9xw3omkopGnAWbPvlN+wNzx8DXOo1Cel 2fQ+dxk6xdZJO3iMaHMYBy6+4DnpvysIifTfnZAwcU6D5rsDw8AdUrwind0fkun8MajR aNHyE6egUvRqRNaqGwZIf0V03aHaqhiDAJw9X3p1/ucdygOQq298ZqpBtkz01bSApI3F 9tQw== X-Gm-Message-State: AOAM530Y/fUDLNfLnagslbV8ItYTLnvlAW4UwsWu7cRU8X826JGcUedq aeh5l2VEF8o7+BIYxiwLEFmiYnEefz74YwUXmV69OA== X-Google-Smtp-Source: ABdhPJy0PPAh9+CRT36/WnNcBPpBRgPC600c1hQtAsuYJZS5GFpKJtb5l5v2LAjd1XIksmtFfFkruvL0x/daNTrYq1o= X-Received: by 2002:a17:902:a605:b029:d0:cbe1:e714 with SMTP id u5-20020a170902a605b02900d0cbe1e714mr14271096plq.34.1600085139550; Mon, 14 Sep 2020 05:05:39 -0700 (PDT) MIME-Version: 1.0 References: <20200912155100.25578-1-songmuchun@bytedance.com> <20200912174241.eeaa771755915f27babf9322@linux-foundation.org> <20200914091844.GE16999@dhcp22.suse.cz> <20200914103205.GI16999@dhcp22.suse.cz> <20200914115724.GO16999@dhcp22.suse.cz> In-Reply-To: <20200914115724.GO16999@dhcp22.suse.cz> From: Muchun Song Date: Mon, 14 Sep 2020 20:05:03 +0800 Message-ID: Subject: Re: [External] Re: [PATCH] mm: memcontrol: Fix out-of-bounds on the buf returned by memory_stat_format To: Michal Hocko Cc: Andrew Morton , Johannes Weiner , Vladimir Davydov , Cgroups , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 47CFC180481BB X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Sep 14, 2020 at 7:57 PM Michal Hocko wrote: > > On Mon 14-09-20 19:46:36, Muchun Song wrote: > > On Mon, Sep 14, 2020 at 6:32 PM Michal Hocko wrote: > > > > > > On Mon 14-09-20 17:43:42, Muchun Song wrote: > > > > On Mon, Sep 14, 2020 at 5:18 PM Michal Hocko wrote: > > > > > > > > > > On Mon 14-09-20 12:02:33, Muchun Song wrote: > > > > > > On Sun, Sep 13, 2020 at 8:42 AM Andrew Morton wrote: > > > > > > > > > > > > > > On Sat, 12 Sep 2020 23:51:00 +0800 Muchun Song wrote: > > > > > > > > > > > > > > > The memory_stat_format() returns a format string, but the return buf > > > > > > > > may not including the trailing '\0'. So the users may read the buf > > > > > > > > out of bounds. > > > > > > > > > > > > > > That sounds serious. Is a cc:stable appropriate? > > > > > > > > > > > > > > > > > > > Yeah, I think we should cc:stable. > > > > > > > > > > Is this a real problem? The buffer should contain 36 lines which makes > > > > > it more than 100B per line. I strongly suspect we are not able to use > > > > > that storage up. > > > > > > > > Before memory_stat_format() return, we should call seq_buf_putc(&s, '\0'). > > > > Otherwise, the return buf string has no trailing null('\0'). But users treat buf > > > > as a string(and read the string oob). It is wrong. Thanks. > > > > > > I am not sure I follow you. vsnprintf which is used by seq_printf will > > > add \0 if there is a room for that. And I argue there is a lot of room > > > in the buffer so a corner case where the buffer gets full doesn't happen > > > with the current code. > > > > Thanks for your explanation. Yeah, seq_printf will add \0 if there is a > > room for that. So I agree with you that the "Fixes" tag is wrong. There > > is nothing to fix. Sorry for the noise. > > > > I think that if someone uses seq_buf_putc(maybe in the feature) at the > > end of memory_stat_format(). It will break the rule and there is no \0. > > So this patch can just make the code robust but need to change the > > commit log and remove the Fixes tag. > > Please see my other reply. Adding \0 is not really sufficient. If we > want to have a robust code to handle the small buffer then we need to > make sure that all counters will make it to the userspace. Short output > is simply a broken result. Implementing this properly is certainly > possible, the question is whether this is worth addressing. It is not > like we are adding a lot of output into this file and it is quite likely > that the code is good as it is. Got it. Thanks. > -- > Michal Hocko > SUSE Labs -- Yours, Muchun