Whamcloud - gitweb
LU-13683 lfs: return -ENOENT when invoked on non-existing file
[fs/lustre-release.git] / contrib / scripts / crypto_patches / 0004_master_keys.patch
1 Linux 5.3 (commit 22d94f493bfb) introduces s_master_keys to struct super_block.
2 As we need encryption support for older kernels, replace this with
3 new lsi_master_keys field in struct lustre_sb_info.
4
5 --- a/libcfs/libcfs/crypto/keyring.c
6 +++ b/libcfs/libcfs/crypto/keyring.c
7 @@ -82,7 +82,7 @@ static void llcrypt_key_describe(const s
8  }
9  
10  /*
11 - * Type of key in ->s_master_keys.  Each key of this type represents a master
12 + * Type of key in ->lsi_master_keys.  Each key of this type represents a master
13   * key which has been added to the filesystem.  Its payload is a
14   * 'struct llcrypt_master_key'.  The "." prefix in the key type name prevents
15   * users from adding keys of this type via the keyrings syscalls rather than via
16 @@ -127,7 +127,7 @@ static struct key_type key_type_llcrypt_
17         .describe               = llcrypt_user_key_describe,
18  };
19  
20 -/* Search ->s_master_keys or ->mk_users */
21 +/* Search ->lsi_master_keys or ->mk_users */
22  static struct key *search_llcrypt_keyring(struct key *keyring,
23                                           struct key_type *type,
24                                           const char *description)
25 @@ -196,13 +196,17 @@ static void format_mk_user_description(
26                 mk_identifier, __kuid_val(current_fsuid()));
27  }
28  
29 -/* Create ->s_master_keys if needed.  Synchronized by llcrypt_add_key_mutex. */
30 +/* Create ->lsi_master_keys if needed.  Synchronized by llcrypt_add_key_mutex. */
31  static int allocate_filesystem_keyring(struct super_block *sb)
32  {
33         char description[LLCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
34         struct key *keyring;
35 +       struct lustre_sb_info *lsi = s2lsi(sb);
36  
37 -       if (sb->s_master_keys)
38 +       if (!lsi)
39 +               return -EINVAL;
40 +
41 +       if (lsi->lsi_master_keys)
42                 return 0;
43  
44         format_fs_keyring_description(description, sb);
45 @@ -214,18 +218,22 @@ static int allocate_filesystem_keyring(s
46                 return PTR_ERR(keyring);
47  
48         /* Pairs with READ_ONCE() in llcrypt_find_master_key() */
49 -       smp_store_release(&sb->s_master_keys, keyring);
50 +       smp_store_release(&lsi->lsi_master_keys, keyring);
51         return 0;
52  }
53  
54  void llcrypt_sb_free(struct super_block *sb)
55  {
56 -       key_put(sb->s_master_keys);
57 -       sb->s_master_keys = NULL;
58 +       struct lustre_sb_info *lsi = s2lsi(sb);
59 +
60 +       if (lsi != NULL) {
61 +               key_put(lsi->lsi_master_keys);
62 +               lsi->lsi_master_keys = NULL;
63 +       }
64  }
65  
66  /*
67 - * Find the specified master key in ->s_master_keys.
68 + * Find the specified master key in ->lsi_master_keys.
69   * Returns ERR_PTR(-ENOKEY) if not found.
70   */
71  struct key *llcrypt_find_master_key(struct super_block *sb,
72 @@ -233,9 +241,13 @@ struct key *llcrypt_find_master_key(stru
73  {
74         struct key *keyring;
75         char description[LLCRYPT_MK_DESCRIPTION_SIZE];
76 +       struct lustre_sb_info *lsi = s2lsi(sb);
77 +
78 +       if (!lsi)
79 +               return ERR_PTR(-EINVAL);
80  
81         /* pairs with smp_store_release() in allocate_filesystem_keyring() */
82 -       keyring = READ_ONCE(sb->s_master_keys);
83 +       keyring = READ_ONCE(lsi->lsi_master_keys);
84         if (keyring == NULL)
85                 return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
86  
87 @@ -432,8 +444,12 @@ static int add_master_key(struct super_b
88  {
89         static DEFINE_MUTEX(llcrypt_add_key_mutex);
90         struct key *key;
91 +       struct lustre_sb_info *lsi = s2lsi(sb);
92         int err;
93  
94 +       if (!lsi)
95 +               return -EINVAL;
96 +
97         mutex_lock(&llcrypt_add_key_mutex); /* serialize find + link */
98  retry:
99         key = llcrypt_find_master_key(sb, mk_spec);
100 @@ -441,14 +457,15 @@ retry:
101                 err = PTR_ERR(key);
102                 if (err != -ENOKEY)
103                         goto out_unlock;
104 -               /* Didn't find the key in ->s_master_keys.  Add it. */
105 +               /* Didn't find the key in ->lsi_master_keys.  Add it. */
106                 err = allocate_filesystem_keyring(sb);
107                 if (err)
108                         goto out_unlock;
109 -               err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
110 +               err = add_new_master_key(secret, mk_spec,
111 +                                        lsi->lsi_master_keys);
112         } else {
113                 /*
114 -                * Found the key in ->s_master_keys.  Re-add the secret if
115 +                * Found the key in ->lsi_master_keys.  Re-add the secret if
116                  * needed, and add the user to ->mk_users if needed.
117                  */
118                 down_write(&key->sem);
119 --- a/libcfs/libcfs/crypto/keysetup.c
120 +++ b/libcfs/libcfs/crypto/keysetup.c
121 @@ -326,7 +326,7 @@ static int setup_file_encryption_key(str
122                 /*
123                  * As a legacy fallback for v1 policies, search for the key in
124                  * the current task's subscribed keyrings too.  Don't move this
125 -                * to before the search of ->s_master_keys, since users
126 +                * to before the search of ->lsi_master_keys, since users
127                  * shouldn't be able to override filesystem-level keys.
128                  */
129                 return llcrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
130 @@ -406,7 +406,7 @@ static void put_crypt_info(struct llcryp
131                  *
132                  * In addition, if we're removing the last inode from a key that
133                  * already had its secret removed, invalidate the key so that it
134 -                * gets removed from ->s_master_keys.
135 +                * gets removed from ->lsi_master_keys.
136                  */
137                 spin_lock(&mk->mk_decrypted_inodes_lock);
138                 list_del(&ci->ci_master_key_link);
139 --- a/libcfs/libcfs/crypto/llcrypt_private.h
140 +++ b/libcfs/libcfs/crypto/llcrypt_private.h
141 @@ -13,6 +13,7 @@
142  
143  #include <libcfs/crypto/llcrypt.h>
144  #include <crypto/hash.h>
145 +#include <lustre_disk.h>
146  
147  #define CONST_STRLEN(str)      (sizeof(str) - 1)
148  
149 @@ -372,7 +373,7 @@ struct llcrypt_master_key {
150  
151         /*
152          * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
153 -        * Once this goes to 0, the master key is removed from ->s_master_keys.
154 +        * Once this goes to 0, the master key is removed from ->lsi_master_keys.
155          * The 'struct llcrypt_master_key' will continue to live as long as the
156          * 'struct key' whose payload it is, but we won't let this reference
157          * count rise again.