linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>
To: "Mickaël Salaün" <mic@digikod.net>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Aleksa Sarai" <cyphar@cyphar.com>,
	"Andy Lutomirski" <luto@kernel.org>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Casey Schaufler" <casey@schaufler-ca.com>,
	"Christian Brauner" <christian.brauner@ubuntu.com>,
	"Christian Heimes" <christian@python.org>,
	"Deven Bowers" <deven.desai@linux.microsoft.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Eric Chiang" <ericchiang@google.com>,
	"Florian Weimer" <fweimer@redhat.com>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	"James Morris" <jmorris@namei.org>, "Jan Kara" <jack@suse.cz>,
	"Jann Horn" <jannh@google.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Kees Cook" <keescook@chromium.org>,
	"Lakshmi Ramasubramanian" <nramas@linux.microsoft.com>,
	"Madhavan T . Venkataraman" <madvenka@linux.microsoft.com>,
	"Matthew Garrett" <mjg59@google.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Miklos Szeredi" <mszeredi@redhat.com>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Philippe Trébuchet" <philippe.trebuchet@ssi.gouv.fr>,
	"Scott Shell" <scottsh@microsoft.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Steve Dower" <steve.dower@python.org>,
	"Steve Grubb" <sgrubb@redhat.com>,
	"Thibaut Sautereau" <thibaut.sautereau@ssi.gouv.fr>,
	"Vincent Strubel" <vincent.strubel@ssi.gouv.fr>,
	"Yin Fengwei" <fengwei.yin@intel.com>,
	kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	"Mickaël Salaün" <mic@linux.microsoft.com>
Subject: Re: [PATCH v16 1/3] fs: Add trusted_for(2) syscall implementation and related sysctl
Date: Sat, 13 Nov 2021 20:56:09 +0100	[thread overview]
Message-ID: <34779736-e875-c3e0-75d5-0f0a55d729aa@gmail.com> (raw)
In-Reply-To: <5312f022-96ea-5555-8d17-4e60a33cf8f8@digikod.net>

Hi Mickaël,

On 11/13/21 14:02, Mickaël Salaün wrote:
>> TL;DR:
>>
>> ISO C specifies that for the following code:
>>
>>      enum foo {BAR};
>>
>>      enum foo foobar;
>>
>> typeof(foo)    shall be int
>> typeof(foobar) is implementation-defined
> 
> I tested with some version of GCC (from 4.9 to 11) and clang (10 and 11)
> with different optimizations and the related sizes are at least the same
> as for the int type.

GCC has -fshort-enums to make enum types be as short as possible.  I 
expected -Os to turn this on, since it saves space, but it doesn't.

Still, not relying on enum == int is better, IMO.

> 
>>
>> Since foobar = BAR; assigns an int, the best thing to do to avoid
>> implementation-defined behavior, is to declare foobar as int too.
> 
> OK, so it should be enough to change the syscall argument type from enum
> trusted_for_usage to int, but we can keep the UAPI with the enum (i.e.
> we don't need to change the value to #define TRUSTED_FOR_EXECUTION 1) right?

Correct.  The enumerations are guaranteed to be int (except in case of 
UB, see below), so they'll be (almost) the same as a #define after the 
preprocessor.


If you do

enum foo {
	FOO = 1L << INT_WIDTH
};

since that doesn't fit in either int or unsigned int,
it is Undefined Behavior,
and here GCC decides to use long for FOO.

+++++++++ UB example ++++++++++++++

$ cat foo.c
	#include <limits.h>
	#include <stdio.h>


	enum foo {
		FOO = 1L << UINT_WIDTH
	};

	int main(void)
	{
		printf("\tsizeof(enum foo) = %zu\n", sizeof(enum foo));
		printf("\tsizeof(FOO)      = %zu\n", sizeof(FOO));
	}

$ cc foo.c -Wall -Wextra -Werror -Wpedantic -pedantic-errors -std=c2x
foo.c:6:23: error: ISO C restricts enumerator values to range of 'int' 
[-Wpedantic]
     6 |                 FOO = 1L << UINT_WIDTH
       |                       ^~
$ cc foo.c -Wall -Wextra -Werror -std=c2x
$ ./a.out
	sizeof(enum foo) = 8
	sizeof(FOO)      = 8

+++++++++++++ -fshort-enums example +++++++++++++++

$ cat foo.c
	#include <stdio.h>


	enum foo {
		FOO = 1
	};

	int main(void)
	{
		printf("\tsizeof(enum foo) = %zu\n", sizeof(enum foo));
		printf("\tsizeof(FOO)      = %zu\n", sizeof(FOO));
	}

$ cc foo.c -Wall -Wextra -Werror -Wpedantic -pedantic-errors -fshort-enums
$ ./a.out
	sizeof(enum foo) = 1
	sizeof(FOO)      = 4

++++++++++++++++++++++++++++++++++++++++++++++++++++++

Cheers,
Alex


> 
>>
>>
>>> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
>>> index 528a478dbda8..c535e0e43cc8 100644
>>> --- a/include/linux/syscalls.h
>>> +++ b/include/linux/syscalls.h
>>> @@ -462,6 +463,7 @@ asmlinkage long sys_fallocate(int fd, int mode,
>>> loff_t offset, loff_t len);
>>>    asmlinkage long sys_faccessat(int dfd, const char __user *filename,
>>> int mode);
>>>    asmlinkage long sys_faccessat2(int dfd, const char __user *filename,
>>> int mode,
>>>                       int flags);
>>> +asmlinkage long sys_trusted_for(int fd, enum trusted_for_usage usage,
>>> u32 flags);
>>
>> Same here.
>>
>>>    asmlinkage long sys_chdir(const char __user *filename);
>>>    asmlinkage long sys_fchdir(unsigned int fd);
>>>    asmlinkage long sys_chroot(const char __user *filename);
>>
>> Thanks,
>> Alex
>>
>>

-- 
Alejandro Colomar
Linux man-pages comaintainer; http://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

  reply	other threads:[~2021-11-13 19:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-10 19:06 [PATCH v16 0/3] Add trusted_for(2) (was O_MAYEXEC) Mickaël Salaün
2021-11-10 19:06 ` [PATCH v16 1/3] fs: Add trusted_for(2) syscall implementation and related sysctl Mickaël Salaün
2021-11-12 19:16   ` Alejandro Colomar (man-pages)
2021-11-13 13:02     ` Mickaël Salaün
2021-11-13 19:56       ` Alejandro Colomar (man-pages) [this message]
2021-11-14 12:09         ` Mickaël Salaün
2021-11-14 15:32         ` Geert Uytterhoeven
2021-11-14 15:45           ` Alejandro Colomar (man-pages)
2021-11-10 19:06 ` [PATCH v16 2/3] arch: Wire up trusted_for(2) Mickaël Salaün
2021-11-10 19:06 ` [PATCH v16 3/3] selftest/interpreter: Add tests for trusted_for(2) policies Mickaël Salaün

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=34779736-e875-c3e0-75d5-0f0a55d729aa@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=casey@schaufler-ca.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=christian@python.org \
    --cc=corbet@lwn.net \
    --cc=cyphar@cyphar.com \
    --cc=deven.desai@linux.microsoft.com \
    --cc=dvyukov@google.com \
    --cc=ebiggers@kernel.org \
    --cc=ericchiang@google.com \
    --cc=fengwei.yin@intel.com \
    --cc=fweimer@redhat.com \
    --cc=geert@linux-m68k.org \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=jmorris@namei.org \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=madvenka@linux.microsoft.com \
    --cc=mic@digikod.net \
    --cc=mic@linux.microsoft.com \
    --cc=mjg59@google.com \
    --cc=mszeredi@redhat.com \
    --cc=nramas@linux.microsoft.com \
    --cc=paul@paul-moore.com \
    --cc=philippe.trebuchet@ssi.gouv.fr \
    --cc=scottsh@microsoft.com \
    --cc=sgrubb@redhat.com \
    --cc=shuah@kernel.org \
    --cc=steve.dower@python.org \
    --cc=thibaut.sautereau@ssi.gouv.fr \
    --cc=vincent.strubel@ssi.gouv.fr \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    --cc=zohar@linux.ibm.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 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).