Skip to content

Commit cefa9bb

Browse files
committed
client: Rework fscrypt set policy to match fscrypt specification
When an fscrypt policy is set to an existing fscrypt directory it should only return EEXIST if policy being applied differs Signed-off-by: Christopher Hoffman <[email protected]> (cherry picked from commit 3aa8819)
1 parent b7ab1bc commit cefa9bb

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/client/Client.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18140,7 +18140,10 @@ int Client::set_fscrypt_policy_v2(int fd, const struct fscrypt_policy_v2& policy
1814018140
int Client::ll_set_fscrypt_policy_v2(Inode *in, const struct fscrypt_policy_v2& policy)
1814118141
{
1814218142
if (in->fscrypt_auth.size() > 0) {
18143-
return -EEXIST;
18143+
struct fscrypt_policy_v2 policy2;
18144+
in->fscrypt_ctx->convert_to(&policy2);
18145+
if (memcmp(&policy, &policy2, sizeof(policy)))
18146+
return -EEXIST;
1814418147
}
1814518148

1814618149
if (!in->is_dir())

src/test/libcephfs/fscrypt.cc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void populate_policy(struct ceph_fscrypt_key_identifier kid, struct fscrypt_poli
102102
policy->contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
103103
policy->filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
104104
policy->flags = FSCRYPT_POLICY_FLAGS_PAD_32;
105+
memset(policy->__reserved, 0, sizeof(policy->__reserved));
105106
memcpy(policy->master_key_identifier, kid.raw, FSCRYPT_KEY_IDENTIFIER_SIZE);
106107
}
107108

@@ -401,7 +402,7 @@ TEST(FSCrypt, SetPolicyNotEmptyDir) {
401402
ceph_shutdown(cmount);
402403
}
403404

404-
TEST(FSCrypt, SetPolicyAlreadyExistSameKey) {
405+
TEST(FSCrypt, SetPolicyAlreadyExistSamePolicy) {
405406
struct ceph_fscrypt_key_identifier kid;
406407

407408
struct ceph_mount_info* cmount;
@@ -422,6 +423,43 @@ TEST(FSCrypt, SetPolicyAlreadyExistSameKey) {
422423
ASSERT_EQ(0, r);
423424

424425
r = ceph_set_fscrypt_policy_v2(cmount, fd, &policy);
426+
ASSERT_EQ(0, r);
427+
428+
ceph_rmdir(cmount, dir_path.c_str());
429+
ceph_shutdown(cmount);
430+
}
431+
432+
TEST(FSCrypt, SetPolicyAlreadyExistDifferentPolicy) {
433+
struct ceph_fscrypt_key_identifier kid;
434+
435+
struct ceph_mount_info* cmount;
436+
int r = init_mount(&cmount);
437+
ASSERT_EQ(0, r);
438+
439+
string dir_path = "dir2";
440+
ceph_mkdir(cmount, dir_path.c_str(), 0777);
441+
442+
int fd = ceph_open(cmount, dir_path.c_str(), O_DIRECTORY, 0);
443+
444+
r = ceph_add_fscrypt_key(cmount, fscrypt_key, sizeof(fscrypt_key), &kid, 1299);
445+
446+
struct fscrypt_policy_v2 policy;
447+
populate_policy(kid, &policy);
448+
449+
r = ceph_set_fscrypt_policy_v2(cmount, fd, &policy);
450+
ASSERT_EQ(0, r);
451+
452+
char fscrypt_key2[32];
453+
for (int i = 0; i < (int)sizeof(fscrypt_key2); ++i) {
454+
fscrypt_key2[i] = (char)rand();
455+
}
456+
struct ceph_fscrypt_key_identifier kid2;
457+
r = ceph_add_fscrypt_key(cmount, fscrypt_key2, sizeof(fscrypt_key2), &kid2, 1299);
458+
459+
struct fscrypt_policy_v2 policy2;
460+
populate_policy(kid2, &policy2);
461+
462+
r = ceph_set_fscrypt_policy_v2(cmount, fd, &policy2);
425463
ASSERT_EQ(-EEXIST, r);
426464

427465
ceph_rmdir(cmount, dir_path.c_str());

0 commit comments

Comments
 (0)