Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 23 Mar 2024 00:56:10 +0000
From:      Shawn Webb <shawn.webb@hardenedbsd.org>
To:        Alan Somers <asomers@freebsd.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Filesystem extended attributes and Capsicum
Message-ID:  <ae4isfrqtyc7l3rwqd2rz6efmuztwqxw5h67xqaqts6ra6br3l@ly347trhgdoq>
In-Reply-To: <CAOtMX2gC3iZ6X6HotpgxgfAfPN-8C7rg3ZCP%2Bpr5e8KddasUsQ@mail.gmail.com>
References:  <bjeg3z2aa5owo7uur75olwuw6le2g3whzxwfgef4ozyy6w6kog@33b426ax6inf> <CAOtMX2gC3iZ6X6HotpgxgfAfPN-8C7rg3ZCP%2Bpr5e8KddasUsQ@mail.gmail.com>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
On Fri, Mar 22, 2024 at 06:20:48PM -0600, Alan Somers wrote:
> On Fri, Mar 22, 2024 at 5:38 PM Shawn Webb <shawn.webb@hardenedbsd.org> wrote:
> >
> > Hey all,
> >
> > I'm writing an application in which I hope to enable Capsicum. I'm
> > experiencing an issue whereby extattr_get_fd fails with a file
> > descriptor that has all the extended attribute capabilities enabled
> > (CAP_EXTATTR_DELETE, CAP_EXTATTR_GET, CAP_EXTATTR_LIST, and
> > CAP_EXTATTR_SET).
> >
> > Looking at the kernel source (sys/kern/vfs_extattr.c) tells me that
> > kern_extattr_get_fd only requires CAP_EXTATTR_GET.
> >
> > So I'm a bit puzzled as to why my call to extattr_get_fd(2) is
> > failing. Am I doing something wrong or are filesystem extended
> > attributes not supported in a Capabilities-enabled process?
> >
> > Here's how I'm creating the file descriptor (before calling
> > cap_enter(2)):
> >
> > ==== BEGIN CODE ====
> > static int
> > open_file(const char *path)
> > {
> >         cap_rights_t rights;
> >         int fd;
> >
> >         fd = open(path, O_PATH | O_CLOEXEC);
> >         if (fd == -1) {
> >                 return (-1);
> >         }
> >
> >         memset(&rights, 0, sizeof(rights));
> >         cap_rights_init(&rights, CAP_EXTATTR_DELETE, CAP_EXTATTR_GET,
> >             CAP_EXTATTR_LIST, CAP_EXTATTR_SET);
> >         cap_rights_limit(fd, &rights);
> >
> >         return (fd);
> > }
> > ==== END CODE ====
> >
> > Eventually, after calling cap_enter(2), the following code is called:
> >
> > ==== BEGIN CODE ====
> > #define ATTRNAME_ENABLED "hbsd.pax.aslr"
> >         sz = extattr_get_fd(fd, ctx->hc_namespace, ATTRNAME_ENABLED, NULL, 0);
> >         if (sz <= 0) {
> >                 if (errno == ENOATTR) {
> >                         /*
> >                         * This is okay, it just means that nothing has been set.
> >                         * No error condition here.
> >                         */
> >                         return (RES_SUCCESS);
> >                 }
> >                 return (RES_FAIL);
> >         }
> > ==== END CODE ====
> >
> > For reference, the program's code is here:
> > https://git.hardenedbsd.org/shawn.webb/hbsdctrl/-/tree/main?ref_type=heads
> >
> > The library code, which is what's responsible for calling the
> > filesystem extended attribute related syscalls is here:
> >
> > https://git.hardenedbsd.org/hardenedbsd/HardenedBSD/-/tree/hardened/current/hbsdcontrol-v2/lib/libhbsdcontrol?ref_type=heads
> >
> > From the rights(4) manual page, I'm instructed all I need are to apply
> > those capabilities to that file descriptor:
> >
> > ==== BEGIN PASTE ====
> >      CAP_EXTATTR_DELETE   Permit extattr_delete_fd(2).
> >
> >      CAP_EXTATTR_GET      Permit extattr_get_fd(2).
> >
> >      CAP_EXTATTR_LIST     Permit extattr_list_fd(2).
> >
> >      CAP_EXTATTR_SET      Permit extattr_set_fd(2).
> > ==== END PASTE ====
> >
> > So I'm a bit unsure if I'm doing something wrong.
> >
> > Thanks,
> >
> > --
> > Shawn Webb
> > Cofounder / Security Engineer
> > HardenedBSD
> >
> > Tor-ified Signal: +1 303-901-1600 / shawn_webb_opsec.50
> > https://git.hardenedbsd.org/hardenedbsd/pubkeys/-/raw/master/Shawn_Webb/03A4CBEBB82EA5A67D9F3853FF2E67A277F8E1FA.pub.asc
> 
> What error code does it fail with?  If it's ENOTCAPABLE, then I
> suggest using dtrace to find the reason why it fails.  Do something
> like this:
> 
> dtrace -i 'fbt:kernel::return /arg1 == 93 && pid == $target/
> {trace(".");}' -c ./my_application
> 
> That will print the name of every non-inlined kernel function that
> returns ENOTCAPABLE during your process.  But it will also print the
> names of any other kernel functions that return an integer value of
> 93.  From there, guess which function is the real source of the error.
> Then you can do

DTrace is unavailable on this particular system.

It does indeed fail with ENOTCAPABLE. I have the kern.trap_enotcap sysctl
set to 1 so that I can know at exactly what point we're failing, and
it's indeed at extattr_get_fd.

Thanks,

-- 
Shawn Webb
Cofounder / Security Engineer
HardenedBSD

Tor-ified Signal: +1 303-901-1600 / shawn_webb_opsec.50
https://git.hardenedbsd.org/hardenedbsd/pubkeys/-/raw/master/Shawn_Webb/03A4CBEBB82EA5A67D9F3853FF2E67A277F8E1FA.pub.asc

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEA6TL67gupaZ9nzhT/y5nonf44foFAmX+KKEACgkQ/y5nonf4
4fq64Q/6AkaAYz9Vo1Ld32z0VlRPoBeLuIY6KFF/5wc0tWbGSD6k8Q15u1RIuplp
MM/gTX6QC9fpD7FR9ywasGBGBUtzGDnOtOvyjtJkmSFR9fc6umhn7kpxPLaxzHlF
65bFRD/+b37ePnoB/LOcOduPN9a9XFMhJiIte/UWJPFlYCDSTTkD9wkZsraxAFGS
NGj0LfmgpD2DLVXckqoXRxuyZ0xqOpJBxUzfT+a+A5s0/FLLzJ1odcRouqm8jYI2
HjR9Jdnr8MPM2VAIrY8ug2Bs4mYmrdhjfPSXpd/+MiP3qwa8gNzKZUMvwSgZsNyg
otmtR5EkJRA9UJ0kwWBe75MWMCOBKlE8liZ6sfwyaMp+rK1A6iVpp0sDqWzgifHG
P+k9aBTtZwAgZIm/uiZkTmwrJ5vbnhd6OdFZyYYut9+F3cOTPHukqF+sGSpPz6/6
fzJHGpQFtTKE6WCxQLnPyzmsycB7zY2NxmUASNzR1N4v5WoZV5n6vgPxho0F6DMb
4Z82m7gu+pe4/E9XIGV9rXEd02aY84tBuKAsw7Z4QM+DsNmPuiyN82xIi0muG0Sv
zn+3bA8/b8AU9KJeV+qDw2U/DXVk0fEDQNOgiURc0MEpqvRAakNkkFM28OrrHsaC
HYsTcsMuoF6JRmeDEKrJsaBV2NgBuUjsuACkoQNCfU/xpYA4wnM=
=0WA5
-----END PGP SIGNATURE-----
help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?ae4isfrqtyc7l3rwqd2rz6efmuztwqxw5h67xqaqts6ra6br3l>