docs: introduce a top-level reference for AI agents
PATCH 1/1 docs: introduce a top-level reference for AI agents
Provide some guidance to AI agents about how to implement changes to Xen. I expect to use this as a baseline that will be further adjusted as we gain more experience about how AI agents interact with Xen.
Signed-off-by Roger Pau Monné <[email protected]>
--- AGENTS.md | 456 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 456 insertions(+) create mode 100644 AGENTS.md
AGENTS.md new file +456 -0
@@ -0,0 +1,456 @@ +# Contributing to Xen with an AI assistant + +This file is for AI assistants (coding agents, review bots, chat models) +that work on the Xen hypervisor tree on behalf of a human contributor. It +says how a change to Xen has to look, how a patch series is structured, and +what to do when you find something that may be a security issue. + +It does not replace the project's own documents. Read them when the task +touches what they cover: + +- `docs/process/sending-patches.pandoc`: subject, commit message, tags, + recipients. +- `CODING_STYLE`: the style of `xen/` and the low-level libraries. +- `docs/process/coding-best-practices.pandoc`: error handling, and when + to use `ASSERT()`, `BUG_ON()` or `domain_crash()`. +- `docs/misra/rules.rst` and `docs/misra/deviations.rst`: the MISRA C + rules Xen follows. +- `MAINTAINERS`: who must approve what, and the check-in policy. +- `CONTRIBUTING`: licences and the Developer's Certificate of Origin. +- `SUPPORT.md`: which features are supported and security supported. + +Where this file and one of those disagree, that file wins. + +## 1. Three rules that come before everything else + +1. **A possible security issue goes to `[email protected]` first, + and nowhere else.** Not to xen-devel, not to an issue tracker, not into + a commit message, not onto a public branch or CI pipeline. Section 5 + says how to recognise one and what to do. If in doubt, treat it as one. +2. **The human is the author and the sender.** You prepare; they review, + sign off and send. Never add a `Signed-off-by:` tag: only a natural + person can certify the Developer's Certificate of Origin. Never send + mail, push, or post on someone's behalf unless they told you to in this + session. +3. **Say what you did and did not verify.** "Builds on x86_64 and arm64, + not booted" is useful. "Tested" without saying how is not. Do not + invent test results, review tags, or reasons. + +## 2. Before you change anything + +- Work against the current `staging` branch of xen.git unless told + otherwise. `git log` on the files you will touch shows the local habits + for subjects, prefixes and the size of a typical patch. +- Find out who maintains the code: `./scripts/get_maintainer.pl -f <file>` + or the `F:` lines in `MAINTAINERS`. A change that spans maintainer areas + needs an ack from each, which is a reason to split it (section 4). +- Find out which builds contain the code. Much of Xen is conditional on + the architecture and on Kconfig. A function may be compiled for x86 only, + for HVM only, or not at all in the default configuration. Check the + `Makefile` in the directory and the `#ifdef`s around the code before you + reason about who can reach it. +- Read the callers. Xen code relies heavily on invariants established + elsewhere: a lock taken by the caller, a bound checked at the hypercall + entry point, a reference held across a call. A change or a finding that + ignores them is wrong more often than not. + +## 3. Making a patch + +### 3.1 One logical change per patch + +A patch does one thing that can be described in a subject line. Do not mix +a functional change with clean-up, renaming, re-indentation or code +movement. If you need a refactoring first, that is its own patch, and its +commit message says "No functional change" (and that must be true). + +Keep the diff minimal. Do not reformat lines you are not changing, do not +"fix" style in the neighbourhood, do not rename things in passing. +Reviewers read every line, and unrelated lines cost them time and hide the +real change. + +### 3.2 Code + +Follow `CODING_STYLE` for everything under `xen/`. The points that differ +from Linux and are most often got wrong: + +- Four spaces per indent level, no tabs. +- Spaces inside the parentheses of control statements: + `if ( (a & b) == c )`, `for ( i = 0; i < n; i++ )`, `while ( cond )`. +- Braces on their own line, except `do {` ... `} while ( cond );`. No + braces around a single statement. +- Lines under 80 characters. Do not split user-visible strings such as + `printk()` messages. +- Only `/* ... */` comments. A multi-line comment has the opening and + closing markers on their own lines and a leading `*` on each line. +- Prefer basic C types and the standard typedefs. Use fixed-width types + (`uint32_t`) only for quantities that really have a fixed width, and do + not introduce new uses of `u8`, `u16`, `u32`, `u64`. +- Qualify pointed-to objects `const` whenever they are not modified. +- New files start with an SPDX line. The licence follows the directory; + `xen/include/public/` is MIT, most of the rest is GPL-2.0-only. + +Files imported from other projects (many came from Linux) keep the style of +their origin. Copy the style of the file you are in. + +Beyond style: + +- **MISRA C.** `docs/misra/rules.rst` lists the rules Xen has adopted, and + CI checks them. Do not add new violations. If a deviation is really + needed it must be documented the way `docs/misra/` describes, and the + commit message says why. +- **Error handling.** Follow `docs/process/coding-best-practices.pandoc`. + Return an error when the caller can handle it. Use `domain_crash()` when + the guest misbehaves and Xen's own state can be kept consistent, and + remember that `domain_crash()` returns: the code after it still runs. + Use `BUG_ON()` only when continuing could leak information or escalate + privilege. `ASSERT()` is not error handling: it is compiled out of + release builds, so the code after it must be safe when the condition is + false. +- **Guest-controlled input.** Anything a guest supplies is hostile: + hypercall arguments, values read from guest memory, emulated register + writes, shared-ring contents. Bound it before use, read guest memory + once and work on the copy, check the result of every copy from or to the + guest, and use `array_index_nospec()` after a bounds check on a + guest-controlled array index where the surrounding code does so. +- **Long-running operations.** A hypercall that loops over a + guest-controlled count needs a preemption check and a continuation, as + its neighbours do. +- **Public interfaces.** `xen/include/public/` is the ABI towards guests + and toolstacks. Do not change the layout or meaning of an existing + stable structure. For `domctl` and `sysctl` changes, check whether the + interface version needs bumping. +- **Documentation that goes with code.** A new or changed command line + option is documented in `docs/misc/xen-command-line.pandoc`. A new + feature or a change of support status goes into `SUPPORT.md`. A change + users will notice gets a line in `CHANGELOG.md`. + +### 3.3 Build and test + +Build what you changed, in the configurations it is compiled in: + +- both `x86_64` and `arm64` for common code, and any other architecture + the file is built for; +- a debug and a non-debug build (`CONFIG_DEBUG`), because `ASSERT()`s and + some code paths exist in only one of them; +- with the feature's Kconfig option on and off, if there is one. + +`automation/scripts/containerize` runs a build in the containers CI uses, +which is the easiest way to get the cross toolchains. + +State in your summary to the human exactly which builds and tests ran. If +you could not build or boot something, say so; do not present an unbuilt +change as finished. + +### 3.4 Commit message + +The format is defined in `docs/process/sending-patches.pandoc`. In short: + + <component>: <what the patch does> + + <Why the change is needed and how it is achieved: the current + situation, what is wrong or missing, what the patch does about it.> + + Fixes: 0123456789ab ("subject of the commit that introduced the bug") + Assisted-by: <agent name>:<model version> + Signed-off-by: <added by the human> + +- **Subject:** a component prefix as used by recent commits to the same + files (`x86/hvm:`, `xen/arm:`, `tools/libxl:`, `docs:`), then a short + description of *what* is done. At most about 80 characters, followed by + a blank line. +- **Body:** explain *why*, then *how*. Describe the behaviour, not the + diff. Mention every additional change the patch makes. Wrap at 75 + columns, except for pasted error output. Refer to other commits as + `commit 0123456789ab ("subject")` with at least 12 hex digits. +- **`Fixes:`** when the patch fixes a bug introduced by a specific commit: + 12-digit id and the full subject, on one line. Find the commit with + `git log -S` or `git blame`; do not guess it. **`Amends:`** when it + completes a commit without fixing a bug in it. +- **`Assisted-by:`** is how Xen records AI involvement. The format is + `Assisted-by: AGENT_NAME:MODEL_VERSION`, optionally followed by + `# <what the tool was used for>`. It refers to the next tag that names a + person, so it goes directly before the author's `Signed-off-by:` when + you helped write the patch, and before `Reported-by:` when you helped + find the issue. Add it yourself; leave `Signed-off-by:` to the human. +- **Other tags** (`Reported-by:`, `Suggested-by:`, `Reviewed-by:`, + `Acked-by:`, `Tested-by:`, `Backport:`) are in chronological order and + never split across lines. Only add a tag that was really given. A + `Reviewed-by:` or `Acked-by:` from an earlier version may be kept only + if the part it covers did not change in a meaningful way. + +Do not write in the commit message that a tool "found" or "suggested" +something as a justification. The message has to stand on the technical +argument alone. + +## 4. Structuring a patch series + +A series is a sequence of patches that a reviewer can read in order and a +committer can apply in part. + +- **Every patch builds and works on its own.** The tree must compile and + behave correctly after each patch, on every architecture, so that + `git bisect` stays usable. Never introduce a function in one patch and + fix its bug in a later one. +- **Order by dependency and by risk.** Bug fixes first, so they can be + committed and backported without the rest. Then preparatory patches + that do not change behaviour (moving code, adding a helper, widening a + type). Then the functional changes, smallest and least controversial + first. Documentation and `CHANGELOG.md` go with the patch that makes + the change, or last. +- **Split along maintainer boundaries where it is natural.** A patch that + touches `xen/arch/x86/` and `xen/arch/arm/` needs both sets of + maintainers. Introduce the common part first, then convert one + architecture per patch. +- **Keep patches small enough to review.** If a reviewer cannot hold the + whole patch in their head, split it. A mechanical change over many + files may be large if it is uniform and the message says how it was + produced, so that it can be reproduced. +- **Cover letter.** A series of more than one patch has a cover letter + (`git format-patch --cover-letter`) saying what the series achieves, + why, how the patches depend on each other, what was tested, and which + patches are independent and could go in early. It is not a list of the + subjects: git generates that. +- **Versions.** A resend is `[PATCH v2 n/m]` (`git format-patch -v2`). + Each patch carries, below the `---` line where it is not part of the + commit message, what changed since the previous version and at whose + request: + + --- + Changes in v2: + - Use array_index_nospec() as requested by Jane Doe. + - Split the header move into its own patch. + + A patch that did not change says so. Address every review comment, + either in the code or by answering it; do not silently skip one. +- **Recipients.** The series goes **to** `[email protected]` + with the maintainers and designated reviewers of every touched file in + **Cc**. `./scripts/add_maintainers.pl --patchdir <dir>` adds them to + patches generated by `git format-patch`. Patches are sent inline with + `git send-email`, not as attachments, pull requests or links. Sending + is the human's step. + +Changes are committed only with the approval of a maintainer of each +touched file and a review by someone other than the author (`MAINTAINERS`, +"Check-in policy"). Expect several rounds, and expect requests about names +and formatting; they are part of keeping the code base uniform. + +## 5. Security issues + +Xen is the isolation boundary between virtual machines. A defect that lets +one guest read, corrupt, crash or stall the hypervisor or another guest +affects every user of the affected versions, and many of them cannot +update at once. The project therefore fixes such defects under embargo: +the Xen Security Team is told first, a fix and an advisory (XSA) are +prepared in private, downstream users are given time to update, and only +then is anything published. + +**If you find, or think you may have found, a security issue in Xen:** + +1. **Stop.** Do not continue towards a public patch for it. +2. **Tell the human you are working for, in private,** what you found and + that it must go to `[email protected]` before anyone else. +3. **Do not disclose it anywhere else.** No mail to xen-devel or any other + list, no issue or merge request, no public chat, no commit pushed to a + public repository or run through a public CI pipeline, no hint in a + commit message or code comment ("fix guest-triggerable overflow"), no + pasting of the details into another online service. +4. **Prepare the report** (section 5.4) for the human to send to + `[email protected]`. The security team will reply, agree a + disclosure date with the discoverer, and coordinate the fix. The + discoverer is credited in the advisory unless they ask not to be. +5. **Keep it private until the security team says otherwise.** A fix is + posted publicly only when the embargo ends, or when the security team + decides the issue can be handled in public. + +This applies even when you are not sure. The security team would rather +see a report that turns out to be an ordinary bug than read about a +vulnerability on a public list. A public post cannot be taken back. + +### 5.1 Is it a security issue? Decide in this order + +**Step 1 - Who can trigger it?** Work out the least privileged party that +can reach the faulty code with the inputs it needs. + +It is a security issue when that party is one of: + +- an unprivileged guest: the kernel of a PV, HVM or PVH guest on x86, or + of an Arm guest, through hypercalls, emulated instructions and devices, + MMIO and port I/O, MSRs and system registers, grant tables, event + channels, shared memory, or several vCPUs racing each other; +- a user process inside a guest attacking its own kernel through Xen; +- a guest attacking another guest; +- a driver domain or a passed-through device, within the limits + `SUPPORT.md` states for PCI passthrough. + +It is **not** a security issue, and is handled as an ordinary bug in +public, when triggering it needs one of: + +- the control domain (dom0), the hardware domain or the host + administrator: they are trusted; +- boot-time input: the command line, firmware tables, the device tree, + boot modules; +- **the privileges of a device-model or stub domain over its target + domain.** A stub domain is considered secure until it has been + compromised, so the attacker would first have to gain control of it. + Fixing such a defect is defence in depth: worth doing, in public, as a + bug; +- **a debug or verbose option being in use**, such as `iommu=verbose` or + `iommu=debug`, or a build with `EXPERT` or `DEBUG` Kconfig options + (`SUPPORT.md`: these are not security supported). A guest-reachable + `ASSERT()` that only fires in a debug build falls here; a missing check + that an `ASSERT()` papers over and that corrupts memory in a release + build does not; +- **a debug key handler.** Code that runs only from a debug key is not + security supported. + +**Step 2 - Is the feature security supported?** Look the feature up in +`SUPPORT.md`. "Supported" is covered. "Experimental", "Tech Preview" and +"Supported, not security supported" are not, and bugs there are posted +publicly. The development branch itself is not yet security supported: a +defect that was introduced after the last release and exists in no +released version is fixed in public. Check which releases contain the +faulty code (`git tag --contains <commit>`, or read the stable branches) +before you conclude that. + +**Step 3 - What is the effect?** It is a security issue when the effect +crosses a privilege boundary: + +- memory corruption or privilege escalation in the hypervisor; +- disclosure of hypervisor or other-domain data, including uninitialised + stack, heap or padding copied to a guest; +- denial of service of the host or of another domain: a crash, a + reachable `BUG()`, `panic()` or fatal fault, a deadlock, a livelock, an + unbounded loop without preemption, exhaustion of a shared resource; +- a guest user process gaining rights over its own kernel. + +An effect that stays inside the domain that caused it (a guest that can +crash or confuse only itself) is a bug, not a security issue. + +If steps 1 to 3 all say yes, or you cannot rule one of them out, treat it +as a security issue. + +### 5.2 Where such defects are found + +When you write or review Xen code, these are the patterns that have led to +vulnerabilities: + +- a guest-controlled value used without a bounds or range check, or with + one that can be bypassed by overflow, truncation or a signedness mix-up + (counts, sizes, offsets, frame numbers, 32-bit versus 64-bit layouts, + compat versus native structures), and off-by-one errors; +- guest memory read twice with the check on the first read and the use on + the second, and unchecked results of copies from or to the guest; +- reference counting and lifetime: a page or domain reference dropped + twice or not taken, a type count that goes wrong on an error path, a + page freed while a mapping of it remains, use after free; +- stale translations: a mapping or permission change, or a page being + freed or changing type, without the TLB or IOMMU flush that must come + before the page is reused; +- locking: a missing lock, two locks taken in opposite orders on paths a + guest can drive, state used after the lock that protects it was + dropped, races with domain destruction or vCPU state changes; +- error paths that leave state half updated, and hypercall continuations + that re-read state which changed in between; +- `domain_crash()` treated as if it did not return; +- an `ASSERT()` as the only guard in front of a memory access; +- data returned to a guest from a structure that was not fully + initialised; +- a missing or misplaced permission (XSM) check; +- a guest-controlled array index reaching a load without + `array_index_nospec()` where the surrounding code follows that + convention; +- messages a guest can cause without rate limiting, when they are printed + in a context that stalls the host. + +### 5.3 Verify before you report + +A report that is wrong costs the security team time; a report that is +right but vague costs more. Before you write one: + +- **Trace the whole path.** From the entry point the attacker controls + (the hypercall, the trapped instruction, the MMIO handler) through every + check on the way to the faulty operation, with file and line for each + step. Read the callers and callees; do not infer what they do from + their names. +- **Look for the guard you missed.** The most common mistake is a check, + lock or reference that exists one or two levels up the call chain, or a + set-up step at boot that makes the bad state impossible. Search for it + actively before you conclude it is absent. +- **Check that the code is built and reachable.** Confirm the file is + compiled for the architecture, the code is not under a disabled + `#ifdef` or Kconfig option, and the attacker's domain type can reach + the entry point. +- **Check the attacker's privilege again.** Many interfaces look + guest-reachable and are restricted to the control domain or to a device + model by an XSM check at the top of the handler. +- **For a race, say what you established.** That two paths can interleave + in principle is one claim; that the window can be hit is another. State + which of the two you have shown. +- **Do not rely on assumed hardware behaviour.** If the conclusion + depends on what a CPU or device does in a corner case, say that it does + and that you did not confirm it. +- **Check whether it is known.** Search the advisories at + `https://xenbits.xen.org/xsa/` and the history of the file. If it is + already fixed in `staging` or in a stable branch, name the commit. +- **Find the affected versions.** Identify the commit that introduced the + defect and which releases contain it. + +Do not try a suspected vulnerability on a system you are not explicitly +allowed to test on, and do not build a working exploit unless the human +asks for a proof of concept to accompany the report. + +### 5.4 What the report contains + +Write it as plain text for the human to send to +`[email protected]`: + +- a one-line summary of the defect and its effect; +- who can trigger it and what they need (guest type, number of vCPUs, a + passed-through device, a particular configuration or command line); +- the affected code, with file, function and line at a named commit, and + the path from the entry point to the fault; +- the impact, and what limits it; +- the affected versions and architectures, and the commit that introduced + it if known; +- how far it was verified: what was read, what was run, what remains an + assumption. Say plainly if nothing was run; +- a proposed fix as a patch, if you have one, formatted as in section 3. + The security team may use it, change it, or write another; +- that an AI assistant was involved in finding it, and which one, so that + credit and `Assisted-by:` can be handled correctly. + +Keep the report, any patch and any notes on local storage only until the +embargo ends. + +### 5.5 Bugs that are not security issues + +Report and fix them in the open, on xen-devel, in the normal way. When a +bug was looked at for security impact and found to have none, say why in +the commit message: who can trigger it and what limits the effect, for +example "only reachable by the control domain" or "requires +iommu=debug". That saves the reviewers the same analysis. Defence in depth +fixes are welcome; describe them as such. + +If you fix an ordinary bug and notice while doing so that it is reachable +by a guest after all, go back to the start of section 5: stop, and report +privately. + +## 6. Checklist before you hand a change to the human + +- [ ] Not a security issue, or already cleared by the security team. +- [ ] Based on current `staging`; applies cleanly. +- [ ] One logical change per patch; no unrelated hunks. +- [ ] `CODING_STYLE` followed; no new MISRA violations. +- [ ] Builds on every architecture and configuration the code is in, + debug and non-debug; the tree builds after each patch of a series. +- [ ] Documentation, `SUPPORT.md` and `CHANGELOG.md` updated where the + change calls for it. +- [ ] Subject has the right component prefix; the message says why and + how; lines wrap at 75 columns. +- [ ] `Fixes:` names the right commit with 12 digits and its subject. +- [ ] `Assisted-by:` present; no `Signed-off-by:` added by you; no tag + that was not given. +- [ ] Cover letter and per-patch change log for a series or a resend. +- [ ] Maintainers and reviewers identified with `get_maintainer.pl`. +- [ ] A summary for the human of what was built, what was tested, and + what was not.
Signature · 1 line
2.55.0
Quoted 5 lines · Roger Pau Monne
Provide some guidance to AI agents about how to implement changes to Xen. I expect to use this as a baseline that will be further adjusted as we gain more experience about how AI agents interact with Xen.
Signed-off-by Roger Pau Monné <[email protected]>
I just want to say that I read it and I like it. It seems helpful even for humans. It does not need to be perfect right away as you mentioned. On that basis:
Acked-by Michal Orzel <[email protected]>
~Michal