Skip to content

Commit f0a0ef4

Browse files
fix(mdm): CU-17tkuw6a8db take the BitLocker key of a host that left MDM
1 parent 48ba46d commit f0a0ef4

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎openframe/docs/windows-disk-encryption.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ stays readable after a re-enrollment, so NULL in `key_stored_at` does not mean t
3737
under upstream's conditions; any other host does not, whatever the team's setting is. Lifting never turns
3838
BitLocker off.
3939
- **The key** (`SetOrUpdateDiskEncryptionKey`): stored for a host that was ever required, also after the requirement
40-
was lifted. On a drive that was already encrypted fleetd removes the other recovery keys before it sends the new
41-
one, so a dropped key would leave none that opens the drive.
40+
was lifted or the host left MDM. On a drive that was already encrypted fleetd removes the other recovery keys before
41+
it sends the new one, so a dropped key would leave none that opens the drive. Outside MDM only a key is taken: an
42+
error is refused, as upstream refuses everything.
4243
- **The status** (`GetMDMWindowsBitLockerStatus`, `os_settings.disk_encryption` in host details): shown on Free for a
4344
required host, empty for any other. No startup PIN is ever required.
4445

@@ -55,7 +56,7 @@ on any platform: the serial can be another machine's, of the tenant or of anothe
5556
- A host that was deleted and enrolls again is a new host: it is not required, and its archived key stays under the old
5657
host and is not found. Requiring the new host makes fleetd send a new key.
5758
- fleetd sends again a key that Fleet did not take only while the host is told to encrypt. A key that failed to
58-
reach Fleet is lost if the requirement is lifted before the next try.
59+
reach Fleet is lost if the requirement is lifted, or the host leaves MDM, before the next try.
5960
- The result is written after the key, not with it. Its write is retried on a deadlock or a lock timeout; if it
6061
fails, `key_stored_at` stays as it was until the next key.
6162
- Reading a key needs Windows MDM to be on for the tenant (`VerifyMDMWindowsConfigured`), as upstream.

‎server/service/openframe_windows_disk_encryption.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func (svc *Service) openframeAcceptsDiskEncryptionKey(ctx context.Context, host
7979
return asked.EverRequired, nil
8080
}
8181

82+
// openframeAcceptsKeyOutsideMDM reports whether a key is stored for a host that is not in MDM: it may have left MDM while
83+
// fleetd was sending the key, and fleetd sends it again only while it is told to encrypt. Errors are not taken.
84+
func (svc *Service) openframeAcceptsKeyOutsideMDM(ctx context.Context, host *fleet.Host, encryptionKey, clientError string) (bool, error) {
85+
if encryptionKey == "" || clientError != "" ||
86+
!fleet.OpenframeWindowsDiskEncryptionEnabled(ctx) || host.FleetPlatform() != "windows" {
87+
return false, nil
88+
}
89+
return svc.openframeAcceptsDiskEncryptionKey(ctx, host)
90+
}
91+
8292
func (svc *Service) openframeRecordDiskEncryptionResult(ctx context.Context, host *fleet.Host, clientError string, keyStored bool) error {
8393
if !fleet.OpenframeWindowsDiskEncryptionEnabled(ctx) || host.FleetPlatform() != "windows" {
8494
return nil

‎server/service/openframe_windows_disk_encryption_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,76 @@ func TestOpenframeDiskEncryptionKeyResult(t *testing.T) {
217217
require.False(t, ds.OpenframeRecordDiskEncryptionResultFuncInvoked)
218218
}
219219

220+
func TestOpenframeDiskEncryptionKeyOutsideMDM(t *testing.T) {
221+
ds := new(mock.Store)
222+
cfg := config.TestConfig()
223+
cfg.MDM.WindowsWSTEPIdentityCert = "testdata/server.pem"
224+
cfg.MDM.WindowsWSTEPIdentityKey = "testdata/server.key"
225+
svc, ctx := newTestServiceWithConfig(t, ds, cfg, nil, nil)
226+
openframe := fleet.NewOpenframeModeContext(ctx)
227+
var asked fleet.OpenframeDiskEncryption
228+
var askedErr error
229+
ds.IsHostConnectedToFleetMDMFunc = func(context.Context, *fleet.Host) (bool, error) { return false, nil }
230+
ds.OpenframeHostDiskEncryptionFunc = func(context.Context, *fleet.Host) (fleet.OpenframeDiskEncryption, error) {
231+
return asked, askedErr
232+
}
233+
ds.TeamMDMConfigFunc = func(context.Context, uint) (*fleet.TeamMDM, error) {
234+
return &fleet.TeamMDM{EnableDiskEncryption: true}, nil
235+
}
236+
ds.SetOrUpdateHostDiskEncryptionKeyFunc = func(_ context.Context, _ *fleet.Host, key, clientError string, decryptable *bool) (bool, error) {
237+
require.NotEmpty(t, key)
238+
require.Empty(t, clientError)
239+
require.Equal(t, new(true), decryptable)
240+
return false, nil
241+
}
242+
ds.OpenframeRecordDiskEncryptionResultFunc = func(_ context.Context, _ *fleet.Host, clientError string, keyStored bool) error {
243+
require.Empty(t, clientError)
244+
require.True(t, keyStored)
245+
return nil
246+
}
247+
required := fleet.OpenframeDiskEncryption{Required: true, EverRequired: true}
248+
249+
for _, c := range []struct {
250+
name string
251+
ctx context.Context
252+
platform string
253+
asked fleet.OpenframeDiskEncryption
254+
key, clientError string
255+
stored bool
256+
}{
257+
{"required", openframe, "windows", required, "123456", "", true},
258+
{"lifted", openframe, "windows", fleet.OpenframeDiskEncryption{EverRequired: true}, "123456", "", true},
259+
{"never required", openframe, "windows", fleet.OpenframeDiskEncryption{}, "123456", "", false},
260+
{"error", openframe, "windows", required, "", "no TPM found", false},
261+
{"key with an error", openframe, "windows", required, "123456", "no TPM found", false},
262+
{"neither", openframe, "windows", required, "", "", false},
263+
{"another platform", openframe, "darwin", required, "123456", "", false},
264+
{"upstream", ctx, "windows", required, "123456", "", false},
265+
} {
266+
t.Run(c.name, func(t *testing.T) {
267+
asked = c.asked
268+
ds.SetOrUpdateHostDiskEncryptionKeyFuncInvoked = false
269+
ds.OpenframeRecordDiskEncryptionResultFuncInvoked = false
270+
host := &fleet.Host{ID: 1, Platform: c.platform, TeamID: new(uint(7))}
271+
272+
err := svc.SetOrUpdateDiskEncryptionKey(hostctx.NewContext(c.ctx, host), c.key, c.clientError)
273+
274+
if c.stored {
275+
require.NoError(t, err)
276+
} else {
277+
var badRequest *fleet.BadRequestError
278+
require.ErrorAs(t, err, &badRequest)
279+
}
280+
require.Equal(t, c.stored, ds.SetOrUpdateHostDiskEncryptionKeyFuncInvoked)
281+
require.Equal(t, c.stored, ds.OpenframeRecordDiskEncryptionResultFuncInvoked)
282+
})
283+
}
284+
285+
askedErr = errors.New("database is down")
286+
host := &fleet.Host{ID: 1, Platform: "windows", TeamID: new(uint(7))}
287+
require.ErrorIs(t, svc.SetOrUpdateDiskEncryptionKey(hostctx.NewContext(openframe, host), "123456", ""), askedErr)
288+
}
289+
220290
func TestOpenframeDiskEncryptionStatusOnFree(t *testing.T) {
221291
ds := new(mock.Store)
222292
svc := &Service{ds: ds, logger: slog.New(slog.DiscardHandler)}

‎server/service/orbit.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,15 @@ func (svc *Service) SetOrUpdateDiskEncryptionKey(ctx context.Context, encryption
13741374
}
13751375

13761376
if !connected {
1377-
return badRequest("host is not enrolled with fleet")
1377+
// >>> OPENFRAME(windows-disk-encryption): in OpenFrame mode a key is taken outside MDM, too, if the host was ever required to encrypt — openframe/docs/windows-disk-encryption.md
1378+
accepts, err := svc.openframeAcceptsKeyOutsideMDM(ctx, host, encryptionKey, clientError)
1379+
if err != nil {
1380+
return err
1381+
}
1382+
if !accepts {
1383+
return badRequest("host is not enrolled with fleet")
1384+
}
1385+
// <<< OPENFRAME(windows-disk-encryption)
13781386
}
13791387

13801388
// Only archive the key if disk encryption is enabled for this host (team/globally)

0 commit comments

Comments
 (0)