From: Kees Cook <keescook@chromium.org>
To: David Gow <davidgow@google.com>
Cc: Brendan Higgins <brendanhiggins@google.com>,
Daniel Latypov <dlatypov@google.com>,
kunit-dev@googlegroups.com, Petr Skocik <pskocik@gmail.com>,
linux-hardening@vger.kernel.org
Subject: mocking init_task ?
Date: Thu, 1 Dec 2022 20:28:32 -0800 [thread overview]
Message-ID: <202212012008.D6F6109@keescook> (raw)
Hi,
I want to make a unit test for kill_something_info(), as there is a patch
to fix a bug with it not working as expected under a specific process
tree arrangement[1]. This seems like a great candidate for a unit test:
given a specific state, return a specific result. Emboldened, I applied
the "kunit: Support redirecting function calls" series[2], preparing to
mock group_send_sig_info(), and ran head-long into for_each_process()
... which uses the address of the global init_task:
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
:(
I'm curious what you think might be the right approach to mock
init_task, or for_each_process(), so I can apply unit tests to some of
the "simple" process tree walkers...
One idea I had was using the "kunit: Provide a static key to check if
KUnit is actively running tests" series[3], and do something like this:
#ifndef CONFIG_KUNIT
#define init_task_ptr &init_task
#else
#define init_task_ptr ({ \
struct task_struct *task = &init_task; \
if (static_branch_unlikely(&kunit_running)) { \
struct kunit *test; \
test = current->kunit_test; \
if (test->mock_init_task) \
task = test->mock_init_task; \
} \
task; \
})
#endif
#define for_each_process(p) \
for (p = init_task_ptr ; (p = next_task(p)) != init_task_ptr ; )
And then tests can hang a mock init_task off the test? It seems really
horrible, but there is a LOT of global state in the kernel, so I figured
I had to start somewhere? :P
Thoughts?
-Kees
[1] https://lore.kernel.org/lkml/20221122161240.137570-1-pskocik@gmail.com/
[2] https://lore.kernel.org/lkml/20220910212804.670622-1-davidgow@google.com/
[3] https://lore.kernel.org/lkml/20221125084306.1063074-1-davidgow@google.com/
--
Kees Cook
next reply other threads:[~2022-12-02 4:28 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-02 4:28 Kees Cook [this message]
2022-12-02 6:52 ` mocking init_task ? Daniel Latypov
2022-12-03 10:29 ` David Gow
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=202212012008.D6F6109@keescook \
--to=keescook@chromium.org \
--cc=brendanhiggins@google.com \
--cc=davidgow@google.com \
--cc=dlatypov@google.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-hardening@vger.kernel.org \
--cc=pskocik@gmail.com \
/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.