aa_kernel_interface(3)
π aa_kernel_interface β bestie edition
okay so this is a page from the AppArmor manual, section 3 (that's the library-functions girlies π). It's all about aa_kernel_interface, which is an opaque object representing the AppArmor kernel interface for policy loading, replacing, and removing. Like, the little go-between that talks to your kernel about policy. Iconic behaviour, honestly.
β¨ NAME β¨
So the whole cast of characters on this page is:
aa_kernel_interfaceβ the opaque object itself: an opaque object representing the AppArmor kernel interface for policy loading, replacing, and removing. She's mysterious. She's opaque. She runs the show. πaa_kernel_interface_newβ create a newaa_kernel_interfaceobject from an optional path. New object, who dis β¨aa_kernel_interface_refβ increments the ref count of anaa_kernel_interfaceobject. Keeping her alive, no cap π«Άaa_kernel_interface_unrefβ decrements the ref count and frees theaa_kernel_interfaceobject when 0. Bye bestie πaa_kernel_interface_load_policyβ load a policy from a buffer into the kernel. Buffer to kernel, direct flight π«aa_kernel_interface_load_policy_from_fileβ load a policy from a file into the kernel. File girlies rise πΈaa_kernel_interface_load_policy_from_fdβ load a policy from a file descriptor into the kernel. fd girlies, your moment π₯aa_kernel_interface_replace_policyβ replace a policy in the kernel with a policy from a buffer. Out with the old πaa_kernel_interface_replace_policy_from_fileβ replace a policy in the kernel with a policy from a file. Glow-up via filesystem πaa_kernel_interface_replace_policy_from_fdβ replace a policy in the kernel with a policy from a file descriptor. Glow-up via fd, iykyk π¦aa_kernel_interface_remove_policyβ remove a policy from the kernel. Blocked. Unloaded. Gone πaa_kernel_interface_write_policyβ write a policy to a file descriptor. Sending it out into the world β¨
π¦ SYNOPSIS π¦
First things first, you gotta include the header and link the library. Copy these exactly, bestie:
#include <sys/apparmor.h>
typedef struct aa_kernel_interface aa_kernel_interface;
int aa_kernel_interface_new(aa_kernel_interface **kernel_interface,
aa_features *kernel_features, const char *apparmorfs);
aa_kernel_interface *aa_kernel_interface_ref(aa_kernel_interface
*kernel_interface);
void aa_kernel_interface_unref(aa_kernel_interface *kernel_interface);
int aa_kernel_interface_load_policy(aa_kernel_interface
*kernel_interface, const char *buffer, size_t size);
int aa_kernel_interface_load_policy_from_file(aa_kernel_interface
*kernel_interface, int dirfd, const char *path);
int aa_kernel_interface_load_policy_from_fd(aa_kernel_interface
*kernel_interface, int fd);
int aa_kernel_interface_replace_policy(aa_kernel_interface
*kernel_interface, const char *buffer, size_t size);
int aa_kernel_interface_replace_policy_from_file(aa_kernel_interface
*kernel_interface, int dirfd, const char *path);
int aa_kernel_interface_replace_policy_from_fd(aa_kernel_interface
*kernel_interface, int fd);
int aa_kernel_interface_remove_policy(aa_kernel_interface
*kernel_interface, const char *fqname);
int aa_kernel_interface_write_policy(int fd, const char *buffer, size_t
size);
And when you compile, link with -lapparmor. Don't forget or she literally will not link. π
π DESCRIPTION π
the object herself
The aa_kernel_interface object contains information about the AppArmor kernel interface for policy loading, replacing, and removing. She's opaque, meaning you don't peek inside, you just use her. Main character energy, but make it a struct. π
making one: aa_kernel_interface_new()
The aa_kernel_interface_new() function creates an aa_kernel_interface object based on:
- an optional
aa_featuresobject, and - an optional path to the apparmor directory of securityfs, which is typically found at
"/sys/kernel/security/apparmor/".
Deets, because details matter:
- If
kernel_featuresisNULL, then the features of the current kernel are used. - When specifying a valid
kernel_featuresobject, it must be compatible with the features of the currently running kernel. Like, compatibility check first, always π«Ά - If
apparmorfsisNULL, then the default location is used. - The allocated
kernel_interfaceobject must be freed usingaa_kernel_interface_unref(). That's not optional, bestie, that's a leak waiting to happen π
refcounting: aa_kernel_interface_ref() and aa_kernel_interface_unref()
aa_kernel_interface_ref() increments the reference count on the kernel_interface object. Keeping her alive β¨
aa_kernel_interface_unref() decrements the reference count on the kernel_interface object and releases all corresponding resources when the reference count reaches zero. And that's the free. Rest in peace, queen π
loading vs replacing (the plot twist)
The aa_kernel_interface_load() family of functions load a policy into the kernel. The operation will fail if a policy of the same name is already loaded. Rude of it, but okay.
Use the aa_kernel_interface_replace() family of functions if you wish to replace a previously loaded policy with a new policy of the same name.
And plot twist: the aa_kernel_interface_replace() functions can also be used to load a policy that does not correspond to a previously loaded policy. So replace is lowkey the flexible one. Slay π
buffers, files, and fds β the three flavours
- When loading or replacing from a buffer, the buffer will contain binary data. The
sizeargument must specify the size of thebufferargument. Don't guess the size. Actually measure. Fr fr. - When loading or replacing from a file, the
dirfdandpathcombination are used to specify the location of the file. See theopenat(2)man page for examples ofdirfdandpath. Go read it, it's giving essential context π - It is also possible to load or replace from a file descriptor specified by the
fdargument. The file must be open for reading and the file offset must be set appropriately. Open for reading = mandatory. Offset set properly = also mandatory. No skipping steps π€
unloading: aa_kernel_interface_remove_policy()
The aa_kernel_interface_remove_policy() function can be used to unload a previously loaded policy.
The fully qualified policy name must be specified with the fqname argument. Not a nickname. Not a vibe. The fully qualified name.
The operation will fail if a policy matching fqname is not found. Nothing to remove? She'll tell you. π
writing out: aa_kernel_interface_write_policy()
The aa_kernel_interface_write_policy() function allows for a policy β which is stored in buffer and consists of size bytes β to be written to a file descriptor.
The fd must be open for writing and the file offset must be set appropriately. Same energy as before: open right, offset right, no chaos. β¨
π RETURN VALUE π
aa_kernel_interface_new()returns0on success, and*kernel_interfacewill point to anaa_kernel_interfaceobject that must be freed byaa_kernel_interface_unref(). π- On error,
-1is returned, witherrnoset appropriately, and*kernel_interfacewill be set toNULL. Clean failure, we love an honest error. aa_kernel_interface_ref()returns the value ofkernel_interface. She just hands her back to you π«Ά- The
aa_kernel_interface_load()family of functions, theaa_kernel_interface_replace()family of functions,aa_kernel_interface_remove(), andaa_kernel_interface_write_policy()return0on success.-1is returned on error, witherrnoset appropriately.
π₯ ERRORS π₯
The errno value will be set according to the underlying error in the aa_kernel_interface family of functions that return -1 on error. So like, the exact error comes from whatever went wrong underneath β no invented drama, just the real code. π§
β NOTES β
- All
aa_kernel_interfacefunctions described above are present in libapparmor version 2.10 and newer. Older than that? Sorry babe. aa_kernel_interface_unref()saves the value oferrnowhen called and restoreserrnobefore exiting in libapparmor version 2.12 and newer. Yourerrnosurvives the cleanup. Respect. π
π BUGS π
None known. If you find any, please report them at <https://gitlab.com/apparmor/apparmor/-/issues>. Do your civic duty, bestie π«‘
π SEE ALSO π
aa_features(3), openat(2) and <https://wiki.apparmor.net>. Go read, go learn, go slay πβ¨
okay that's the whole page, you're officially an aa_kernel_interface girlie now π
this has been the AppArmor manual, AppArmor 4.1.8, dated 2026-10-18 β aa_kernel_interface(3), signing off π¦π
π¦ where this page comes from
- arch rolling: apparmor 4.1.8-2 Β· GPL-2.0-only AND LGPL-2.0-only AND LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms