From: Eric Biggers <ebiggers3@gmail.com>
To: keyrings@vger.kernel.org
Subject: [PATCH] syscalls/keyctl04: new test for thread keyring memory leak
Date: Fri, 28 Jul 2017 21:13:13 +0000 [thread overview]
Message-ID: <20170728211313.89836-1-ebiggers3@gmail.com> (raw)
From: Eric Biggers <ebiggers@google.com>
Add a test for a kernel bug that allowed unprivileged programs to
exhaust kernel memory by leaking thread keyrings (CVE-2017-7472).
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/.gitignore | 1 +
testcases/kernel/syscalls/keyctl/keyctl04.c | 72 +++++++++++++++++++++++++++++
3 files changed, 74 insertions(+)
create mode 100644 testcases/kernel/syscalls/keyctl/keyctl04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 8e1f58731..5c7fd8e94 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -494,6 +494,7 @@ io_submit01 io_submit01
keyctl01 keyctl01
keyctl02 keyctl02
keyctl03 keyctl03
+keyctl04 keyctl04
kcmp01 kcmp01
kcmp02 kcmp02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 6e0af314c..e311ba3f8 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -457,6 +457,7 @@
/keyctl/keyctl01
/keyctl/keyctl02
/keyctl/keyctl03
+/keyctl/keyctl04
/kcmp/kcmp01
/kcmp/kcmp02
/kcmp/kcmp03
diff --git a/testcases/kernel/syscalls/keyctl/keyctl04.c b/testcases/kernel/syscalls/keyctl/keyctl04.c
new file mode 100644
index 000000000..c4a493b45
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl04.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2017 Google, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program, if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Regression test for commit c9f838d104fe ("KEYS: fix
+ * keyctl_set_reqkey_keyring() to not leak thread keyrings"), a.k.a.
+ * CVE-2017-7472. This bug could be used to exhaust kernel memory, though it
+ * would take a while to do that and it would grind the test suite to a halt.
+ * Instead we do a quick check for whether the existing thread keyring is
+ * replaced when the default request-key destination is set to the thread
+ * keyring. It shouldn't be, but before the fix it was (and the old thread
+ * keyring was leaked).
+ */
+
+#include "config.h"
+#ifdef HAVE_LINUX_KEYCTL_H
+# include <linux/keyctl.h>
+#endif
+#include "tst_test.h"
+#include "linux_syscall_numbers.h"
+
+#ifdef HAVE_LINUX_KEYCTL_H
+
+static void do_test(void)
+{
+ int tid_keyring;
+
+ /* Create a thread keyring and remember its ID */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
+ KEY_SPEC_THREAD_KEYRING, 1));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to create thread keyring");
+ tid_keyring = TEST_RETURN;
+
+ /* Set the default request-key destination to the thread keyring */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_SET_REQKEY_KEYRING,
+ KEY_REQKEY_DEFL_THREAD_KEYRING));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to set reqkey keyring");
+
+ /* Get the thread keyring ID again; it shouldn't have changed */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
+ KEY_SPEC_THREAD_KEYRING, 0));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to get thread keyring ID");
+ if (TEST_RETURN != tid_keyring)
+ tst_brk(TFAIL, "thread keyring was leaked!");
+
+ tst_res(TPASS, "thread keyring was not leaked");
+}
+
+static struct tst_test test = {
+ .test_all = do_test,
+};
+
+#else
+ TST_TEST_TCONF("linux/keyctl.h was missing upon compilation.");
+#endif /* HAVE_LINUX_KEYCTL_H */
--
2.14.0.rc0.400.g1c36432dff-goog
WARNING: multiple messages have this Message-ID (diff)
From: Eric Biggers <ebiggers3@gmail.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] syscalls/keyctl04: new test for thread keyring memory leak
Date: Fri, 28 Jul 2017 14:13:13 -0700 [thread overview]
Message-ID: <20170728211313.89836-1-ebiggers3@gmail.com> (raw)
From: Eric Biggers <ebiggers@google.com>
Add a test for a kernel bug that allowed unprivileged programs to
exhaust kernel memory by leaking thread keyrings (CVE-2017-7472).
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/.gitignore | 1 +
testcases/kernel/syscalls/keyctl/keyctl04.c | 72 +++++++++++++++++++++++++++++
3 files changed, 74 insertions(+)
create mode 100644 testcases/kernel/syscalls/keyctl/keyctl04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 8e1f58731..5c7fd8e94 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -494,6 +494,7 @@ io_submit01 io_submit01
keyctl01 keyctl01
keyctl02 keyctl02
keyctl03 keyctl03
+keyctl04 keyctl04
kcmp01 kcmp01
kcmp02 kcmp02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 6e0af314c..e311ba3f8 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -457,6 +457,7 @@
/keyctl/keyctl01
/keyctl/keyctl02
/keyctl/keyctl03
+/keyctl/keyctl04
/kcmp/kcmp01
/kcmp/kcmp02
/kcmp/kcmp03
diff --git a/testcases/kernel/syscalls/keyctl/keyctl04.c b/testcases/kernel/syscalls/keyctl/keyctl04.c
new file mode 100644
index 000000000..c4a493b45
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl04.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2017 Google, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program, if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Regression test for commit c9f838d104fe ("KEYS: fix
+ * keyctl_set_reqkey_keyring() to not leak thread keyrings"), a.k.a.
+ * CVE-2017-7472. This bug could be used to exhaust kernel memory, though it
+ * would take a while to do that and it would grind the test suite to a halt.
+ * Instead we do a quick check for whether the existing thread keyring is
+ * replaced when the default request-key destination is set to the thread
+ * keyring. It shouldn't be, but before the fix it was (and the old thread
+ * keyring was leaked).
+ */
+
+#include "config.h"
+#ifdef HAVE_LINUX_KEYCTL_H
+# include <linux/keyctl.h>
+#endif
+#include "tst_test.h"
+#include "linux_syscall_numbers.h"
+
+#ifdef HAVE_LINUX_KEYCTL_H
+
+static void do_test(void)
+{
+ int tid_keyring;
+
+ /* Create a thread keyring and remember its ID */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
+ KEY_SPEC_THREAD_KEYRING, 1));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to create thread keyring");
+ tid_keyring = TEST_RETURN;
+
+ /* Set the default request-key destination to the thread keyring */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_SET_REQKEY_KEYRING,
+ KEY_REQKEY_DEFL_THREAD_KEYRING));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to set reqkey keyring");
+
+ /* Get the thread keyring ID again; it shouldn't have changed */
+ TEST(tst_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
+ KEY_SPEC_THREAD_KEYRING, 0));
+ if (TEST_RETURN < 0)
+ tst_brk(TFAIL | TTERRNO, "failed to get thread keyring ID");
+ if (TEST_RETURN != tid_keyring)
+ tst_brk(TFAIL, "thread keyring was leaked!");
+
+ tst_res(TPASS, "thread keyring was not leaked");
+}
+
+static struct tst_test test = {
+ .test_all = do_test,
+};
+
+#else
+ TST_TEST_TCONF("linux/keyctl.h was missing upon compilation.");
+#endif /* HAVE_LINUX_KEYCTL_H */
--
2.14.0.rc0.400.g1c36432dff-goog
next reply other threads:[~2017-07-28 21:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-28 21:13 Eric Biggers [this message]
2017-07-28 21:13 ` [LTP] [PATCH] syscalls/keyctl04: new test for thread keyring memory leak Eric Biggers
-- strict thread matches above, loose matches on Subject: below --
2017-07-31 7:58 Richard Palethorpe
2017-07-31 7:58 ` Richard Palethorpe
2017-07-31 20:57 ` Eric Biggers
2017-07-31 20:57 ` Eric Biggers
2017-08-01 7:42 ` Richard Palethorpe
2017-08-01 7:42 ` Richard Palethorpe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170728211313.89836-1-ebiggers3@gmail.com \
--to=ebiggers3@gmail.com \
--cc=keyrings@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.