I want to Authorize USB/CD on Mac OS X. Now i am using DiskArbitration framework to get MountApprovalCallback in user mode. But the problem with this callback is that there is no assurance of it.
And if i'll get callback i am using CFUserNotificationReceiveResponse()
to accept password from user.
But when prompt is shown at the same time user can open DiskUtility and can mount a device
So,
- Is there any other way to get mount approval callback or to authorize a device?
- How i can handle it in kernel mode?
Thanks in advance.
In a kext, you can get an authorisation callback for file system mount callbacks via the
mpo_mount_check_mount
callback in the MAC (TrustedBSD) policy framework. You can decide whether the mount should go ahead or not in there. I suspect you won't get any information about the logged-in user from thecred
argument, as the mount syscall is probably initiated by the daemon running as root. I don't know what you're actually trying to do, so this might not be the best way to approach the problem for your specific case.Note that this is an unsupported KPI, so Apple says it might go away or break in a future release. Indeed, the policy callback function signatures frequently change between major OS X releases, so you may need to check OS X version at runtime and use different functions for different versions. You'll also need to stay up to date with any betas that Apple releases, to see if they break your code.
With that out of the way, here's how you actually use it:
com.apple.kpi.dsep
to your kext's OSBundleLibraries dictionary. (it uses darwin versioning, so use the same version as for the other com.apple.kpi.* bundles)#include <security/mac_policy.h>
in your code (it's already provided in Kernel.framework)struct mac_policy_ops
, and initialise any of the function pointer fields you're interested in, e.g.mpo_mount_check_mount
.mac_policy_register()
and save the handle it returns. You'll need to configure your policy using amac_policy_conf
struct, where you setmpc_ops
to point to your policy struct,mpc_loadtime_flags
toMPC_LOADTIME_FLAG_UNLOADOK
,mpc_name
to a reverse-DNS identifier for your kext,mpc_fullname
to a human-readable string, and zero-initialise everything else.mac_policy_unregister()
and the handle you received frommac_policy_register()
.A lot more information can be found in the header file.