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=-18.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=ham 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 95156C433F5 for ; Fri, 3 Sep 2021 21:56:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2D4566108E for ; Fri, 3 Sep 2021 21:56:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2D4566108E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id A3E8C900002; Fri, 3 Sep 2021 17:56:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9ED466B0072; Fri, 3 Sep 2021 17:56:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B487900002; Fri, 3 Sep 2021 17:56:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7CA976B0071 for ; Fri, 3 Sep 2021 17:56:33 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 39D302B35A for ; Fri, 3 Sep 2021 21:56:33 +0000 (UTC) X-FDA: 78547621866.36.96C6CFA Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) by imf14.hostedemail.com (Postfix) with ESMTP id 053A46001981 for ; Fri, 3 Sep 2021 21:56:32 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id f15so1090448ybg.3 for ; Fri, 03 Sep 2021 14:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zG5/7AV9BzevEKzJQKSrnmTBIp1ROnBD6nq2xHoY4wg=; b=kFkSIiu5R2dx6dF1mtriwDnLYB3riLPv3xSIambOd/wio1cGYvXWPu65Mk4C6WBuN+ mRcqBD34nOnJdi1ZkqeLWw6Al9MkAvElp6zXiUx/gZBqb1plytyUGRay7F72mEEyfHLw qPGLVGfxwreGIUCMBQ1dA24p/Nih73FDf5fMOSlNkcyu1zsiMYqYKM6xENDs9mRNSfrt qiaPu2cOc7NANF/FcEXMkaZEAu2abAIKsLtuf7xiDLmsv7SRB57+q3x3ErQl3ZfdGF+K mIrLanpaYYWSqCGILhdt06H1ZytO5wpkX8rf6ALNMKThmri0pCz2viNiA4S7KAEx0JSe sorA== 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=zG5/7AV9BzevEKzJQKSrnmTBIp1ROnBD6nq2xHoY4wg=; b=Ti4gH4JU8vt4zX/IpNq+wBhm7qLqEQlBXfjV/Nj8gJeX1Ac/qbePbtHDTX3IiCWVg/ nGLFwjI8elWPJo2XQl6mgv+BY29H451BQeoE3WBII0WCHkmcLUD7swymy0Lm+9GcVtVz GPZGS9SI6CJ7J8AgZX/RcvDdmilRyryQTktpKdBYSERr4Eq/lYM0FRE019WPgfzv+r38 nYiKoFhWumpX9UV3W/yjf+sZgDqd44Gv1LHyFMZWG/jxvWQ5Jaj3feKlb9xQlTODEd30 t4rSvwCgL+lBjr4eGPdtxMT2l4cxpbFryGM+qZMOjuNmE85mSp99GGznXrBZGET+Nl4I vnVw== X-Gm-Message-State: AOAM530RnFRyuOmVBTzPC6vi7PYIDGQHTNEqM7RdcWAj2WTUtcfZwhXK d3O66BB6jkWqhPv7H4I/tKvZYqnqMTsySfHFnrbsIQ== X-Google-Smtp-Source: ABdhPJzz7uw4W54vK73OFEef5oMMxi0ghWudIDn5FIoOhEpBC3oPOTJGuoQEDqHr84k9mmpwgrr/M+gaBSMIwsQF2bY= X-Received: by 2002:a05:6902:1247:: with SMTP id t7mr1542942ybu.161.1630706192001; Fri, 03 Sep 2021 14:56:32 -0700 (PDT) MIME-Version: 1.0 References: <20210902231813.3597709-1-surenb@google.com> <20210902231813.3597709-2-surenb@google.com> <202109031439.B58932AF0@keescook> In-Reply-To: <202109031439.B58932AF0@keescook> From: Suren Baghdasaryan Date: Fri, 3 Sep 2021 14:56:21 -0700 Message-ID: Subject: Re: [PATCH v9 2/3] mm: add a field to store names for private anonymous memory To: Kees Cook Cc: Andrew Morton , Colin Cross , Sumit Semwal , Michal Hocko , Dave Hansen , Matthew Wilcox , "Kirill A . Shutemov" , Vlastimil Babka , Johannes Weiner , Jonathan Corbet , Al Viro , Randy Dunlap , Kalesh Singh , Peter Xu , rppt@kernel.org, Peter Zijlstra , Catalin Marinas , vincenzo.frascino@arm.com, =?UTF-8?B?Q2hpbndlbiBDaGFuZyAo5by16Yym5paHKQ==?= , Axel Rasmussen , Andrea Arcangeli , Jann Horn , apopple@nvidia.com, John Hubbard , Yu Zhao , Will Deacon , fenghua.yu@intel.com, thunder.leizhen@huawei.com, Hugh Dickins , feng.tang@intel.com, Jason Gunthorpe , Roman Gushchin , Thomas Gleixner , krisman@collabora.com, chris.hyser@oracle.com, Peter Collingbourne , "Eric W. Biederman" , Jens Axboe , legion@kernel.org, Rolf Eike Beer , Cyrill Gorcunov , Muchun Song , Viresh Kumar , Thomas Cedeno , sashal@kernel.org, cxfcosmos@gmail.com, Rasmus Villemoes , LKML , linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm , kernel-team Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kFkSIiu5; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of surenb@google.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=surenb@google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 053A46001981 X-Stat-Signature: op5wah4a1i3rax91wucxzazk8d8gk18y X-HE-Tag: 1630706192-853492 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 Fri, Sep 3, 2021 at 2:47 PM Kees Cook wrote: > > (Sorry, a few more things jumped out at me when I looked again...) > > On Thu, Sep 02, 2021 at 04:18:12PM -0700, Suren Baghdasaryan wrote: > > [...] > > diff --git a/kernel/sys.c b/kernel/sys.c > > index 72c7639e3c98..25118902a376 100644 > > --- a/kernel/sys.c > > +++ b/kernel/sys.c > > @@ -2299,6 +2299,64 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, > > > > #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) > > > > +#ifdef CONFIG_MMU > > + > > +#define ANON_VMA_NAME_MAX_LEN 256 > > + > > +static inline bool is_valid_name_char(char ch) > > +{ > > + /* printable ascii characters, except [ \ ] */ > > + return (ch > 0x1f && ch < 0x5b) || (ch > 0x5d && ch < 0x7f); > > +} > > In the back of my mind, I feel like disallowing backtick would be nice, > but then if $, (, and ) are allowed, it doesn't matter, and that seems > too limiting. :) It's not used by the only current user (Android) and we can always allow more chars later. However going the other direction and disallowing some of them I think would be harder (need to make sure nobody uses them). WDYT if we keep it stricter and relax if needed? > > > + > > +static int prctl_set_vma(unsigned long opt, unsigned long addr, > > + unsigned long size, unsigned long arg) > > +{ > > + struct mm_struct *mm = current->mm; > > + const char __user *uname; > > + char *name, *pch; > > + int error; > > + > > + switch (opt) { > > + case PR_SET_VMA_ANON_NAME: > > + uname = (const char __user *)arg; > > + if (!uname) { > > + /* Reset the name */ > > + name = NULL; > > + goto set_name; > > + } > > + > > + name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN); > > + > > + if (IS_ERR(name)) > > + return PTR_ERR(name); > > + > > + for (pch = name; *pch != '\0'; pch++) { > > + if (!is_valid_name_char(*pch)) { > > + kfree(name); > > + return -EINVAL; > > + } > > + } > > +set_name: > > + mmap_write_lock(mm); > > + error = madvise_set_anon_name(mm, addr, size, name); > > + mmap_write_unlock(mm); > > + kfree(name); > > + break; > > This is a weird construct with a needless goto. Why not: > > switch (opt) { > case PR_SET_VMA_ANON_NAME: > uname = (const char __user *)arg; > if (uname) { > name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN); > if (IS_ERR(name)) > return PTR_ERR(name); > > for (pch = name; *pch != '\0'; pch++) { > if (!is_valid_name_char(*pch)) { > kfree(name); > return -EINVAL; > } > } > } else { > /* Reset the name */ > name = NULL; > } > mmap_write_lock(mm); > error = madvise_set_anon_name(mm, addr, size, name); > mmap_write_unlock(mm); > kfree(name); > break; Yeah, I was contemplating one way or the other (less indents vs clear flow) and you convinced me :) Will change in the next rev. Thanks for the review! > > > -- > Kees Cook