Development documentation

This is the in-progress lota-next branch. For the released version, see the stable documentation.

Anti-cheat SDK

liblota_anticheat.so carries both ends of an anti-cheat session. In the game process the producer opens a session and builds heartbeats: small packets that wrap a token whose nonce binds the session, the sequence number, the time, the game and the process's own code. On the backend the verifier checks a heartbeat with the token verification of Server SDK. The header is lota_anticheat.h (include/lota_anticheat.h), the implementation src/sdk/lota_anticheat.c and the pkg-config module lota-anticheat:

cc $(pkg-config --cflags lota-anticheat) -o producer producer.c \
   $(pkg-config --libs lota-anticheat)

The library links the gaming and server objects in, with their symbols local (Public API and ABI surface), so a program that also calls lota_connect() or lota_server_verify_token() itself links liblotagaming.so or liblotaserver.so as well. A working producer and backend are in examples/demo_anticheat and examples/demo_server.

Sessions

lota_ac_init() takes a struct lota_ac_config:

struct_size

sizeof(struct lota_ac_config). A value below LOTA_AC_CONFIG_SIZE_MIN is refused with LOTA_AC_ERR_CONFIG_SIZE.

provider

LOTA_AC_PROVIDER_EAC or LOTA_AC_PROVIDER_BATTLEYE. The value goes into every heartbeat; the library attaches no other meaning to it.

game_id

The title's identifier, 1 to 63 bytes.

required_flags

LOTA_FLAG_* bits the session requires besides LOTA_FLAG_ATTESTED before it reports itself trusted.

direct

1 to talk to the agent through the gaming SDK, 0 to read the files of the Wine hook.

token_dir

File mode: the hook's output directory. NULL takes a non-empty $LOTA_HOOK_TOKEN_DIR whether or not the directory exists, and an empty string skips it. Otherwise the first of $XDG_RUNTIME_DIR/lota-hook and /tmp/lota-<uid> that is a directory is taken. With neither, it is $XDG_RUNTIME_DIR/lota-hook, and without that variable the session starts in LOTA_AC_STATE_ERROR with LOTA_AC_ERR_TOKEN_DIR.

socket_path, publisher_profile

Direct mode: as in struct lota_connect_opts (Gaming SDK). A host enrolled with several publishers refuses a session that names none.

At init the library draws a random 16-byte session id and computes the game binding hash of the running executable (below). In direct mode it connects to the agent; a session that finds no agent is returned in LOTA_AC_STATE_ERROR with the reason in lota_ac_last_error(), and never connects later, so the title opens another session once the host is ready. NULL means a bad configuration, no memory, or a failed session id or game binding hash.

lota_ac_tick() refreshes the state: in direct mode one status request and one token request with no nonce, in file mode a read of the snapshot file. The state is then:

LOTA_AC_STATE_TRUSTED

LOTA_FLAG_ATTESTED and every required_flags bit are set.

LOTA_AC_STATE_UNTRUSTED

A token, or a status with any flag set, arrived without those bits.

LOTA_AC_STATE_ERROR

Nothing to judge: no agent, no snapshot or one whose token has expired, a refused request, or neither a token nor any flag.

LOTA_AC_STATE_IDLE is a NULL session, and LOTA_AC_STATE_RUNNING does not outlast lota_ac_init(). lota_ac_get_state() and lota_ac_get_info() read this state out of the game's own memory, which the player controls; they serve the title's interface, and a backend trusts only a verified heartbeat. lota_ac_shutdown() closes the connection and frees the session.

Heartbeats

lota_ac_heartbeat() builds one packet in direct mode. Each call measures the process's loaded code, computes the heartbeat nonce and asks the agent for a token over it, so a beat costs one token of the agent's rate limit (Tokens). The token sets the state from its flags; a token request that times out or fails with LOTA_ERR_PROTOCOL, as one to a lost agent does, sets LOTA_AC_STATE_ERROR. The sequence number starts at 0 and grows by one with each packet built. File mode returns -EOPNOTSUPP: the hook's token was issued with no nonce, so it cannot carry a heartbeat's.

#include <lota/lota_anticheat.h>

struct lota_ac_config cfg = {
        .struct_size = sizeof(cfg),
        .provider = LOTA_AC_PROVIDER_EAC,
        .game_id = "example-title",
        .direct = 1,
        .publisher_profile = publisher_hex,
};
struct lota_ac_session *s = lota_ac_init(&cfg);
uint8_t pkt[LOTA_AC_MAX_HEARTBEAT];
size_t len;

if (!s)
        return -1;
if (lota_ac_heartbeat(s, pkt, sizeof(pkt), &len) == 0)
        send_to_backend(pkt, len);
else
        show(lota_ac_strerror(lota_ac_last_error()));

The heartbeat nonce is

SHA256("lota-ac-heartbeat:v1\0" || session_id || provider ||
       sequence || timestamp || game_id_hash || domain_version ||
       runtime_measure)

with integers little-endian. The header's lota_flags are the token's, which the TPM signs with the token, so the signature covers every header field but magic, version, total_size and token_size. domain_version selects the pair of domain strings used for the nonce and the game binding; LOTA_AC_DOMAIN_VERSION_V1 is the only one.

lota_ac_boot_evidence() returns the boot evidence that goes with the session's tokens, from the agent in direct mode or from the hook's lota-boot-evidence.bin in file mode. A buffer of LOTA_BOOT_EVIDENCE_MAX_SIZE holds it. The producer sends it with a heartbeat.

Game binding and runtime measure

The game binding hash names the title and its executable on disk:

SHA256("lota-ac-game-binding:v1\0" || game_id || SHA256(executable))

The producer hashes /proc/self/exe at init. The backend computes the value it expects with lota_ac_compute_game_binding_hash() from the game_id and a copy of the executable it ships.

The runtime measure hashes the code the process has mapped at the time of the beat: the executable PT_LOAD segments of every file-backed object dl_iterate_phdr() lists, the vDSO left out. Each object's digest covers its segments in ascending address; the combined digest folds the objects sorted by file basename, so neither load order nor library path changes it. The layout is in the header comment. lota_ac_list_runtime_objects() lists the files it covers, and lota_ac_compute_expected_runtime_measure_set() computes the same digest from those files on the backend. The live and the file digests differ for an object with DT_TEXTREL. A process started by running the dynamic loader on its executable cannot be measured (-ENOTSUP), nor can more than 256 objects or 16 executable segments in one (-E2BIG).

The measure is computed inside the game process, so a player who controls that process controls the value. The nonce keeps it from changing between the producer and the backend; it says nothing a cheat could not have prepared. What the agent measured of the process is in the token's runtime_protect_digest and the registry (Server SDK).

Verifying a heartbeat

lota_ac_verify_heartbeat() takes the packet, the client's AIK, the game binding hash and the runtime measure the backend expects, and fills struct lota_ac_info. It checks, in order:

  1. magic, version, the sizes, the provider and domain_version;

  2. game_id_hash against the expected one;

  3. runtime_measure against the expected one (LOTA_AC_ERR_RUNTIME_IMAGE);

  4. timestamp within 120 seconds of the backend's clock;

  5. the token, with lota_server_verify_token() and the heartbeat nonce recomputed from the header as expected_nonce;

  6. the header's lota_flags against the token's.

info.state is LOTA_AC_STATE_TRUSTED when the signed flags carry LOTA_FLAG_ATTESTED; the producer's required_flags are not in the packet, and a backend that requires more bits reads info.lota_flags, the token's word. info.pcr_mask and info.pcr_digest are the token's, for lota_ac_verify_boot_evidence(), which applies the boot evidence rules of Server SDK and returns LOTA_AC_ERR_BAD_EVIDENCE on a mismatch. info.session_start is 0, as one packet does not carry it.

The backend issues no nonce: the producer derives it, so the freshness of a packet rests on the 120-second timestamp window and the token's expiry. The verifier keeps no state between calls. A backend that refuses a packet seen before records the last sequence of each session_id and accepts only a higher one.

The AIK is the one the backend holds on record for the account, as for Server SDK. Without it the call returns LOTA_AC_ERR_INVALID_ARG.

Snapshot file

In file mode the session reads lota-attestation.bin (LOTA_SNAPSHOT_FILE_NAME) in the token directory, which the Wine hook writes. lota_snapshot.h (include/lota_snapshot.h) gives its 16-byte header, little-endian, followed by the token:

Offset

Size

Field

Content

0

4

magic

LOTA_SNAPSHOT_MAGIC, "LOTA" in memory.

4

2

version

LOTA_SNAPSHOT_VERSION (1).

6

2

reserved

8

4

flags

The LOTA_FLAG_* word the hook last read.

12

4

token_size

LOTA_TOKEN_HEADER_SIZE to LOTA_AC_MAX_TOKEN bytes of token follow.

The file sits in a directory the player owns, so its flags are what the session shows locally and nothing more.

Heartbeat wire format

struct lota_ac_heartbeat_wire is the LOTA_AC_HEADER_SIZE (110) byte header, little-endian and packed, followed by the token. A packet is at most LOTA_AC_MAX_HEARTBEAT bytes.

Offset

Size

Field

Content

0

4

magic

LOTA_AC_MAGIC, "HCAL" on the wire.

4

1

version

LOTA_AC_VERSION (1).

5

1

provider

enum lota_ac_provider.

6

2

total_size

Header and token.

8

16

session_id

Random, per session.

24

4

sequence

Packet number in the session.

28

4

lota_flags

The token's LOTA_FLAG_* word.

32

8

timestamp

Unix time the packet was built.

40

32

game_id_hash

The game binding hash.

72

2

token_size

Bytes of token.

74

4

domain_version

LOTA_AC_DOMAIN_VERSION_*.

78

32

runtime_measure

The producer's runtime measure.

Errors

The codes lie below LOTA_AC_ERR_BASE (-1000), so none collides with the negative errno the hash and measure functions return. The producer functions return a negative errno and leave the LOTA_AC_ERR_* reason in lota_ac_last_error(), kept per thread; the verifiers return the LOTA_AC_ERR_* code itself. lota_ac_strerror() words either kind.

Value

Code

Meaning

-1001

LOTA_AC_ERR_INVALID_ARG

A bad argument, a buffer too small, or no AIK.

-1002

LOTA_AC_ERR_MALFORMED

The packet does not parse, names another game, lies outside the timestamp window, or its flags differ from the token's.

-1003

LOTA_AC_ERR_VERSION

Another version, domain_version or token version.

-1004

LOTA_AC_ERR_SIG_FAIL

The token signature does not verify.

-1005

LOTA_AC_ERR_NONCE_FAIL

The token does not answer the heartbeat nonce.

-1006

LOTA_AC_ERR_EXPIRED

The token expired or expires beyond the window.

-1007

LOTA_AC_ERR_CRYPTO

The key does not decode or is not RSA of 2048 bits or more, or OpenSSL failed.

-1008

LOTA_AC_ERR_CONFIG_SIZE

struct_size unset or too small.

-1009

LOTA_AC_ERR_GAME_ID

game_id missing, empty or too long.

-1010

LOTA_AC_ERR_PROVIDER

Neither EAC nor BattlEye.

-1011

LOTA_AC_ERR_NO_MEMORY

An allocation failed.

-1012

LOTA_AC_ERR_INTERNAL

The session id or the game binding hash failed.

-1013

LOTA_AC_ERR_NO_AGENT

No agent answers, or file mode found no snapshot or one whose token has expired.

-1014

LOTA_AC_ERR_CONSENT_REQUIRED

Nobody on the host agreed to that publisher.

-1015

LOTA_AC_ERR_UNKNOWN_PROFILE

The host is not enrolled with that publisher, or with several and none was named.

-1016

LOTA_AC_ERR_ACCESS_DENIED

The agent refused the caller.

-1017

LOTA_AC_ERR_TOKEN_DIR

File mode found no hook directory.

-1018

LOTA_AC_ERR_NOT_ATTESTED

The host holds no verdict to issue a token on.

-1019

LOTA_AC_ERR_RATE_LIMITED

Beating faster than the agent issues tokens.

-1020

LOTA_AC_ERR_STATUS

The agent refused a status request.

-1021

LOTA_AC_ERR_TOKEN

The agent refused a token, or the request for one failed.

-1022

LOTA_AC_ERR_MEASURE

The runtime measure failed.

-1023

LOTA_AC_ERR_SERIALIZE

The token did not serialize.

-1024

LOTA_AC_ERR_RUNTIME_IMAGE

The runtime measure is not the expected one, or the token's PID and registry digests do not match its signed digest.

-1025

LOTA_AC_ERR_NO_KEY_SLOT

The agent has no attestation-key slot left for that publisher.

-1026

LOTA_AC_ERR_TPM_LOCKOUT

The TPM is in dictionary-attack lockout.

-1027

LOTA_AC_ERR_NO_EVIDENCE

No boot evidence to hand over.

-1028

LOTA_AC_ERR_BAD_EVIDENCE

Boot evidence malformed or of another boot.

Functions

struct lota_ac_session *lota_ac_init(const struct lota_ac_config *cfg)

Create an anti-cheat session.

Parameters

const struct lota_ac_config *cfg

The session's configuration; struct_size set by the caller.

Direct mode connects to the agent, file mode locates the Wine hook directory.

A direct-mode session that found no agent never connects later: a host that gets ready takes a new session.

Return

The session, in LOTA_AC_STATE_ERROR if it got no verdict. NULL on error. lota_ac_last_error() holds the reason.

int lota_ac_last_error(void)

Why the last call on this thread failed.

Set by every call that returns a value to check, LOTA_AC_ERR_OK on success: errno for the measure helpers, LOTA_AC_ERR_* otherwise. The renderers, lota_ac_get_state() and lota_ac_shutdown() leave it alone. Thread-local, as a title and its launcher can share a process.

Return

The code the last such call left.

const char *lota_ac_strerror(int err)

The error code as a sentence.

Parameters

int err

A LOTA_AC_ERR_* code or a negative errno.

Return

Stable English with no trailing punctuation, never NULL.

void lota_ac_shutdown(struct lota_ac_session *session)

Destroy a session.

Parameters

struct lota_ac_session *session

From lota_ac_init(), or NULL.

enum lota_ac_state lota_ac_get_state(const struct lota_ac_session *session)

Current session state.

Parameters

const struct lota_ac_session *session

From lota_ac_init(), or NULL.

Local telemetry: client memory can be hooked, so a server trusts only lota_ac_verify_heartbeat().

Return

The state, LOTA_AC_STATE_IDLE if session is NULL.

int lota_ac_get_info(const struct lota_ac_session *session, struct lota_ac_info *info)

Local session telemetry.

Parameters

const struct lota_ac_session *session

From lota_ac_init().

struct lota_ac_info *info

Filled on success.

Not authoritative, as with lota_ac_get_state().

Return

0 on success, -EINVAL if an argument is NULL.

int lota_ac_tick(struct lota_ac_session *session)

Refresh session state from the agent or the hook files.

Parameters

struct lota_ac_session *session

From lota_ac_init().

Return

0 on success, negative errno on failure.

int lota_ac_heartbeat(struct lota_ac_session *session, uint8_t *buf, size_t buflen, size_t *written)

Generate a heartbeat packet.

Parameters

struct lota_ac_session *session

From lota_ac_init().

uint8_t *buf

Receives the packet.

size_t buflen

Size of buf.

size_t *written

The packet size, on success.

A token sets the state from its flags. A token request that times out or fails with LOTA_ERR_PROTOCOL, as one to a lost agent does, sets LOTA_AC_STATE_ERROR. The token nonce binds sequence, timestamp, session and game, which takes a live token, so file mode has no heartbeat.

Return

0 on success, -EINVAL for bad arguments, -ENOTCONN without an agent, -ENODATA if no token is available, -ENOSPC if buf is too small, -EOPNOTSUPP in file mode, another negative errno on failure.

int lota_ac_boot_evidence(struct lota_ac_session *session, uint8_t *buf, size_t buflen, size_t *written)

Fetch the boot evidence for this session's tokens.

Parameters

struct lota_ac_session *session

From lota_ac_init().

uint8_t *buf

Receives the evidence.

size_t buflen

Size of buf.

size_t *written

The evidence size, on success.

Layout in lota_token.h, from the agent in direct mode or the hook's lota-boot-evidence.bin in file mode. Send it with a heartbeat. A buffer of LOTA_BOOT_EVIDENCE_MAX_SIZE holds any.

Return

0 on success, -EINVAL for bad arguments, -ENOTCONN without an agent, -ENODATA if there is none to give, -ENOSPC if buf is too small, -EIO when the agent refused.

int lota_ac_compute_game_binding_hash(const char *game_id, const char *exe_path, uint8_t out[LOTA_AC_GAME_HASH_SIZE])

Expected game hash for a server.

Parameters

const char *game_id

The title's game_id, as in lota_ac_config.

const char *exe_path

The title's main executable.

uint8_t out[LOTA_AC_GAME_HASH_SIZE]

Receives the hash.

SHA-256("lota-ac-game-binding:v1\0" || game_id ||

SHA-256(executable-bytes))

Return

0 on success, negative errno on failure.

int lota_ac_compute_runtime_measure(uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE])

Measure the calling process's live image.

Parameters

uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE]

Receives the digest.

Return

0 on success, -EINVAL on a NULL argument, -ENOMEM on allocation failure, -E2BIG with too many objects or segments in one, -EIO on a digest failure, -ENOENT if no file-backed executable object is found, -ENOTSUP if the process was started by running the dynamic loader on the executable.

int lota_ac_list_runtime_objects(lota_ac_runtime_object_fn fn, void *user)

Enumerate what the runtime measure covers.

Parameters

lota_ac_runtime_object_fn fn

Called with each object's path.

void *user

Passed to fn.

Use it to capture the trusted runtime manifest.

Return

0 on success or when fn stops early, -EINVAL on a NULL fn, -ENOTSUP if the process was started by running the dynamic loader on the executable, negative errno if the main executable path cannot be resolved.

int lota_ac_compute_expected_runtime_measure_set(const char *const *paths, size_t count, uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE])

Measure of ELF files.

Parameters

const char *const *paths

The ELF files the image is made of.

size_t count

Number of paths.

uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE]

Receives the digest.

Objects are keyed by file basename and sorted, so the order of paths does not matter. A file with no executable PT_LOAD segment contributes nothing.

Return

0 on success, -EINVAL on a NULL argument, -ENOENT if the set yields no executable object, -E2BIG if too many objects or segments, negative errno on an I/O or parse failure.

int lota_ac_compute_expected_runtime_measure(const char *exe_path, uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE])

Runtime measure of a single ELF.

Parameters

const char *exe_path

The ELF file.

uint8_t out[LOTA_AC_RUNTIME_MEASURE_SIZE]

Receives the digest.

Same as lota_ac_compute_expected_runtime_measure_set(&exe_path, 1, out).

Return

As lota_ac_compute_expected_runtime_measure_set().

int lota_ac_verify_heartbeat(const uint8_t *data, size_t len, const uint8_t *aik_pub_der, size_t aik_pub_len, const uint8_t expected_game_id_hash[LOTA_AC_GAME_HASH_SIZE], const uint8_t expected_runtime_measure[LOTA_AC_RUNTIME_MEASURE_SIZE], struct lota_ac_info *info)

Verify a heartbeat packet.

Parameters

const uint8_t *data

The packet.

size_t len

Size of data.

const uint8_t *aik_pub_der

Mandatory; unverified claims are attacker-controlled.

size_t aik_pub_len

Size of aik_pub_der.

const uint8_t expected_game_id_hash[LOTA_AC_GAME_HASH_SIZE]

See lota_ac_compute_game_binding_hash().

const uint8_t expected_runtime_measure[LOTA_AC_RUNTIME_MEASURE_SIZE]

See lota_ac_compute_expected_runtime_measure(). Also bound into the nonce.

struct lota_ac_info *info

Session and attestation details.

info is LOTA_AC_STATE_TRUSTED only if the signed status word carries LOTA_FLAG_ATTESTED. A server wanting more reads info->lota_flags, the same word.

Return

0 on success, a LOTA_AC_ERR_* code on failure.

int lota_ac_verify_boot_evidence(const struct lota_ac_info *info, const uint8_t *evidence, size_t len, int *secure_boot)

Read Secure Boot from boot evidence.

Parameters

const struct lota_ac_info *info

From lota_ac_verify_heartbeat() on the heartbeat the evidence came with; the evidence counts only for the boot that quote signed.

const uint8_t *evidence

From lota_ac_boot_evidence().

size_t len

Size of evidence.

int *secure_boot

1 if the firmware measured Secure Boot on before the boot loader, else 0.

Return

0 on success, LOTA_AC_ERR_BAD_EVIDENCE for evidence malformed or of another boot, another LOTA_AC_ERR_* code on failure.