linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: linux-api@vger.kernel.org
Subject: discovering native audit arch
Date: Mon, 14 Feb 2022 13:40:49 -0800	[thread overview]
Message-ID: <202202141339.94EF9CAF@keescook> (raw)

Hi,

Is there a better way than this to find the native architecture value
needed for seccomp filters? Right now everyone basically hard-codes it
with other compile-time checks...

static unsigned int get_syscall_arch(void)
{
	struct ptrace_syscall_info info = { };
	siginfo_t siginfo = { };
	unsigned int arch = -1;
	pid_t pid = fork();

	if (pid < 0)
		return -1;
	if (pid == 0) {
		if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0) {
			perror("PTRACE_TRACEME");
			_exit(1);
		}
		if (raise(SIGSTOP) != 0) {
			perror("raise");
			_exit(1);
		}
		_exit(0);
	}
	if (ptrace(PTRACE_ATTACH, pid, 0, 0) != 0)
		goto reap;
	if (waitid(P_PID, pid, &siginfo, WEXITED | WSTOPPED | WCONTINUED) != 0)
		goto reap;
	if (siginfo.si_code != CLD_STOPPED &&
	    siginfo.si_code != CLD_TRAPPED)
		goto reap;
	if (ptrace(PTRACE_GET_SYSCALL_INFO, pid, sizeof(info), &info)
	    < offsetof(typeof(info), arch) + sizeof(info.arch))
		goto reap;
	arch = info.arch;
	ptrace(PTRACE_DETACH, pid, 0, 0);
reap:
	kill(pid, SIGKILL);
	if (waitpid(pid, NULL, 0) != pid)
		perror("waitpid");
	return arch;
}


-- 
Kees Cook

                 reply	other threads:[~2022-02-14 21:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=202202141339.94EF9CAF@keescook \
    --to=keescook@chromium.org \
    --cc=linux-api@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).