Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp2811741ima; Mon, 22 Oct 2018 16:49:53 -0700 (PDT) X-Google-Smtp-Source: AJdET5c33TS0715/292ux6gZeRkETLDMVikwUDTF9NlS+FRQScDq0uajxLm4J5aJGyGiTDc9BbQo X-Received: by 2002:a63:f306:: with SMTP id l6-v6mr11938795pgh.255.1540252193943; Mon, 22 Oct 2018 16:49:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540252193; cv=none; d=google.com; s=arc-20160816; b=rInE/0DN0uzvculAcx13ot6prED1/SIvose4tkcjrzSljt805v/hddLe//1dR9oMVe yBW10KDOfIbxy/J/RCzF5nTCPhVbjvktM1q4yoVPFgp5QBvcsPEojQDHpPODAmTxgYHw yFKAuhA//4Kki/kv/1dX9OYrr+D61mM4BNZUhkORxSiDDqpQZnXEbXoBVGgfwF9L/MEr no0CGXYnd6M2jDx7+K6IoQgQAWuQ/jyOy7vHNWWOV3dEVLuCqhIdxnyCj1YvqIKyaqJQ qa5flR2eaXzSFCymT8Cdevm0FNbL+e9lJTIAHjTcwtGRCOc0Ej4hRGvPJpr6ISamGRPj FKvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=XDtkWfJX6UOD3pDo61X4U4dXHBPp3rHlTaGgivs5jOQ=; b=sZB5/a2ol8UQrdeymamZABiNFPjO332ZUHOo2ntN/rjz5KaUtyi8QUMpRFUSTEuEBL pm23sACR2DYGFeS00Ag6d0Ie6O+ArDIbKm2aCnOXHlbo5xPlKqKMAi59nZyHdlEzVUoA wUfizhU71CiQCXlmbvMXN4/RVSu9qDxRZrYxTkieH/FRCxmpFpDawL/T2r3f/ggER/mD ZZqWaER0LKRKK+r7Bnxf6a4/Ej6mfLYvfkLlvrVlzHkuvSchetSnMsI5GyLy1A2EEg17 18LLGbGgn0lnQvzsmgM4XAss/L4hwhKLvrSoE2+TNPJT8v3Zm078q1qX/5gJPEeRKYMi BwAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=aWvu7mLN; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p17-v6si34574434pgh.515.2018.10.22.16.49.39; Mon, 22 Oct 2018 16:49:53 -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=@joelfernandes.org header.s=google header.b=aWvu7mLN; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726570AbeJWIJy (ORCPT + 99 others); Tue, 23 Oct 2018 04:09:54 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:33708 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725799AbeJWIJx (ORCPT ); Tue, 23 Oct 2018 04:09:53 -0400 Received: by mail-pf1-f196.google.com with SMTP id 78-v6so18321488pfq.0 for ; Mon, 22 Oct 2018 16:49:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=XDtkWfJX6UOD3pDo61X4U4dXHBPp3rHlTaGgivs5jOQ=; b=aWvu7mLNfAVBbY1hY/XceAAANDRgy+a3Y+d4pv8GNPenyXA4/s4UQxx3vPRbpkG4dw IiJPhfR3ctTUU6iO3cC2Jtp3OMfilRD0l3t1H7zO8C2i6ibWDo7vhzlQTJ/56auDYfLW sCdLLKL1j8wn7iHHg9AW7WD3WqzlPIVhPvWtI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=XDtkWfJX6UOD3pDo61X4U4dXHBPp3rHlTaGgivs5jOQ=; b=CulPVtbK/xkM7ypUJaU0wLub/ouL3/x3ldrAyDuRvCFL2lXwKn9A2dTzx9MxtuUaFO 8EMdFe/Zu8Lm71ctSohdV8Wgl7DDtrR0wbzIRlSqsTrTrq2WYGtPeM6zO4qa+z5P1+Ch 0eYbsqkO1zNFRA5WKrNsSIPEcFSkWsmxgk3PUcZqmfO3WtCffRMUsWp8z+KFEGL3Gbm9 LDCGb8L19fFjY+F1K0DgB+hlQDVkFnRL4TN3XNf//FngkWma9BLNzitk8CXY4mxLwkAQ /dwYBk8L5g6tb2OlsqjBZHKyTh33+Jx8/FkIgoCPf62YpKukz+jGXnvRKtX+djuqzID2 fbTw== X-Gm-Message-State: ABuFfoiwVkus+hZXAgg1qUxS+bIZ7KpyHzoKfaYt/as7KvlgEkxVGfoL BdaKiKXxfPzw8DNpp17nm0XGVLPjI6s= X-Received: by 2002:a62:4301:: with SMTP id q1-v6mr46658364pfa.163.1540252148829; Mon, 22 Oct 2018 16:49:08 -0700 (PDT) Received: from localhost ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id r76-v6sm42460515pgr.11.2018.10.22.16.49.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 Oct 2018 16:49:07 -0700 (PDT) Date: Mon, 22 Oct 2018 16:49:06 -0700 From: Joel Fernandes To: linux-kernel@vger.kernel.org Cc: john.stultz@linaro.org, tkjos@google.com, gregkh@linuxfoundation.org, hch@infradead.org, Al Viro , Andrew Morton , dancol@google.com, "J. Bruce Fields" , Jeff Layton , linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, marcandre.lureau@redhat.com, Mike Kravetz , minchan@kernel.org, Shuah Khan , Thomas Gleixner Subject: Re: [PATCH v3 1/2] mm: Add an F_SEAL_FUTURE_WRITE seal to memfd Message-ID: <20181022234906.GA22110@joelaf.mtv.corp.google.com> References: <20181018065908.254389-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181018065908.254389-1-joel@joelfernandes.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 17, 2018 at 11:59:07PM -0700, 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 add protection against making any > "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 documentation 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_FUTURE_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: > > #include > #include > #include > #include > #include > #include > #include > #define F_SEAL_FUTURE_WRITE 0x0010 > #define REGION_SIZE (5 * 1024 * 1024) > > int memfd_create_region(const char *name, size_t size) > { > int ret; > int fd = syscall(__NR_memfd_create, name, MFD_ALLOW_SEALING); > if (fd < 0) return fd; > ret = ftruncate(fd, size); > if (ret < 0) { close(fd); return ret; } > return fd; > } > > 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 future-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_FUTURE_WRITE | > F_SEAL_GROW | > F_SEAL_SHRINK); > if (ret == -1) > printf("fcntl failed, errno: %d\n", errno); > else > printf("future-write seal now active\n"); > > if ((ret = write(fd, "test", 4)) != 4) > printf("write failed as expected due to future-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 > future-write seal now active > write failed as expected due to future-write seal > map 2 prot-write failed as expected due to seal > : Permission denied > map 3 prot-read passed as expected > > Cc: jreck@google.com > Cc: john.stultz@linaro.org > Cc: tkjos@google.com > Cc: gregkh@linuxfoundation.org > Cc: hch@infradead.org > Reviewed-by: John Stultz > Signed-off-by: Joel Fernandes (Google) Apologies for the follow-up. Now that merge window has opened, just checking if this patch (which IMO has been beaten to death) can make it for 4.20? Its pretty much completed and is well tested at this point (tests are in 2/2). Then I can move onto other memfd enhancements I'm planning. thanks, - Joel