diff mbox

[ndctl] test, device-dax: test read-only mappings

Message ID 148107449265.11076.1913078641623047618.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Dan Williams Dec. 7, 2016, 1:34 a.m. UTC
Hugh notes:
"I think that is more restrictive than you intended: haven't tried, but
I believe it rejects a PROT_READ, MAP_SHARED, O_RDONLY fd mmap, leaving
no way to mmap /dev/dax without write permission to it."

Reported-by: Hugh Dickins <hughd@google.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 test/device-dax.c |   26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/test/device-dax.c b/test/device-dax.c
index 82154d5c1fff..75b17ed63088 100644
--- a/test/device-dax.c
+++ b/test/device-dax.c
@@ -201,22 +201,22 @@  static int test_device_dax(int loglevel, struct ndctl_test *test,
 	}
 
 	sprintf(path, "/dev/%s", daxctl_dev_get_devname(dev));
-	fd = open(path, O_RDWR);
+	fd = open(path, O_RDONLY);
 	if (fd < 0) {
-		fprintf(stderr, "%s: failed to open device-dax instance\n",
+		fprintf(stderr, "%s: failed to open(O_RDONLY) device-dax instance\n",
 				daxctl_dev_get_devname(dev));
 		rc = -ENXIO;
 		goto out;
 	}
 
-	buf = mmap(NULL, VERIFY_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+	buf = mmap(NULL, VERIFY_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
 	if (buf != MAP_FAILED) {
 		fprintf(stderr, "%s: expected MAP_PRIVATE failure\n", path);
 		rc = -ENXIO;
 		goto out;
 	}
 
-	buf = mmap(NULL, VERIFY_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	buf = mmap(NULL, VERIFY_SIZE, PROT_READ, MAP_SHARED, fd, 0);
 	if (buf == MAP_FAILED) {
 		fprintf(stderr, "%s: expected MAP_SHARED success\n", path);
 		return -ENXIO;
@@ -226,6 +226,24 @@  static int test_device_dax(int loglevel, struct ndctl_test *test,
 	if (rc)
 		goto out;
 
+	/* upgrade to a writable mapping */
+	close(fd);
+	munmap(buf, VERIFY_SIZE);
+	fd = open(path, O_RDWR);
+	if (fd < 0) {
+		fprintf(stderr, "%s: failed to open(O_RDWR) device-dax instance\n",
+				daxctl_dev_get_devname(dev));
+		rc = -ENXIO;
+		goto out;
+	}
+
+	buf = mmap(NULL, VERIFY_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (buf == MAP_FAILED) {
+		fprintf(stderr, "%s: expected PROT_WRITE + MAP_SHARED success\n",
+				path);
+		return -ENXIO;
+	}
+
 	/*
 	 * Prior to 4.8-final these tests cause crashes, or are
 	 * otherwise not supported.