Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 13 Nov 2021 20:56:09 +0100
From: "Alejandro Colomar (man-pages)" <alx.manpages@...il.com>
To: Mickaël Salaün <mic@...ikod.net>,
 Al Viro <viro@...iv.linux.org.uk>, Andrew Morton <akpm@...ux-foundation.org>
Cc: Aleksa Sarai <cyphar@...har.com>, Andy Lutomirski <luto@...nel.org>,
 Arnd Bergmann <arnd@...db.de>, Casey Schaufler <casey@...aufler-ca.com>,
 Christian Brauner <christian.brauner@...ntu.com>,
 Christian Heimes <christian@...hon.org>,
 Deven Bowers <deven.desai@...ux.microsoft.com>,
 Dmitry Vyukov <dvyukov@...gle.com>, Eric Biggers <ebiggers@...nel.org>,
 Eric Chiang <ericchiang@...gle.com>, Florian Weimer <fweimer@...hat.com>,
 Geert Uytterhoeven <geert@...ux-m68k.org>, James Morris <jmorris@...ei.org>,
 Jan Kara <jack@...e.cz>, Jann Horn <jannh@...gle.com>,
 Jonathan Corbet <corbet@....net>, Kees Cook <keescook@...omium.org>,
 Lakshmi Ramasubramanian <nramas@...ux.microsoft.com>,
 "Madhavan T . Venkataraman" <madvenka@...ux.microsoft.com>,
 Matthew Garrett <mjg59@...gle.com>, Matthew Wilcox <willy@...radead.org>,
 Miklos Szeredi <mszeredi@...hat.com>, Mimi Zohar <zohar@...ux.ibm.com>,
 Paul Moore <paul@...l-moore.com>,
 Philippe Trébuchet <philippe.trebuchet@....gouv.fr>,
 Scott Shell <scottsh@...rosoft.com>, Shuah Khan <shuah@...nel.org>,
 Steve Dower <steve.dower@...hon.org>, Steve Grubb <sgrubb@...hat.com>,
 Thibaut Sautereau <thibaut.sautereau@....gouv.fr>,
 Vincent Strubel <vincent.strubel@....gouv.fr>,
 Yin Fengwei <fengwei.yin@...el.com>, kernel-hardening@...ts.openwall.com,
 linux-api@...r.kernel.org, linux-fsdevel@...r.kernel.org,
 linux-integrity@...r.kernel.org, linux-kernel@...r.kernel.org,
 linux-security-module@...r.kernel.org,
 Mickaël Salaün <mic@...ux.microsoft.com>
Subject: Re: [PATCH v16 1/3] fs: Add trusted_for(2) syscall implementation and
 related sysctl

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/

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.