Skip to content

Commit 544d08f

Browse files
ebiggerstytso
authored andcommitted
fscrypt: use a common logging function
Use a common function for fscrypt warning and error messages so that all the messages are consistently ratelimited, include the "fscrypt:" prefix, and include the filesystem name if applicable. Also fix up a few of the log messages to be more descriptive. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 11b8818 commit 544d08f

5 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎fs/crypto/crypto.c‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw,
174174
res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
175175
skcipher_request_free(req);
176176
if (res) {
177-
printk_ratelimited(KERN_ERR
178-
"%s: crypto_skcipher_encrypt() returned %d\n",
179-
__func__, res);
177+
fscrypt_err(inode->i_sb,
178+
"%scryption failed for inode %lu, block %llu: %d",
179+
(rw == FS_DECRYPT ? "de" : "en"),
180+
inode->i_ino, lblk_num, res);
180181
return res;
181182
}
182183
return 0;
@@ -416,6 +417,27 @@ int fscrypt_initialize(unsigned int cop_flags)
416417
return res;
417418
}
418419

420+
void fscrypt_msg(struct super_block *sb, const char *level,
421+
const char *fmt, ...)
422+
{
423+
static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
424+
DEFAULT_RATELIMIT_BURST);
425+
struct va_format vaf;
426+
va_list args;
427+
428+
if (!__ratelimit(&rs))
429+
return;
430+
431+
va_start(args, fmt);
432+
vaf.fmt = fmt;
433+
vaf.va = &args;
434+
if (sb)
435+
printk("%sfscrypt (%s): %pV\n", level, sb->s_id, &vaf);
436+
else
437+
printk("%sfscrypt: %pV\n", level, &vaf);
438+
va_end(args);
439+
}
440+
419441
/**
420442
* fscrypt_init() - Set up for fs encryption.
421443
*/

‎fs/crypto/fname.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname,
7171
res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
7272
skcipher_request_free(req);
7373
if (res < 0) {
74-
printk_ratelimited(KERN_ERR
75-
"%s: Error (error code %d)\n", __func__, res);
74+
fscrypt_err(inode->i_sb,
75+
"Filename encryption failed for inode %lu: %d",
76+
inode->i_ino, res);
7677
return res;
7778
}
7879

@@ -115,8 +116,9 @@ static int fname_decrypt(struct inode *inode,
115116
res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
116117
skcipher_request_free(req);
117118
if (res < 0) {
118-
printk_ratelimited(KERN_ERR
119-
"%s: Error (error code %d)\n", __func__, res);
119+
fscrypt_err(inode->i_sb,
120+
"Filename decryption failed for inode %lu: %d",
121+
inode->i_ino, res);
120122
return res;
121123
}
122124

‎fs/crypto/fscrypt_private.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
100100
gfp_t gfp_flags);
101101
extern const struct dentry_operations fscrypt_d_ops;
102102

103+
extern void __printf(3, 4) __cold
104+
fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...);
105+
106+
#define fscrypt_warn(sb, fmt, ...) \
107+
fscrypt_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
108+
#define fscrypt_err(sb, fmt, ...) \
109+
fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
110+
103111
/* fname.c */
104112
extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
105113
u8 *out, unsigned int olen);

‎fs/crypto/hooks.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ int fscrypt_file_open(struct inode *inode, struct file *filp)
3939
dir = dget_parent(file_dentry(filp));
4040
if (IS_ENCRYPTED(d_inode(dir)) &&
4141
!fscrypt_has_permitted_context(d_inode(dir), inode)) {
42-
pr_warn_ratelimited("fscrypt: inconsistent encryption contexts: %lu/%lu",
43-
d_inode(dir)->i_ino, inode->i_ino);
42+
fscrypt_warn(inode->i_sb,
43+
"inconsistent encryption contexts: %lu/%lu",
44+
d_inode(dir)->i_ino, inode->i_ino);
4445
err = -EPERM;
4546
}
4647
dput(dir);

‎fs/crypto/keyinfo.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
103103

104104
if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
105105
|| master_key->size % AES_BLOCK_SIZE != 0) {
106-
printk_once(KERN_WARNING
107-
"%s: key size incorrect: %d\n",
108-
__func__, master_key->size);
106+
fscrypt_warn(NULL, "key size incorrect: %u",
107+
master_key->size);
109108
res = -ENOKEY;
110109
goto out;
111110
}
@@ -132,9 +131,10 @@ static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
132131
u32 mode;
133132

134133
if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
135-
pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
136-
inode->i_ino,
137-
ci->ci_data_mode, ci->ci_filename_mode);
134+
fscrypt_warn(inode->i_sb,
135+
"inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
136+
inode->i_ino, ci->ci_data_mode,
137+
ci->ci_filename_mode);
138138
return -EINVAL;
139139
}
140140

@@ -173,8 +173,9 @@ static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
173173

174174
tfm = crypto_alloc_shash("sha256", 0, 0);
175175
if (IS_ERR(tfm)) {
176-
pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
177-
PTR_ERR(tfm));
176+
fscrypt_warn(NULL,
177+
"error allocating SHA-256 transform: %ld",
178+
PTR_ERR(tfm));
178179
return PTR_ERR(tfm);
179180
}
180181
prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
@@ -309,8 +310,9 @@ int fscrypt_get_encryption_info(struct inode *inode)
309310
ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
310311
if (IS_ERR(ctfm)) {
311312
res = PTR_ERR(ctfm);
312-
pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
313-
__func__, res, inode->i_ino);
313+
fscrypt_warn(inode->i_sb,
314+
"error allocating '%s' transform for inode %lu: %d",
315+
cipher_str, inode->i_ino, res);
314316
goto out;
315317
}
316318
crypt_info->ci_ctfm = ctfm;
@@ -327,8 +329,9 @@ int fscrypt_get_encryption_info(struct inode *inode)
327329
crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
328330
res = init_essiv_generator(crypt_info, raw_key, keysize);
329331
if (res) {
330-
pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
331-
__func__, res, inode->i_ino);
332+
fscrypt_warn(inode->i_sb,
333+
"error initializing ESSIV generator for inode %lu: %d",
334+
inode->i_ino, res);
332335
goto out;
333336
}
334337
}

0 commit comments

Comments
 (0)