All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Luís Henriques" <lhenriques@suse.de>
To: Xiubo Li <xiubli@redhat.com>
Cc: Jeff Layton <jlayton@kernel.org>,
	Ilya Dryomov <idryomov@gmail.com>,
	ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH v3 4/4] ceph: replace base64url by the encoding used for mailbox names
Date: Fri, 18 Mar 2022 11:01:01 +0000	[thread overview]
Message-ID: <8735jfh6du.fsf@brahms.olymp> (raw)
In-Reply-To: <6ac92645-dede-b87c-3731-2280d59f8d8e@redhat.com> (Xiubo Li's message of "Fri, 18 Mar 2022 16:32:15 +0800")

Xiubo Li <xiubli@redhat.com> writes:

> On 3/17/22 11:45 PM, Luís Henriques wrote:
>> The base64url encoding includes the '_' character, which may cause problems
>> in snapshot names (if the name starts with '_').  Thus, use the base64
>> encoding defined for IMAP mailbox names (RFC 3501), which uses '+' and ','
>> instead of '-' and '_'.
>>
>> Signed-off-by: Luís Henriques <lhenriques@suse.de>
>> ---
>>   fs/ceph/crypto.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
>>   fs/ceph/crypto.h |  3 +++
>>   fs/ceph/dir.c    |  2 +-
>>   fs/ceph/inode.c  |  2 +-
>>   4 files changed, 54 insertions(+), 4 deletions(-)
>>
>> diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c
>> index caa9863dee93..d6f1c444ce91 100644
>> --- a/fs/ceph/crypto.c
>> +++ b/fs/ceph/crypto.c
>> @@ -7,6 +7,53 @@
>>   #include "mds_client.h"
>>   #include "crypto.h"
>>   +static const char base64_table[65] =
>> +        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
>> +
>> +int ceph_base64_encode(const u8 *src, int srclen, char *dst)
>> +{
>> +	u32 ac = 0;
>> +	int bits = 0;
>> +	int i;
>> +	char *cp = dst;
>> +
>> +	for (i = 0; i < srclen; i++) {
>> +		ac = (ac << 8) | src[i];
>> +		bits += 8;
>> +		do {
>> +			bits -= 6;
>> +			*cp++ = base64_table[(ac >> bits) & 0x3f];
>> +		} while (bits >= 6);
>> +	}
>> +	if (bits)
>> +		*cp++ = base64_table[(ac << (6 - bits)) & 0x3f];
>> +	return cp - dst;
>> +}
>> +
>> +int ceph_base64_decode(const char *src, int srclen, u8 *dst)
>> +{
>> +	u32 ac = 0;
>> +	int bits = 0;
>> +	int i;
>> +	u8 *bp = dst;
>> +
>> +	for (i = 0; i < srclen; i++) {
>> +		const char *p = strchr(base64_table, src[i]);
>> +
>> +		if (p == NULL || src[i] == 0)
>> +			return -1;
>> +		ac = (ac << 6) | (p - base64_table);
>> +		bits += 6;
>> +		if (bits >= 8) {
>> +			bits -= 8;
>> +			*bp++ = (u8)(ac >> bits);
>> +		}
>> +	}
>> +	if (ac & ((1 << bits) - 1))
>> +		return -1;
>> +	return bp - dst;
>> +}
>
> Maybe this should be in fs/crypto.c ?

Yeah, if the solution is to modify the base64 encoding, that's my
preference too (in the series cover-letter this would correspond to
alternative #3).

[ In fact, I've probably done something very wrong here, as I didn't even
  mentioned that this patch is basically a copy of someone else's code; I
  simply modified the table used.  So, please do *not* consider merging
  this patch. ]

Cheers,
-- 
Luís

>
> -- Xiubo
>
>> +
>>   static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
>>   {
>>   	struct ceph_inode_info *ci = ceph_inode(inode);
>> @@ -260,7 +307,7 @@ int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name, char
>>   	}
>>     	/* base64 encode the encrypted name */
>> -	elen = fscrypt_base64url_encode(cryptbuf, len, buf);
>> +	elen = ceph_base64_encode(cryptbuf, len, buf);
>>   	dout("base64-encoded ciphertext name = %.*s\n", elen, buf);
>>     	WARN_ON(elen > (CEPH_NOHASH_NAME_MAX + SHA256_DIGEST_SIZE));
>> @@ -365,7 +412,7 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
>>   			tname = &_tname;
>>   		}
>>   -		declen = fscrypt_base64url_decode(name, name_len, tname->name);
>> +		declen = ceph_base64_decode(name, name_len, tname->name);
>>   		if (declen <= 0) {
>>   			ret = -EIO;
>>   			goto out;
>> diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h
>> index 3273d076a9e5..d22316011810 100644
>> --- a/fs/ceph/crypto.h
>> +++ b/fs/ceph/crypto.h
>> @@ -93,6 +93,9 @@ static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
>>    */
>>   #define CEPH_NOHASH_NAME_MAX (180 - SHA256_DIGEST_SIZE)
>>   +int ceph_base64_encode(const u8 *src, int srclen, char *dst);
>> +int ceph_base64_decode(const char *src, int srclen, u8 *dst);
>> +
>>   void ceph_fscrypt_set_ops(struct super_block *sb);
>>     void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
>> diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
>> index 5ae5cb778389..417d8c3a7edd 100644
>> --- a/fs/ceph/dir.c
>> +++ b/fs/ceph/dir.c
>> @@ -960,7 +960,7 @@ static int prep_encrypted_symlink_target(struct ceph_mds_request *req, const cha
>>   		goto out;
>>   	}
>>   -	len = fscrypt_base64url_encode(osd_link.name, osd_link.len,
>> req->r_path2);
>> +	len = ceph_base64_encode(osd_link.name, osd_link.len, req->r_path2);
>>   	req->r_path2[len] = '\0';
>>   out:
>>   	fscrypt_fname_free_buffer(&osd_link);
>> diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
>> index 359e29896f16..8fd493257e0b 100644
>> --- a/fs/ceph/inode.c
>> +++ b/fs/ceph/inode.c
>> @@ -875,7 +875,7 @@ static int decode_encrypted_symlink(const char *encsym, int enclen, u8 **decsym)
>>   	if (!sym)
>>   		return -ENOMEM;
>>   -	declen = fscrypt_base64url_decode(encsym, enclen, sym);
>> +	declen = ceph_base64_decode(encsym, enclen, sym);
>>   	if (declen < 0) {
>>   		pr_err("%s: can't decode symlink (%d). Content: %.*s\n", __func__, declen, enclen, encsym);
>>   		kfree(sym);
>>
>

      reply	other threads:[~2022-03-18 11:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 15:45 [RFC PATCH v3 0/4] ceph: add support for snapshot names encryption Luís Henriques
2022-03-17 15:45 ` [RFC PATCH v3 1/4] ceph: add support for encrypted snapshot names Luís Henriques
2022-03-17 15:45 ` [RFC PATCH v3 2/4] ceph: handle encrypted snapshot names in subdirectories Luís Henriques
2022-03-18  4:57   ` Xiubo Li
2022-03-18  9:57     ` Jeff Layton
2022-03-18 11:19       ` Xiubo Li
2022-03-18 10:53     ` Luís Henriques
2022-03-18 11:28       ` Xiubo Li
2022-03-18 11:49         ` Luís Henriques
2022-03-17 15:45 ` [RFC PATCH v3 3/4] ceph: update documentation regarding snapshot naming limitations Luís Henriques
2022-03-17 15:45 ` [RFC PATCH v3 4/4] ceph: replace base64url by the encoding used for mailbox names Luís Henriques
2022-03-18  8:32   ` Xiubo Li
2022-03-18 11:01     ` Luís Henriques [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8735jfh6du.fsf@brahms.olymp \
    --to=lhenriques@suse.de \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=jlayton@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xiubli@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.