access_mask is a bitfield, and every gate tests one bit (L4 auth / ACL)

There is no access level and no ordering among the rights. acl_right_t holds two single bits, READ and WRITE, a stored ACE may carry either or both, and each door in the graph asks for one of them (RFC-0036, reference/05 §0x0A).

The example gives subjects READ, WRITE or both on one vertex and runs every door for each, so every allow is the bit under test and every deny is a caller authorized for the other bit rather than for nothing.

What to notice

  • A : field is a path under its vertex. It has no ACL of its own: reading any field, :acl included, needs READ on the vertex, and writing one needs WRITE.

  • Subscribing is a read. Appending to :subscribers[] needs READ on the source vertex, because a subscription delivers what a read would. The dual question, “who may write into me?”, is the target’s WRITE bit, evaluated at delivery (reference/13 §Delivery and the two ACLs).

  • Creating is a write to the parent. :children[] creation, a creation hook and the creator endpoint all ask for WRITE.

  • Writing :acl is not a right. Only the owner, the empty local caller, may do it, so a subject holding both bits still cannot rewrite the policy. Remote administration goes through an owner-app vertex (reference/13).

  • A :subscribers[N] slot is its holder’s. A subject with READ may clear or replace its own slot, never another’s or the owner’s.

  • A bit outside READ|WRITE is refused at install with TYPE_MISMATCH, so a retired right is never stored and left inert.

  • Nothing here is conditional — the target builds and runs under every CI leg.

Source

  1/*
  2 * SPDX-License-Identifier: Apache-2.0
  3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
  4 */
  5
  6/**
  7 * @file
  8 * @brief ONE CONCEPT — `access_mask` holds two rights, and a `:` field is a path under its
  9 *        vertex (RFC-0036).
 10 *
 11 * `acl_right_t` has two single bits, READ and WRITE, and every gate in the graph asks for one
 12 * of them (docs/reference/05 §0x0A). A `:` field has no ACL of its own: reading any field of a
 13 * vertex needs READ on it, and writing one needs WRITE. Subscribing is a read, because a
 14 * subscription delivers what a read would; creating a child is a write to the parent.
 15 *
 16 * Two things are not rights at all. Writing `:acl` is the OWNER's, the empty local caller's,
 17 * and no ACE can grant it, so a subject holding both bits still cannot rewrite the policy. A
 18 * deployment that wants remote administration registers an owner-app vertex whose handler
 19 * applies ACEs by the application's own policy (reference/13). And a `:subscribers[N]` slot is
 20 * its holder's: a subject with READ may clear its own, never another's.
 21 *
 22 * A mask bit outside READ|WRITE is refused at install with `TYPE_MISMATCH`, so a retired right
 23 * is never stored and left inert.
 24 *
 25 * Runs under ctest as `example_acl_right_bits`; returns non-zero on any failed check.
 26 */
 27
 28#include <cstdio>
 29#include <cstring>
 30#include <span>
 31#include <string_view>
 32#include <vector>
 33
 34#include "libtracer/graph.hpp"
 35#include "libtracer/mem_heap.hpp"
 36#include "libtracer/security_acl.hpp"
 37#include "libtracer/tlv_emit.hpp"
 38
 39namespace {
 40
 41using tr::graph::ace_t;
 42using tr::graph::acl_right_t;
 43using tr::graph::graph_t;
 44using tr::graph::path_t;
 45using tr::graph::role_t;
 46using tr::graph::status_t;
 47using tr::graph::vertex_handle_t;
 48using tr::wire::opt_t;
 49using tr::wire::type_t;
 50
 51/** @brief Report expectation @p what and record a failure on @p ok. */
 52void check(bool& ok, bool cond, const char* what) {
 53    std::printf("  [%s] %s\n", cond ? "ok" : "FAIL", what);
 54    ok = ok && cond;
 55}
 56
 57/** @brief Append @p subject to the gate's buffer @p out; a refused append fails closed. */
 58std::expected<void, tr::wire::err_t> put_subject(tr::mem::bytes_t& out, std::string_view subject) {
 59    if (!out.append(reinterpret_cast<const std::byte*>(subject.data()), subject.size()))
 60        return std::unexpected(tr::wire::err_t::ACCESS_DENIED);
 61    return {};
 62}
 63
 64/** @brief The caller context IS the subject token — the resolver is not what this example is
 65 *         about (see acl_subject_resolver for the seam itself). */
 66std::expected<void, tr::wire::err_t> caller_is_subject(void*, std::string_view caller,
 67                                                       tr::mem::bytes_t& out, std::uint64_t&) {
 68    return put_subject(out, caller);
 69}
 70
 71/** @brief @p right as the single `access_mask` bit it is. */
 72constexpr std::uint32_t bit(acl_right_t right) { return static_cast<std::uint32_t>(right); }
 73
 74/** @brief A one-byte VALUE — the payload the WRITE gate carries. */
 75tr::view::view_t some_value() {
 76    const std::byte one[1] = {std::byte{0x01}};
 77    return *tr::view::over_bytes(one);
 78}
 79
 80/** @brief A `SUBSCRIBER{PATH}` naming @p target — the value a `:subscribers[]` append carries. */
 81tr::view::view_t subscriber_to(std::string_view target) {
 82    std::vector<std::byte> path;
 83    (void)tr::wire::emit_path_segment(path, target);
 84    std::vector<std::byte> path_tlv;
 85    tr::wire::emit_tlv(path_tlv, type_t::PATH, opt_t{}, path);
 86    std::vector<std::byte> out;
 87    tr::wire::emit_tlv(out, type_t::SUBSCRIBER, opt_t{.pl = true}, path_tlv);
 88    return *tr::view::over_bytes(out);
 89}
 90
 91/** @brief A `SPEC` creating a STORED_VALUE child named @p name — the `:children[]` value. */
 92tr::view::view_t child_named(std::string_view name) {
 93    std::vector<std::byte> body;
 94    tr::wire::emit_name(body, "type");
 95    tr::wire::emit_name(body, "stored_value");
 96    tr::wire::emit_name(body, "name");
 97    tr::wire::emit_name(body, name);
 98    std::vector<std::byte> out;
 99    tr::wire::emit_tlv(out, type_t::SPEC, opt_t{.pl = true}, body);
100    return *tr::view::over_bytes(out);
101}
102
103/** @brief True iff @p r was refused by an ACL gate. */
104template <class T>
105bool denied(const tr::graph::result_t<T>& r) {
106    return !r.has_value() && r.error() == status_t::PERMISSION_DENIED;
107}
108
109}  // namespace
110
111int main() {
112    bool ok = true;
113    graph_t g;
114    g.set_hook(&tr::graph::graph_hooks_t::subject_lookup, {caller_is_subject, nullptr});
115    const vertex_handle_t v = g.register_vertex(path_t("/x"), role_t::STORED_VALUE);
116    (void)g.write(v, some_value());  // trusted local seed, so a refused read is the only failure
117
118    // One ACE per right, one subject per ACE, and one subject holding both.
119    const ace_t aces[] = {
120        {.subject = tr::graph::subject_of("reader"), .access_mask = bit(acl_right_t::READ)},
121        {.subject = tr::graph::subject_of("writer"), .access_mask = bit(acl_right_t::WRITE)},
122        {.subject = tr::graph::subject_of("both"),
123         .access_mask = bit(acl_right_t::READ) | bit(acl_right_t::WRITE)},
124    };
125    (void)g.write(path_t("/x:acl"), *tr::view::over_bytes(tr::graph::encode_acl(aces)));
126
127    const auto acl_field = path_t::parse("/x:acl");
128    const auto subscribers = path_t::parse("/x:subscribers[]");
129    const auto children = path_t::parse("/x:children[]");
130
131    // READ (0x01) — the read door, every `:` field read, and subscribe.
132    check(ok, g.read(v, "reader").has_value(), "READ: the reader reads");
133    check(ok, denied(g.read(v, "writer")), "READ: the writer may not — WRITE is a different bit");
134    check(ok, g.read(v, acl_field->field(), "reader").has_value(),
135          "READ: the reader reads the :acl, a field like any other");
136    check(ok, g.write(v, subscribers->field(), subscriber_to("sink"), "reader").has_value(),
137          "READ: the reader subscribes — a subscription delivers what a read would");
138    check(ok, denied(g.write(v, subscribers->field(), subscriber_to("sink"), "writer")),
139          "READ: a WRITE grant does not buy a subscription");
140
141    // WRITE (0x02) — the write door, every `:` field write, creation, and fan-in deliveries.
142    check(ok, g.write(v, some_value(), "writer").has_value(), "WRITE: the writer writes");
143    check(ok, denied(g.write(v, some_value(), "reader")), "WRITE: the reader may not");
144    check(ok, g.write(v, children->field(), child_named("kid"), "writer").has_value(),
145          "WRITE: the writer creates /x/kid — creating is a write to the parent");
146    check(ok, denied(g.write(v, children->field(), child_named("other"), "reader")),
147          "WRITE: the reader may not create a child");
148
149    // `:acl` writes are the owner's: no ACE grants them, both bits included.
150    const ace_t next[] = {
151        {.subject = tr::graph::subject_of("both"), .access_mask = bit(acl_right_t::READ)}};
152    const tr::view::view_t next_acl = *tr::view::over_bytes(tr::graph::encode_acl(next));
153    check(ok, denied(g.write(v, acl_field->field(), next_acl, "both")),
154          ":acl: a remote subject holding READ|WRITE cannot rewrite the policy");
155    check(ok, g.write(*acl_field, next_acl).has_value(), ":acl: the owner can");
156
157    // A retired right is refused at install, and nothing is stored.
158    const ace_t retired[] = {{.subject = tr::graph::subject_of("watcher"), .access_mask = 0x04}};
159    const auto refused = g.write(*acl_field, *tr::view::over_bytes(tr::graph::encode_acl(retired)));
160    check(ok, !refused.has_value() && refused.error() == status_t::TYPE_MISMATCH,
161          "a mask bit outside READ|WRITE is refused with TYPE_MISMATCH");
162
163    std::printf("two rights, two single-bit grants: 0x%02x 0x%02x\n", bit(acl_right_t::READ),
164                bit(acl_right_t::WRITE));
165    return ok ? 0 : 1;
166}

See also: security-acl module · protocol TLVs · network formation · open by default · inheritance.