# Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- List: linux-security-module
- 6 messages, 4 authors
- Page: https://ratatoskr.run/linux-security-module/2021/06/1414071/t

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: Kees Cook \<hidden\>
- Date: 2021-06-14 22:51:10 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1414071.md

```text
On Tue, Jun 15, 2021 at 02:46:19AM +0800, youling 257 wrote:
> I test this patch cause "init: cannot setexeccon(u:r:ueventd:s0)
> operation not permitted.
> init ctrl_write_limited.

Thanks for testing!

This appears to come from here:
https://github.com/aosp-mirror/platform_system_core/blob/master/init/service.cpp#L242


In setexeccon(), I see (pid=0, attr="exec"):

        fd = openattr(pid, attr, O_RDWR | O_CLOEXEC);
...
                        ret = write(fd, context2, strlen(context2) + 1);
...
        close(fd);


and openattr() is doing:
...
                rc = asprintf(&path, "/proc/thread-self/attr/%s", attr);
                if (rc < 0)
                        return -1;
                fd = open(path, flags | O_CLOEXEC);
...

I'm not sure how the above could fail. (mm_access() always allows
introspection...)

The only way I can understand the check failing is if a process did:

open, exec, write

But setexeccon() is not doing anything between the open and the write...

I will keep looking...

-Kees

-- 
Kees Cook
```

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: youling 257 \<hidden\>
- Date: 2021-06-15 02:24:48 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1414147.md

```text
if try to find problem on userspace, i used linux 5.13rc6 on old
android 7 cm14.1, not aosp android 11.
http://git.osdn.net/view?p=android-x86/system-core.git;a=blob;f=init/service.cpp;h=a5334f447fc2fc34453d2f6a37523bedccadc690;hb=refs/heads/cm-14.1-x86#l457

 457         if (!seclabel_.empty()) {
 458             if (setexeccon(seclabel_.c_str()) < 0) {
 459                 ERROR("cannot setexeccon('%s'): %s\n",
 460                       seclabel_.c_str(), strerror(errno));
 461                 _exit(127);
 462             }
 463         }

2021-06-15 6:50 GMT+08:00, Kees Cook [ref]:
> On Tue, Jun 15, 2021 at 02:46:19AM +0800, youling 257 wrote:
>> I test this patch cause "init: cannot setexeccon(u:r:ueventd:s0)
>> operation not permitted.
>> init ctrl_write_limited.
>
> Thanks for testing!
>
> This appears to come from here:
> https://github.com/aosp-mirror/platform_system_core/blob/master/init/service.cpp#L242
>
>
> In setexeccon(), I see (pid=0, attr="exec"):
>
>         fd = openattr(pid, attr, O_RDWR | O_CLOEXEC);
> ...
>                         ret = write(fd, context2, strlen(context2) + 1);
> ...
>         close(fd);
>
>
> and openattr() is doing:
> ...
>                 rc = asprintf(&path, "/proc/thread-self/attr/%s", attr);
>                 if (rc < 0)
>                         return -1;
>                 fd = open(path, flags | O_CLOEXEC);
> ...
>
> I'm not sure how the above could fail. (mm_access() always allows
> introspection...)
>
> The only way I can understand the check failing is if a process did:
>
> open, exec, write
>
> But setexeccon() is not doing anything between the open and the write...
>
> I will keep looking...
>
> -Kees
>
> --
> Kees Cook
>
```

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: Linus Torvalds \<torvalds@linux-foundation.org\>
- Date: 2021-06-15 18:19:27 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1415388.md

```text
On Mon, Jun 14, 2021 at 6:55 PM youling 257 [ref] wrote:
>
> if try to find problem on userspace, i used linux 5.13rc6 on old
> android 7 cm14.1, not aosp android 11.
> http://git.osdn.net/view?p=android-x86/system-core.git;a=blob;f=init/service.cpp;h=a5334f447fc2fc34453d2f6a37523bedccadc690;hb=refs/heads/cm-14.1-x86#l457
>
>  457         if (!seclabel_.empty()) {
>  458             if (setexeccon(seclabel_.c_str()) < 0) {
>  459                 ERROR("cannot setexeccon('%s'): %s\n",
>  460                       seclabel_.c_str(), strerror(errno));
>  461                 _exit(127);
>  462             }
>  463         }

I have no idea where the cm14.1 libraries are. Does anybody know where
the matching source code for setexeccon() would be?

For me - obviously not on cm14.1 - all "setexeccon()" does is

   n = openat(AT_FDCWD, "/proc/thread-self/attr/exec", O_RDWR|O_CLOEXEC)
   write(n, string, len)
   close(n)

and if that fails, it would seem to indicate that proc_mem_open()
failed. Which would be mm_access() failing. But I don't see how that
can be the case, because mm_access() explicitly allows "mm ==
current->mm" (which the above clearly should be).

youling, can you double-check with the current -git tree? But as far
as I can tell, my minimal patch is exactly the same as Kees' patch
(just smaller and simpler).

Kees, do you see anything?

           Linus
```

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: Kees Cook \<hidden\>
- Date: 2021-06-15 21:50:45 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1415539.md

```text
On Tue, Jun 15, 2021 at 11:19:04AM -0700, Linus Torvalds wrote:
> On Mon, Jun 14, 2021 at 6:55 PM youling 257 [ref] wrote:
> >
> > if try to find problem on userspace, i used linux 5.13rc6 on old
> > android 7 cm14.1, not aosp android 11.
> > http://git.osdn.net/view?p=android-x86/system-core.git;a=blob;f=init/service.cpp;h=a5334f447fc2fc34453d2f6a37523bedccadc690;hb=refs/heads/cm-14.1-x86#l457
> >
> >  457         if (!seclabel_.empty()) {
> >  458             if (setexeccon(seclabel_.c_str()) < 0) {
> >  459                 ERROR("cannot setexeccon('%s'): %s\n",
> >  460                       seclabel_.c_str(), strerror(errno));
> >  461                 _exit(127);
> >  462             }
> >  463         }
> 
> I have no idea where the cm14.1 libraries are. Does anybody know where
> the matching source code for setexeccon() would be?
> 
> For me - obviously not on cm14.1 - all "setexeccon()" does is
> 
>    n = openat(AT_FDCWD, "/proc/thread-self/attr/exec", O_RDWR|O_CLOEXEC)
>    write(n, string, len)
>    close(n)
> 
> and if that fails, it would seem to indicate that proc_mem_open()
> failed. Which would be mm_access() failing. But I don't see how that
> can be the case, because mm_access() explicitly allows "mm ==
> current->mm" (which the above clearly should be).

Yeah, that was what I saw too.

> youling, can you double-check with the current -git tree? But as far
> as I can tell, my minimal patch is exactly the same as Kees' patch
> (just smaller and simpler).

FWIW, for that patch:

Acked-by: Kees Cook <redacted>
Cc: <redacted>

> 
> Kees, do you see anything?

No, I haven't been able to reproduce the failure. :(

-- 
Kees Cook
```

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: youling 257 \<hidden\>
- Date: 2021-06-16 05:15:31 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1415862.md

```text
I test "proc: only require mm_struct for writing" fixed my cm14.1 problem.

2021-06-16 2:19 GMT+08:00, Linus Torvalds [ref]:
> On Mon, Jun 14, 2021 at 6:55 PM youling 257 [ref] wrote:
>>
>> if try to find problem on userspace, i used linux 5.13rc6 on old
>> android 7 cm14.1, not aosp android 11.
>> http://git.osdn.net/view?p=android-x86/system-core.git;a=blob;f=init/service.cpp;h=a5334f447fc2fc34453d2f6a37523bedccadc690;hb=refs/heads/cm-14.1-x86#l457
>>
>>  457         if (!seclabel_.empty()) {
>>  458             if (setexeccon(seclabel_.c_str()) < 0) {
>>  459                 ERROR("cannot setexeccon('%s'): %s\n",
>>  460                       seclabel_.c_str(), strerror(errno));
>>  461                 _exit(127);
>>  462             }
>>  463         }
>
> I have no idea where the cm14.1 libraries are. Does anybody know where
> the matching source code for setexeccon() would be?
>
> For me - obviously not on cm14.1 - all "setexeccon()" does is
>
>    n = openat(AT_FDCWD, "/proc/thread-self/attr/exec", O_RDWR|O_CLOEXEC)
>    write(n, string, len)
>    close(n)
>
> and if that fails, it would seem to indicate that proc_mem_open()
> failed. Which would be mm_access() failing. But I don't see how that
> can be the case, because mm_access() explicitly allows "mm ==
> current->mm" (which the above clearly should be).
>
> youling, can you double-check with the current -git tree? But as far
> as I can tell, my minimal patch is exactly the same as Kees' patch
> (just smaller and simpler).
>
> Kees, do you see anything?
>
>            Linus
>
```

## Re: \[PATCH\] proc: Track /proc/$pid/attr/ opener mm\_struct

- From: Greg KH \<gregkh@linuxfoundation.org\>
- Date: 2021-06-16 05:56:14 
- Message: https://ratatoskr.run/linux-security-module/2021/06/1415876.md

```text
On Tue, Jun 15, 2021 at 02:50:39PM -0700, Kees Cook wrote:
> On Tue, Jun 15, 2021 at 11:19:04AM -0700, Linus Torvalds wrote:
> > On Mon, Jun 14, 2021 at 6:55 PM youling 257 [ref] wrote:
> > >
> > > if try to find problem on userspace, i used linux 5.13rc6 on old
> > > android 7 cm14.1, not aosp android 11.
> > > http://git.osdn.net/view?p=android-x86/system-core.git;a=blob;f=init/service.cpp;h=a5334f447fc2fc34453d2f6a37523bedccadc690;hb=refs/heads/cm-14.1-x86#l457
> > >
> > >  457         if (!seclabel_.empty()) {
> > >  458             if (setexeccon(seclabel_.c_str()) < 0) {
> > >  459                 ERROR("cannot setexeccon('%s'): %s\n",
> > >  460                       seclabel_.c_str(), strerror(errno));
> > >  461                 _exit(127);
> > >  462             }
> > >  463         }
> > 
> > I have no idea where the cm14.1 libraries are. Does anybody know where
> > the matching source code for setexeccon() would be?
> > 
> > For me - obviously not on cm14.1 - all "setexeccon()" does is
> > 
> >    n = openat(AT_FDCWD, "/proc/thread-self/attr/exec", O_RDWR|O_CLOEXEC)
> >    write(n, string, len)
> >    close(n)
> > 
> > and if that fails, it would seem to indicate that proc_mem_open()
> > failed. Which would be mm_access() failing. But I don't see how that
> > can be the case, because mm_access() explicitly allows "mm ==
> > current->mm" (which the above clearly should be).
> 
> Yeah, that was what I saw too.
> 
> > youling, can you double-check with the current -git tree? But as far
> > as I can tell, my minimal patch is exactly the same as Kees' patch
> > (just smaller and simpler).
> 
> FWIW, for that patch:
> 
> Acked-by: Kees Cook <redacted>
> Cc: <redacted>

Thanks, I'll go pick it up now.

greg k-h
```
