Re: [PATCH] fanotify: move path permission and security check

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Mar 1, 2024 at 3:16 PM Mete Durlu <meted@xxxxxxxxxxxxx> wrote:
>
> On 3/1/24 10:52, Amir Goldstein wrote:
> > On Thu, Feb 29, 2024 at 7:53 PM Mete Durlu <meted@xxxxxxxxxxxxx> wrote:
> >>
> >> In current state do_fanotify_mark() does path permission and security
> >> checking before doing the event configuration checks. In the case
> >> where user configures mount and sb marks with kernel internal pseudo
> >> fs, security_path_notify() yields an EACESS and causes an earlier
> >> exit. Instead, this particular case should have been handled by
> >> fanotify_events_supported() and exited with an EINVAL.
> >
> > What makes you say that this is the expected outcome?
> > I'd say that the expected outcome is undefined and we have no reason
> > to commit to either  EACCESS or EINVAL outcome.
>
> TLDR; I saw the failing ltp test(fanotify14) started investigating, read
> the comments on the related commits and noticed that the fanotify
> documentation does not mention any EACESS as an errno. For these reasons
> I made an attempt to provide a fix. The placement of the checks aim
> minimal change, I just tried not to alter the logic more than needed.
> Thanks for the feedback, will apply suggestions.
>

Generally speaking, the reasons above themselves are good enough
reasons for fixing the documentation and fixing the test code, but not
enough reasons to change the code.

There may be other good reasons for changing the code, but I am not
sure they apply here.

>
> The main reason is the following commit;
> * linux: 69562eb0bd3e ("fanotify: disallow mount/sb marks on kernel
> internal pseudo fs")
>
> fanotify_user: fanotify_events_supported()
>      /*
>       * mount and sb marks are not allowed on kernel internal pseudo
>           * fs, like pipe_mnt, because that would subscribe to events on
>           * all the anonynous pipes in the system.
>       */
>      if (mark_type != FAN_MARK_INODE &&
>          path->mnt->mnt_sb->s_flags & SB_NOUSER)
>          return -EINVAL;
>
> It looks to me as, when configuring fanotify_mark with pipes and
> FAN_MARK_MOUNT or FAN_MARK_FILESYSTEM, the path above should be taken
> instead of an early return with EACCES.
>

It is a subjective opinion. I do not agree with it, but it does not matter if
I agree with this statement or not, what matters it that there is no clear
definition across system calls of what SHOULD happen in this case
and IMO there is no reason for us to commit to this behavior or the other.

> Also the following commit on linux test project(ltp) expects EINVAL as
> expected errno.
>
> * ltp: 8e897008c ("fanotify14: Test disallow sb/mount mark on anonymous
> pipe")
>
> To be honest, the test added on above commit is the main reason why I
> started investigating this.
>

This is something that I don't understand.
If you are running LTP in a setup that rejects fanotify_mark() due to
security policy, how do the rest of the fanotify tests pass?
I feel like I am missing information about the test regression report.
I never test with a security policy applied so I have no idea what
might be expected.

> > I don't really mind the change of outcome, but to me it seems
> > nicer that those tests are inside fanotify_find_path(), so I will
> > want to get a good reason for moving them out.
>
> I agree, when those tests are inside fanotify_find_path() it looks much
> cleaner but then the check for psuedo fs in fanotify_events_supported()
> is not made. And I believe when configuring fanotify an EINVAL makes
> more sense than EACCES, it just seems more informative(at least to me).
> Would it maybe make sense to put them in a separate helper function,
> sth like:
>
> static int fanotify_path_security(struct path *path,
>                                   __u64 mask,
>                                   unsigned int obj_type) {
>         int ret;
>
>         ret = path_permission(path, MAY_READ);
>         if (ret)
>                 return ret;
>         ret = security_path_notify(path, mask, obj_type);
>         return ret;
> }
>

*if* we agree that a change to code is needed, then this helper
would be very nice.

> ...
>
> ret = fanotify_path_security(...)
> if (ret)
>         goto path_put_and_out;
>
> >>
> >> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> >> index fbdc63cc10d9..14121ad0e10d 100644
> >> --- a/fs/notify/fanotify/fanotify_user.c
> >> +++ b/fs/notify/fanotify/fanotify_user.c
> >> @@ -1015,7 +1015,7 @@ static int fanotify_find_path(int dfd, const char __user *filename,
> >>                          fdput(f);
> >>                          goto out;
> >>                  }
> >> -
> >> +               ret = 0;
> >
> > Better convert all gotos in this helper to return.
> > There is nothing in the out label.
> >
> Good point, will do!
>
> >>                  *path = f.file->f_path;
> >>                  path_get(path);
> >>                  fdput(f);
> >> @@ -1028,21 +1028,7 @@ static int fanotify_find_path(int dfd, const char __user *filename,
> >>                          lookup_flags |= LOOKUP_DIRECTORY;
> >>
> >>                  ret = user_path_at(dfd, filename, lookup_flags, path);
> >> -               if (ret)
> >> -                       goto out;
> >>          }
> >> -
> >> -       /* you can only watch an inode if you have read permissions on it */
> >> -       ret = path_permission(path, MAY_READ);
> >> -       if (ret) {
> >> -               path_put(path);
> >> -               goto out;
> >> -       }
> >> -
> >> -       ret = security_path_notify(path, mask, obj_type);
> >> -       if (ret)
> >> -               path_put(path);
> >> -
> >>   out:
> >>          return ret;
> >>   }
> >> @@ -1894,6 +1880,14 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
> >>                  if (ret)
> >>                          goto path_put_and_out;
> >>          }
> >> +       /* you can only watch an inode if you have read permissions on it */
> >> +       ret = path_permission(&path, MAY_READ);
> >> +       if (ret)
> >> +               goto path_put_and_out;
> >> +
> >> +       ret = security_path_notify(&path, mask, obj_type);
> >> +       if (ret)
> >> +               goto path_put_and_out;
> >>
> >>          if (fid_mode) {
> >>                  ret = fanotify_test_fsid(path.dentry, flags, &__fsid);
> >
> > If we do accept your argument that security_path_notify() should be
> > after fanotify_events_supported(). Why not also after fanotify_test_fsid()
> > and fanotify_test_fid()?
>
> I tried to place the checks as close as possible to their original
> position, that is why I placed them right after
> fanotify_events_supported(). I wanted to keep the ordering as close as
> possible to original to not break any other check. I am open to
> suggestions regarding this.
>

It is a matter of principle IMO.
If you argue that access permission errors have priority over validity
of API arguments, then  fanotify_test_{fsid,fid}() are not that much
different (priority-wise) from fanotify_events_supported().

My preference is to not change the code, but maybe Jan will
have a different opinion.

Thanks,
Amir.





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux