8000
Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magma local-license protocol proof of concept

This repository is a deliberately small proof of concept for launching a locally installed research program after authorization by a host service. It models a possible CoCalc/Magma integration without putting mathematical execution behind a network service:

  • fake-magma is a small C program that embeds an Ed25519 public key. It generates a fresh 256-bit nonce at startup, obtains and verifies a signed launch ticket, and then implements exactly one expression: 2+2.
  • license-server is a documented C host service. C keeps this small proof of concept self-contained and makes the security-sensitive system calls easy to inspect; it is not a requirement for a deployed server. It authenticates the Unix socket peer, hashes the peer executable independently through /proc, and returns a short-lived ticket bound to the nonce, executable, and project.
  • make demo-podman runs the signer on the Linux host while running fake-magma in a Podman container with only a project-specific socket directory bind-mounted into it.

This project is not Magma and is not affiliated with the Magma Computational Algebra System. It demonstrates a protocol boundary, not production copy protection.

Quick start

Requirements on a Debian or Ubuntu Linux host are a C17 compiler, GNU Make, and the OpenSSL development package:

sudo apt-get install build-essential libssl-dev
make test
make demo

The host demonstration prints output like:

$ fake-magma
Fake Magma: signed launch ticket accepted.
4

--- signing server audit log ---
license-server: peer pid=... uid=... gid=...
license-server: executable=/.../build/fake-magma dev=... ino=...
license-server: executable-sha256=...
license-server: GRANTED project=cocalc-demo-project ticket=... expires=...

fake-magma will not start without a valid signer, rejects a ticket captured from another launch because its nonce differs, and rejects a ticket issued for a different executable hash.

You can also run a demo directly by explicitly starting the server in one terminal:

~/magma-license-poc/scripts$ ./start-license-server.sh 
license-server: READY socket=/tmp/tmp.HKjOZOUVBu/magma.sock project=cocalc-demo-project

then start the fake-magma clilent pointed at the socket:

~/magma-license-poc/build$ ./fake-magma  # should NOT work:
fake-magma: cannot connect to signer at /run/magma-license/magma.sock: No such file or directory
~/magma-license-poc/build$ ./fake-magma --socket /tmp/tmp.HKjOZOUVBu/magma.sock  # works with provided info for one start
Fake Magma: signed launch ticket accepted.
> 2+2
4
~/magma-license-poc/build$ 

Podman demonstration

Install Podman and run:

make demo-podman

The demonstration performs this flow:

 Podman container                         Linux host
┌────────────────────────┐               ┌─────────────────────────────┐
│ fake-magma             │               │ license-server              │
│                        │ AF_UNIX       │                             │
│ fresh getrandom nonce ─┼──────────────►│ SO_PEERCRED                 │
│                        │               │ pidfd_open(peer PID)        │
│ embedded public key    │               │ hash /proc/<PID>/exe       │
│                        │◄───────────────┤ sign nonce/build/project   │
│ verify, then run 2+2   │ signed ticket │                             │
└────────────────────────┘               └─────────────────────────────┘
       ▲
       │ read-only bind mount
       │ /run/magma-license/magma.sock

The signer logs the host-visible peer PID, UID, executable inode and hash, and cgroup. The demonstration computes the approved hash from the binary in the container image; it does not trust a hash claimed by the client.

The script works directly with normal rootless Podman. With rootful Podman, run the complete demonstration as root (sudo make demo-podman) so that the host signer is allowed to inspect the container process through /proc. A real CoCalc licensing agent would already be a privileged host service.

Manual demonstration

The scripts are intentionally thin. The complete host interaction can also be run by hand:

make
mkdir -p /tmp/magma-license-demo
HASH=$(build/fake-magma --print-self-sha256)

build/license-server \
  --socket /tmp/magma-license-demo/magma.sock \
  --project-id example-project \
  --allow-sha256 "$HASH" \
  --once

In a second terminal:

printf '2+2\n' | build/fake-magma \
  --socket /tmp/magma-license-demo/magma.sock

The --project-id option represents authoritative project metadata associated with that project-specific socket endpoint. In production, the host agent would resolve the peer PID's cgroup/container through the CoCalc project manager and verify that it agrees with the socket endpoint before checking the current entitlement.

What the proof of concept establishes

The server does not trust process environment variables, a PID supplied by the client, or an executable hash supplied by the client. Linux supplies the peer credentials for the connected Unix socket. The server obtains a pidfd and opens /proc/<peer-pid>/exe, then signs only when that independently computed SHA-256 hash is approved.

The signed ticket contains:

  • a protocol-specific magic value and version;
  • issue and expiration times;
  • the client's fresh 256-bit nonce;
  • the server-observed executable SHA-256 hash;
  • a SHA-256 hash of the server's authoritative project ID;
  • a random 128-bit ticket ID; and
  • an Ed25519 signature over every preceding byte.

The ticket is used only for this launch and is never saved. Once it verifies, the program has no further licensing dependency: long computations survive a signer restart or network outage, and the local process remains visible to ordinary tools such as top.

See PROTOCOL.md for the exact byte-level format and verification rules.

Production server and signing-key custody

The C license server in this repository demonstrates the Linux peer-inspection and ticket-signing boundary. It is not intended to prescribe the language or shape of CoCalc's production service. A deployed implementation could instead be written in TypeScript, Go, Rust, or another suitable language so that it can reuse CoCalc infrastructure for entitlement lookups, rate limiting, retries, key rotation, audit logging, monitoring, and operational error handling. It could also split the small privileged peer-inspection component from a larger unprivileged policy service. The wire protocol and the public-key verification inside Magma do not depend on the server implementation language.

Most importantly, a production license-server binary must contain no private signing key, and no usable private signing key should be stored in its config files or anywhere on the host's persistent disk. On startup, the host service would authenticate to a CoCalc hub, enrollment service, or KMS-backed service over an authenticated encrypted network protocol and obtain a unique, short-lived signing credential. The credential would be held only in process memory, never written to disk, logs, or crash reports, and erased when the service exits.

An especially strong variant has the host generate an ephemeral key in memory and send only its public key for a short-lived signed delegation. An acceptable initial variant provisions a unique per-host, per-boot private key over the startup protocol. Neither design sends the long-lived Magma or CoCalc root private key to a host.

Consequently, exfiltrating the license-server executable, its configuration, and its disk does not yield a signing key or allow the attacker to recreate a working signer elsewhere. Compromise of a running privileged host can still expose or misuse its in-memory credential, so that credential must be narrowly scoped and short-lived. A production service should additionally disable core dumps, avoid swapping or diagnostic serialization of secret buffers, and replace or zero those buffers at shutdown. KMS/HSM protection for central keys and non-exportable vTPM-backed host keys are natural later hardening steps.

Explicit limitations

This is reasonable deterrence, not DRM claimed to withstand a hostile machine owner. Someone who can patch the executable, inject code into it, or debug it can bypass client-side checks. In particular, production work should consider dynamic-loader injection and ptrace policy, but no amount of client hardening turns a locally controlled binary into an unextractable secret.

The repository intentionally contains the private half of a public RFC 8032 test key so that the demonstration is reproducible. That key provides no security and must never be used outside this proof of concept. In production:

  1. Magma would embed only the Magma root public key.
  2. The Magma root could certify a constrained CoCalc signing key, including an audience, grant expiration, allowed build IDs, and maximum ticket lifetime.
  3. CoCalc would keep its long-lived private signing key in a KMS/HSM, never in a repository, project container, host binary, config file, or persistent host disk. A host agent would receive only a unique short-lived credential during authenticated startup and keep it in RAM.
  4. The host agent would map peer cgroups to authoritative project records and check a current verified-email entitlement.
  5. Key IDs, rotation, revocation, rate limiting, and audit-log retention would be added.

The proof of concept targets modern Linux because it deliberately uses SO_PEERCRED, pidfd_open, /proc, and pathname Unix sockets. The production CoCalc use case has exactly that platform boundary.

Source layout

include/mlp_protocol.h       fixed wire-format API
src/protocol.c               explicit encoding and ticket validation
src/crypto.c                 getrandom, SHA-256, and Ed25519 wrappers
src/io.c                     AF_UNIX SOCK_SEQPACKET transport
src/fake_magma.c             public-key client and tiny evaluator
src/license_server.c         peer authentication and signing server
tests/                       protocol and end-to-end denial tests
scripts/                     host and Podman demonstrations

The code is MIT licensed.

About

Proof of concept for nonce-bound signed launch tickets across a local Unix socket and Podman boundary

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0