From nobody Sat Mar 23 02:07:17 2024 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4V1jJK75WSz5FvxV for ; Sat, 23 Mar 2024 02:07:29 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-ua1-f54.google.com (mail-ua1-f54.google.com [209.85.222.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4V1jJK5DTnz4nk4 for ; Sat, 23 Mar 2024 02:07:29 +0000 (UTC) (envelope-from asomers@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-ua1-f54.google.com with SMTP id a1e0cc1a2514c-7e046990b6aso1164096241.1 for ; Fri, 22 Mar 2024 19:07:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711159649; x=1711764449; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/ESsYLClGuGUl4zsMMdQTHx35z1rReZre65BKj8DpgY=; b=YdN9LjoCt4FNzXMUz05UZEZTxoQy2bcB4UgdYMdmm/dscQvf8Ylu8vSZAf/3nAiO5e hZiAoCRdlIBE5pmEAFkezAjFn+EjwWoXUStkLj9MmPXyqqI8GimPGQszwOtekIIkbrcI /FZYXE+HMZM4G2dUWSP49adS5oIaKxd5RwQpbknPZvtA3+uGofFTiXbCTWg/1nyzw+/n +2FhMn5nFc422SJtconU1EznItI19d9k9j5s2VWSEpFpN8ivxSdEwh73AKzJrZ9MT53E AmiNyEGHdhYHxaXuMycgHtBIMyXJiafv2tnI7SIEh7aklyK8N4bpsRtYeITsgS7Cz6dE KUkA== X-Gm-Message-State: AOJu0Yzx6DOzy/Np+ALjy93a+u/piIQ90MuYhFUgkvYECT07ZxS39n8E 8gw8DbK+U7OBOaYeYKUNM50nL8FlLu+SapHFRatUeDsQK2eSb0seGudFde38kmsAALkph6l9I2o 3T86NklDaJ6yrfssZ6SMztAUgNvQ= X-Google-Smtp-Source: AGHT+IHxwMw2CEPVPmvD//yloN9HZVW3wsWg6L1YLWDO+7MVfdjUvpBFgsM2V/TUBPHyBUu2o5KF9wOoz5/ccqA1QJY= X-Received: by 2002:a05:6122:32c7:b0:4d4:3fc:2869 with SMTP id ck7-20020a05612232c700b004d403fc2869mr1465338vkb.15.1711159648708; Fri, 22 Mar 2024 19:07:28 -0700 (PDT) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 References: In-Reply-To: From: Alan Somers Date: Fri, 22 Mar 2024 20:07:17 -0600 Message-ID: Subject: Re: Filesystem extended attributes and Capsicum To: Shawn Webb Cc: freebsd-hackers@freebsd.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US] X-Rspamd-Queue-Id: 4V1jJK5DTnz4nk4 On Fri, Mar 22, 2024 at 6:56=E2=80=AFPM Shawn Webb wrote: > > On Fri, Mar 22, 2024 at 06:20:48PM -0600, Alan Somers wrote: > > On Fri, Mar 22, 2024 at 5:38=E2=80=AFPM Shawn Webb 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)): > > > > > > =3D=3D=3D=3D BEGIN CODE =3D=3D=3D=3D > > > static int > > > open_file(const char *path) > > > { > > > cap_rights_t rights; > > > int fd; > > > > > > fd =3D open(path, O_PATH | O_CLOEXEC); > > > if (fd =3D=3D -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); > > > } > > > =3D=3D=3D=3D END CODE =3D=3D=3D=3D > > > > > > Eventually, after calling cap_enter(2), the following code is called: > > > > > > =3D=3D=3D=3D BEGIN CODE =3D=3D=3D=3D > > > #define ATTRNAME_ENABLED "hbsd.pax.aslr" > > > sz =3D extattr_get_fd(fd, ctx->hc_namespace, ATTRNAME_ENABLED= , NULL, 0); > > > if (sz <=3D 0) { > > > if (errno =3D=3D ENOATTR) { > > > /* > > > * This is okay, it just means that nothing ha= s been set. > > > * No error condition here. > > > */ > > > return (RES_SUCCESS); > > > } > > > return (RES_FAIL); > > > } > > > =3D=3D=3D=3D END CODE =3D=3D=3D=3D > > > > > > For reference, the program's code is here: > > > https://git.hardenedbsd.org/shawn.webb/hbsdctrl/-/tree/main?ref_type= =3Dheads > > > > > > 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/c= urrent/hbsdcontrol-v2/lib/libhbsdcontrol?ref_type=3Dheads > > > > > > From the rights(4) manual page, I'm instructed all I need are to appl= y > > > those capabilities to that file descriptor: > > > > > > =3D=3D=3D=3D BEGIN PASTE =3D=3D=3D=3D > > > 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). > > > =3D=3D=3D=3D END PASTE =3D=3D=3D=3D > > > > > > 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_We= bb/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 =3D=3D 93 && pid =3D=3D $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/0= 3A4CBEBB82EA5A67D9F3853FF2E67A277F8E1FA.pub.asc Without dtrace, you've got your work cut out for you. I suggest simply adding all capabilities, verifying that extattr_get_fd works, and then removing capabilities until it fails. Or, run your program on vanilla FreeBSD with dtrace.