Skip to content

Commit bc97e8b

Browse files
feat(mdm): CU-17tkuw6a8c2 lock Windows hosts with BitLocker on Fleet Free
1 parent c055dde commit bc97e8b

17 files changed

Lines changed: 1107 additions & 1 deletion

‎openframe/docs/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The agent has its own switch, `--openframe-mode` / `ORBIT_OPENFRAME_MODE`.
4848
| [enroll-machine-identity.md](enroll-machine-identity.md) | One Fleet host per OpenFrame machine: with `FLEET_OPENFRAME_MACHINE_IDENTITY` on, agents enroll with the OpenFrame machine id and move their host to it with the node key they held, so machines sharing a hardware UUID stop overwriting each other; a second machine (another osquery instance) with the same hardware UUID refused while the owner is live, takeover log for alerting, MDM refused for a UUID shared within a team. |
4949
| [windows-wipe.md](windows-wipe.md) | `POST /hosts/{id}/wipe` wipes Windows hosts on Fleet Free, over Windows MDM (RemoteWipe CSP); refused, or cancelled before it is sent, when the hardware UUID does not identify one device; `completed_wipe` activity when the device accepts it. |
5050
| [windows-disk-encryption.md](windows-disk-encryption.md) | BitLocker required per Windows host on Fleet Free (`POST`/`DELETE /disk_encryption/hosts`): the fleetd notification, the key and the status follow the host, not its team; archived keys are never looked up by serial. |
51+
| [windows-lock.md](windows-lock.md) | `POST /hosts/{id}/lock` locks Windows hosts on Fleet Free with BitLocker: forced recovery and a restart, with an optional message on the recovery screen; refused without a recovery key Fleet can decrypt and finds on the drive; the unlock script restores the TPM protector once the PC checks in after someone typed the key; only Fleet's own scripts in OpenFrame mode; `completed_lock` and `failed_lock` activities. |
5152

5253
## Agent-side (orbit / fleetd)
5354

‎openframe/docs/fork-file-manifest.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ and the heaviest standing rebase cost.
176176
| Shared-mode MDM | instance-wide MDM flags: `server/fleet/openframe.go`, `server/datastore/mysql/app_configs.go`, `server/service/mdm.go`; instance settings and instance-config reads: `cmd/fleet/serve.go`, `server/datastore/cached_mysql/cached_mysql.go`, `server/mdm/android/service/service.go`; enrollment routes: `server/service/openframe_middleware.go`; Android enrollment: `server/mdm/android/service/pubsub.go`; tenant change: `server/datastore/mysql/{hosts,openframe}.go`; Apple OTA: `server/datastore/mysql/apple_mdm.go`; Windows profile cleanup: `server/datastore/mysql/mdm.go`; per-tenant Windows MDM: `server/fleet/{app,datastore,microsoft_mdm,openframe_windows_mdm}.go`, `server/service/{openframe_windows_mdm,orbit,microsoft_mdm,hosts,appconfig,service,openframe_middleware}.go`, `server/service/osquery_utils/{queries,queries_openframe_windows_mdm}.go`, `{cmd/fleet,server/datastore/mysql}/openframe_windows_wstep.go`, `server/datastore/mysql/{microsoft_mdm,microsoft_mdm_batched,apple_mdm,openframe_windows_mdm}.go`, `migrations/openframe/20261127000001_AddTeamIdToMdmWindowsEnrollments.go`, `server/mdm/microsoft/openframe_wstep.go`, `server/mock/datastore_mock.go`; tests: `server/datastore/mysql/{app_configs,android,apple_ota,windows_mdm}_openframe_test.go`, `server/datastore/mysql/openframe_windows_{wstep,mdm}_test.go`, `server/service/{openframe_middleware,openframe_windows_mdm}_test.go`, `server/service/osquery_utils/queries_openframe_mdm_link_test.go`, `migrations/openframe/migration_test.go`, `server/fleet/openframe_windows_mdm_test.go`, `cmd/fleet/openframe_windows_wstep_test.go`, `server/mdm/android/service/pubsub_openframe_test.go` — see [mysql-multitenancy-feature.md](mysql-multitenancy-feature.md) |
177177
| Windows wipe (`openframe/docs/windows-wipe.md`) | `server/service/{scripts,mdm,microsoft_mdm,openframe_windows_mdm,openframe_windows_wipe}.go`, `server/fleet/{datastore,microsoft_mdm,openframe_windows_wipe}.go`, `server/mock/datastore_mock.go`, `server/datastore/mysql/{microsoft_mdm,hosts,openframe_windows_wipe}.go`; tests: `server/service/openframe_windows_wipe_test.go`, `server/datastore/mysql/openframe_windows_wipe_test.go` |
178178
| Windows disk encryption (`openframe/docs/windows-disk-encryption.md`) | `server/service/{orbit,hosts,handler,openframe_windows_disk_encryption}.go`, `server/fleet/{datastore,service,openframe_windows_disk_encryption}.go`, `server/mock/datastore_mock.go`, `server/mock/service/service_mock.go`, `server/datastore/mysql/{apple_mdm,disk_encryption,hosts,microsoft_mdm,openframe_windows_disk_encryption}.go`, `migrations/openframe/20261201000001_AddHostDiskEncryptionTable.go`, `schema.sql`; tests: `server/service/openframe_windows_disk_encryption_test.go`, `server/datastore/mysql/openframe_windows_disk_encryption_test.go`, `server/datastore/mysql/hosts_test.go`, `migrations/openframe/migration_test.go` |
179+
| Windows lock (`openframe/docs/windows-lock.md`) | `server/service/{scripts,orbit,openframe_windows_lock}.go`, `server/fleet/{api_scripts,datastore,service,openframe_windows_lock}.go`, `server/mock/datastore_mock.go`, `server/mock/service/service_mock.go`, `server/datastore/mysql/{scripts,openframe_windows_lock}.go`; tests: `server/service/openframe_windows_lock_test.go`, `server/datastore/mysql/openframe_windows_lock_test.go` |
179180
| Agent options kept | `cmd/fleet/serve.go` (starter-library skip under multitenancy), `server/fleet/openframe.go` (trimmed seed/fallback defaults), `server/datastore/mysql/teams_openframe_test.go`, `server/fleet/openframe_test.go` |
180181
| Agent JSON content-type | `client/orbit_client.go`, `client/device_client.go`, `orbit/cmd/fetch_cert/main.go`, `client/orbit_client_content_type_test.go` |
181182
| Orbit 401 streak across restarts | `client/orbit_client.go`, `client/orbit_reenroll_openframe.go`; tests: `client/orbit_reenroll_openframe_test.go` — see [node-key-management.md](node-key-management.md) |

‎openframe/docs/windows-lock.md‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Windows lock on Fleet Free (OpenFrame)
2+
3+
Upstream locks Windows hosts on Premium only, with a script that disables local accounts. The fork's lock on Free
4+
is a hard lock with BitLocker: the system drive is forced into recovery and the PC restarts to the BitLocker
5+
recovery screen. It starts again only with the drive's recovery key, which Fleet holds. There is no unlock command:
6+
someone types the key at the PC, and Fleet notices when the PC checks in again.
7+
8+
Only in OpenFrame mode (`FLEET_OPENFRAME_MODE`) or shared mode, and only for Windows hosts; otherwise the request goes
9+
to upstream's `LockHost` (402 on Free).
10+
11+
## Request
12+
13+
`POST /api/_version_/fleet/hosts/{id}/lock`, with an optional body `{"message": "..."}`
14+
(`server/service/scripts.go`, `server/service/openframe_windows_lock.go`). The message, up to 240 characters with no
15+
line breaks or other control characters, is set as the custom message of the recovery screen.
16+
17+
Refused:
18+
19+
- without permission to run MDM commands on the host's team (403); another tenant's host is 404;
20+
- when fleetd doesn't run scripts on the host (`scripts_enabled` not true, or no fleetd) (422);
21+
- with a pending lock, unlock or wipe, or when the host is locked (422) or wiped (409);
22+
- when Fleet has no current recovery key for the host that it can decrypt (422, "Lock needs BitLocker"). An archived
23+
key is not used: it may be of another disk. Windows MDM doesn't have to be on for the host, but the tenant's WSTEP
24+
pair has to decrypt the key.
25+
26+
The lock is queued by upstream's `LockHostViaScript` as an internal script, so the host's status is upstream's: pending
27+
lock until fleetd reports the result, locked once it reports exit code 0. `locked_host` is recorded with the request.
28+
29+
## The lock script
30+
31+
It runs on the PC as SYSTEM, in PowerShell, and on `%SystemDrive%`:
32+
33+
1. checks that BitLocker protects the drive (exit 2);
34+
2. checks that one of the drive's recovery passwords is the key Fleet holds: the script carries its SHA-256, never
35+
the key (exit 3). A locked PC that Fleet's key doesn't open could not be started again;
36+
3. refuses a drive with a TPM protector that has a PIN or a startup key, which the lock would delete and the unlock
37+
couldn't restore (exit 4);
38+
4. records under `HKLM\SOFTWARE\OpenFrame\BitLockerLock` when it locked, whether the drive had a TPM protector, and
39+
the recovery message policy it replaces;
40+
5. sets the message, if any, as the recovery message policy (`HKLM\SOFTWARE\Policies\Microsoft\FVE`,
41+
`RecoveryKeyMessageSource` = 2 and `RecoveryKeyMessage`). The message reaches the script in base64, so nothing in
42+
it runs;
43+
6. runs `manage-bde -forcerecovery`, which also deletes the drive's TPM protectors (exit 5 if it fails, with the
44+
message and the record undone);
45+
7. restarts the PC in 30 seconds, closing open apps, and exits 0, so that fleetd reports the result before it.
46+
47+
A failed lock clears the pending lock and records `failed_lock` with the exit code; a lock that succeeded records
48+
`completed_lock` (`server/fleet/openframe_windows_lock.go`).
49+
50+
## Unlock
51+
52+
When a locked Windows host checks in (orbit config) at least 15 minutes after its lock succeeded, someone has typed
53+
the recovery key. The fork then queues an internal unlock script with upstream's `unlock_ref`
54+
(`OpenframeQueueWindowsUnlock`, `server/datastore/mysql/openframe_windows_lock.go`), unless an unlock is pending, a
55+
wipe is, or an unlock was tried within the last 15 minutes. The host's row is locked while the script is queued, so
56+
concurrent check-ins queue one.
57+
58+
The script:
59+
60+
1. exits 0 if no lock is recorded on the PC;
61+
2. exits 10 if the PC hasn't restarted since the lock: the host stays locked and the unlock is tried again later;
62+
3. adds a TPM protector back if the drive had one, as `-forcerecovery` deleted it and Windows would otherwise ask for
63+
the key at every start (exit 11 if it can't);
64+
4. restores the recovery message policy and removes the record.
65+
66+
Exit code 0 makes the host unlocked, as upstream, and records `unlocked_host` with no user. A failed unlock records
67+
nothing.
68+
69+
Wipe is refused while a host is locked, by upstream's checks. Fleet can't turn MDM off for a Windows host.
70+
71+
## Scripts in OpenFrame mode
72+
73+
fleetd on Windows runs scripts only when started with `--enable-scripts`, so OpenFrame has to start it with that flag;
74+
without it the lock is refused. In
75+
OpenFrame and shared mode the fork then refuses every script that isn't Fleet's own (`openframeRefuseScript`, in
76+
upstream's `insertNewHostScriptExecution`): a user's, a batch's or a policy's script is refused (422) and not queued.
77+
Internal scripts (lock, unlock, upstream's internal cleanups) and software installs and uninstalls are not affected.
78+
79+
## Tenancy
80+
81+
The lock reads the host through the fenced `Host`, so another tenant's host is 404, and decrypts the key with the
82+
host's tenant's WSTEP pair. The unlock is queued for the host that authenticated the check-in, and its result is
83+
recorded in that request's tenant.
84+
85+
## Not verified on a real PC yet
86+
87+
- that the recovery screen shows the message set by the policy;
88+
- that fleetd always reports the lock's result within the 30 seconds before the restart. A result that is lost
89+
leaves the host pending lock.
90+
91+
## Tests
92+
93+
- `server/service/openframe_windows_lock_test.go`
94+
- `server/datastore/mysql/openframe_windows_lock_test.go` (`MYSQL_TEST=1`)

‎openframe/scripts/verify.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ rm -f vet.err
4646
# 3. Marker presence: if a merge silently dropped fork code, its OPENFRAME markers
4747
# vanish too. A slug dropping to zero is a red flag worth a human look.
4848
step "OPENFRAME marker presence (dropped-fork-code detector)"
49-
for slug in host-assignments managed-policies managed-queries redis-key-prefix redis-seed-nodes query-results-ttl osquery-host-id agent-openframe-mode agent-json-content-type agent-skip-setup-experience agent-host-info-diagnostics agent-verify-server-tls migration-race server-private-key-kms enroll-machine-identity windows-disk-encryption; do
49+
for slug in host-assignments managed-policies managed-queries redis-key-prefix redis-seed-nodes query-results-ttl osquery-host-id agent-openframe-mode agent-json-content-type agent-skip-setup-experience agent-host-info-diagnostics agent-verify-server-tls migration-race server-private-key-kms enroll-machine-identity windows-disk-encryption windows-lock; do
5050
n=$(grep -rIl "OPENFRAME($slug" --include='*.go' --include='*.yaml' --include='*.tpl' . 2>/dev/null | wc -l | tr -d ' ')
5151
if [ "$n" -gt 0 ]; then ok "$slug — present in $n file(s)"; else bad "$slug — NO markers found (fork code may have been dropped in the merge)"; fi
5252
done
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// >>> OPENFRAME(windows-lock): BitLocker hard lock of Windows hosts on Fleet Free — openframe/docs/windows-lock.md
2+
3+
package mysql
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"errors"
9+
"time"
10+
11+
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
12+
"github.com/fleetdm/fleet/v4/server/fleet"
13+
"github.com/jmoiron/sqlx"
14+
)
15+
16+
var errOpenframeOnlyInternalScripts = fleet.NewInvalidArgumentError("script",
17+
"Fleet runs only its own scripts on hosts in OpenFrame mode.")
18+
19+
func openframeWindowsLockEnabled(ctx context.Context) bool {
20+
return fleet.OpenframeWindowsLockEnabled(ctx) || openframeSharedMode(ctx)
21+
}
22+
23+
// openframeRefuseScript refuses, in OpenFrame mode, a script a user or a policy asked for: fleetd runs scripts there
24+
// for Fleet's own actions only.
25+
func openframeRefuseScript(ctx context.Context, isInternal bool) error {
26+
if isInternal || !openframeWindowsLockEnabled(ctx) {
27+
return nil
28+
}
29+
return ctxerr.Wrap(ctx, errOpenframeOnlyInternalScripts)
30+
}
31+
32+
// openframeWindowsUnlockDue selects the locked Windows host whose unlock may be queued now: its lock succeeded at least
33+
// the delay ago, nothing else is pending for it, and no unlock was tried within the delay.
34+
const openframeWindowsUnlockDue = `SELECT hma.lock_ref
35+
FROM host_mdm_actions hma
36+
JOIN host_script_results lock_result ON lock_result.execution_id = hma.lock_ref AND lock_result.host_id = hma.host_id
37+
WHERE hma.host_id = ? AND hma.fleet_platform = 'windows' AND hma.unlock_ref IS NULL AND hma.wipe_ref IS NULL
38+
AND lock_result.exit_code = 0 AND lock_result.updated_at <= CURRENT_TIMESTAMP - INTERVAL ? SECOND
39+
AND NOT EXISTS (
40+
SELECT 1 FROM host_script_results attempt
41+
JOIN script_contents sc ON sc.id = attempt.script_content_id
42+
WHERE attempt.host_id = hma.host_id AND sc.md5_checksum = UNHEX(?)
43+
AND attempt.created_at > CURRENT_TIMESTAMP - INTERVAL ? SECOND)`
44+
45+
// OpenframeQueueWindowsUnlock queues the unlock script of a locked Windows host and reports whether it did. A host that
46+
// checks in while it is locked was unlocked with its recovery key; the script confirms the PC restarted after the lock
47+
// and restores what the lock removed.
48+
func (ds *Datastore) OpenframeQueueWindowsUnlock(ctx context.Context, hostID uint, script string, delay time.Duration) (bool, error) {
49+
seconds := int64(delay / time.Second)
50+
checksum := md5ChecksumScriptContent(script)
51+
args := []any{hostID, seconds, checksum, seconds}
52+
53+
var lockRef string
54+
switch err := sqlx.GetContext(ctx, ds.reader(ctx), &lockRef, openframeWindowsUnlockDue, args...); {
55+
case errors.Is(err, sql.ErrNoRows):
56+
return false, nil
57+
case err != nil:
58+
return false, ctxerr.Wrap(ctx, err, "check whether a locked Windows host is due its unlock")
59+
}
60+
61+
var queued bool
62+
err := ds.withRetryTxx(ctx, func(tx sqlx.ExtContext) error {
63+
queued = false
64+
if err := sqlx.GetContext(ctx, tx, &lockRef, openframeWindowsUnlockDue+` FOR UPDATE OF hma`, args...); err != nil {
65+
if errors.Is(err, sql.ErrNoRows) {
66+
return nil
67+
}
68+
return ctxerr.Wrap(ctx, err, "lock the host's MDM actions")
69+
}
70+
contents, err := insertScriptContents(ctx, tx, script)
71+
if err != nil {
72+
return err
73+
}
74+
contentID, _ := contents.LastInsertId()
75+
result, err := ds.newHostScriptExecutionRequest(ctx, tx, &fleet.HostScriptRequestPayload{
76+
HostID: hostID,
77+
ScriptContents: script,
78+
ScriptContentID: uint(contentID), //nolint:gosec // dismiss G115
79+
}, true)
80+
if err != nil {
81+
return ctxerr.Wrap(ctx, err, "queue the unlock script")
82+
}
83+
if _, err := tx.ExecContext(ctx, `UPDATE host_mdm_actions SET unlock_ref = ? WHERE host_id = ? AND lock_ref = ?`,
84+
result.ExecutionID, hostID, lockRef); err != nil {
85+
return ctxerr.Wrap(ctx, err, "record the unlock script")
86+
}
87+
queued = true
88+
return nil
89+
})
90+
return queued, err
91+
}
92+
93+
// <<< OPENFRAME(windows-lock)

0 commit comments

Comments
 (0)