Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp5710314imm; Tue, 16 Oct 2018 14:58:19 -0700 (PDT) X-Google-Smtp-Source: ACcGV606E3hyVkQK4eD1Tek/9ZcIElZAf9CZjpx7fvh/m8gnGqk5REMSGPpgZhcGxF9QQ+BxlLz9 X-Received: by 2002:a62:22c7:: with SMTP id p68-v6mr23934997pfj.53.1539727099045; Tue, 16 Oct 2018 14:58:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539727099; cv=none; d=google.com; s=arc-20160816; b=zDMli0VRaSITGf3mmYPTKe3c2WgHvdJoPgW26qhfnt22wb5LXXORb91gY8dhE5tLBc c81zRgkHqZ225EvHcdpZb/Qp2YR4Mu8SdvxuihOGRfOO4dpSOzZKGbOxHFIpB3nSCsZJ SvAypmxxaSqLaZnwpyOqGKQLZZg6DUsC+fVjQuzyBVN60L0wtxvs4t5vlLi/jr9rjvnA EFI7hdcWMC4KFJti8WWGluKKvFb9TW00oo8RL/QtG/tR/EkOXmQWcTAHbeO+EyTDTSrR 3SboA3C4SWeH+FsGP4IxtvGRIfycd4tWN1Qcgp/fZUutpj9YqCgG1tleEooH9hdiOR6X UR5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature; bh=zGS25YocjFzs0rNN+PJEzpXfG8MY1k8ajW9x/zVmPzY=; b=gkNPXoC7LWozHB1pf8xL1/M9SSb3w4Dl7sgV0r2qnSHsE1c9CZhqs38Z5xFc604sTF doKVaLmHy8h68xRK8C+iHMoTK/zC5uKcGvTmlfE8WWNwj73j1WWwXfszsO/5tT/1eXGQ 85vskiqudQx2qoV80PIO0tpxE3+igtjGu0h7gxXKSZRrS9rSnnrK+VlVlcfhBBVs0N33 rLgNWW3Tt/OmUPh24yScbX8z38T4HYfxFrOn/4qexPDeYqp4G03+fq5u58xqlLBjvBWA TNDOyjdrIsd4+FdLHR4OaVssn3ndu86RqXtP735fV7YivLxUWvzbo0hsRTH9WiFGP3G/ V+hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CK1Txqga; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i23-v6si15297340pfj.269.2018.10.16.14.58.03; Tue, 16 Oct 2018 14:58:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CK1Txqga; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727169AbeJQFtl (ORCPT + 99 others); Wed, 17 Oct 2018 01:49:41 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:32889 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726935AbeJQFtk (ORCPT ); Wed, 17 Oct 2018 01:49:40 -0400 Received: by mail-wr1-f68.google.com with SMTP id e4-v6so27324175wrs.0 for ; Tue, 16 Oct 2018 14:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=zGS25YocjFzs0rNN+PJEzpXfG8MY1k8ajW9x/zVmPzY=; b=CK1Txqga0cNAiFNQRHsEkuXDG1ki5RgYKTSpyLJxYpStdnzgvsZsg5PYeis/fYMa7m 7U/0paNM3vVFQZAmc/LuOpi7Ain518Rsr9N1JAHLfUQWus7j+ms4p6d7/h9fMkYnSiGc xqc4ZyCFMq2aj2xC81AOMj6B0eQEoc7zJUmYA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=zGS25YocjFzs0rNN+PJEzpXfG8MY1k8ajW9x/zVmPzY=; b=SyU17ZcPex6c0gtTrSGFBYGO0UDPXf0XhBR4hWO9jFHptmwmR2oIoD7Vbzx2mQOZFJ ua3jLSM2CFESEUYoPZSUuo+zmLdwkrWZZW+X3txJZEvKFViv8720uTXdvV7mbHNchi/P y0201mOYrvTSBcYlgWzmNMa2jNhp7rQoksqgx7Gz7VUY7L5yK2JUtDVGUeguYOAeQGc2 iFKUHNftYxixQlFd3SPq0MnxBWKdHn1QMWsQH/uYp412aLQMdCli7FD/H80J407EOQ7J FDeoTH6oXSZku9UyMe2+uZ5ma2ch/yjJoESGmvykGlze7hFkqvInnUIE8SpxAnSjgcVf tjbA== X-Gm-Message-State: ABuFfojE13yAeQtlSuNYbHXt+JKe1t/1vX1drr3rM0CcOW3bT+F37Nlx wrI+r5m93T42bSa5G32KU2hpJ883agEjFg9aeuzlJQ== X-Received: by 2002:adf:a194:: with SMTP id u20-v6mr12799777wru.50.1539727033716; Tue, 16 Oct 2018 14:57:13 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a1c:b485:0:0:0:0:0 with HTTP; Tue, 16 Oct 2018 14:57:12 -0700 (PDT) In-Reply-To: <20181009222042.9781-1-joel@joelfernandes.org> References: <20181009222042.9781-1-joel@joelfernandes.org> From: John Stultz Date: Tue, 16 Oct 2018 14:57:12 -0700 Message-ID: Subject: Re: [PATCH v2 1/2] mm: Add an F_SEAL_FS_WRITE seal to memfd To: "Joel Fernandes (Google)" Cc: lkml , Android Kernel Team , John Reck , Todd Kjos , Greg KH , Andrew Morton , Daniel Colascione , "J. Bruce Fields" , Jeff Layton , Khalid Aziz , linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm , Mike Kravetz , Minchan Kim , Shuah Khan Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 9, 2018 at 3:20 PM, Joel Fernandes (Google) wrote: > Android uses ashmem for sharing memory regions. We are looking forward > to migrating all usecases of ashmem to memfd so that we can possibly > remove the ashmem driver in the future from staging while also > benefiting from using memfd and contributing to it. Note staging drivers > are also not ABI and generally can be removed at anytime. > > One of the main usecases Android has is the ability to create a region > and mmap it as writeable, then drop its protection for "future" writes > while keeping the existing already mmap'ed writeable-region active. > This allows us to implement a usecase where receivers of the shared > memory buffer can get a read-only view, while the sender continues to > write to the buffer. See CursorWindow in Android for more details: > https://developer.android.com/reference/android/database/CursorWindow > > This usecase cannot be implemented with the existing F_SEAL_WRITE seal. > To support the usecase, this patch adds a new F_SEAL_FS_WRITE seal which > prevents any future mmap and write syscalls from succeeding while > keeping the existing mmap active. The following program shows the seal > working in action: > > int main() { > int ret, fd; > void *addr, *addr2, *addr3, *addr1; > ret = memfd_create_region("test_region", REGION_SIZE); > printf("ret=%d\n", ret); > fd = ret; > > // Create map > addr = mmap(0, REGION_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); > if (addr == MAP_FAILED) > printf("map 0 failed\n"); > else > printf("map 0 passed\n"); > > if ((ret = write(fd, "test", 4)) != 4) > printf("write failed even though no fs-write seal " > "(ret=%d errno =%d)\n", ret, errno); > else > printf("write passed\n"); > > addr1 = mmap(0, REGION_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); > if (addr1 == MAP_FAILED) > perror("map 1 prot-write failed even though no seal\n"); > else > printf("map 1 prot-write passed as expected\n"); > > ret = fcntl(fd, F_ADD_SEALS, F_SEAL_FS_WRITE); > if (ret == -1) > printf("fcntl failed, errno: %d\n", errno); > else > printf("fs-write seal now active\n"); > > if ((ret = write(fd, "test", 4)) != 4) > printf("write failed as expected due to fs-write seal\n"); > else > printf("write passed (unexpected)\n"); > > addr2 = mmap(0, REGION_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); > if (addr2 == MAP_FAILED) > perror("map 2 prot-write failed as expected due to seal\n"); > else > printf("map 2 passed\n"); > > addr3 = mmap(0, REGION_SIZE, PROT_READ, MAP_SHARED, fd, 0); > if (addr3 == MAP_FAILED) > perror("map 3 failed\n"); > else > printf("map 3 prot-read passed as expected\n"); > } > > The output of running this program is as follows: > ret=3 > map 0 passed > write passed > map 1 prot-write passed as expected > fs-write seal now active > write failed as expected due to fs-write seal > map 2 prot-write failed as expected due to seal > : Permission denied > map 3 prot-read passed as expected > > Note: This seal will also prevent growing and shrinking of the memfd. > This is not something we do in Android so it does not affect us, however > I have mentioned this behavior of the seal in the manpage. > > Cc: jreck@google.com > Cc: john.stultz@linaro.org > Cc: tkjos@google.com > Cc: gregkh@linuxfoundation.org > Signed-off-by: Joel Fernandes (Google) Reviewed-by: John Stultz thanks -john