diff mbox series

KEYS: remove redundant memsets

Message ID 20200721141516.20335-1-trix@redhat.com (mailing list archive)
State New, archived
Headers show
Series KEYS: remove redundant memsets | expand

Commit Message

Tom Rix July 21, 2020, 2:15 p.m. UTC
From: Tom Rix <trix@redhat.com>

Reviewing use of memset in keyctrl_pkey.c

keyctl_pkey_params_get prologue code to set params up

	memset(params, 0, sizeof(*params));
	params->encoding = "raw";

keyctl_pkey_params_get_2 and keyctl_pkey_query have the same
prologue and they call keyctl_pkey_params_get.

So remove the prologue from the callers.

In keyctl_pkey_params_get_2, reorder the copy_from_user
of uparams to closer to it's use to ensure that
the keyctrl_pkey_params_get is called first.

Fixes: 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")

Signed-off-by: Tom Rix <trix@redhat.com>
---
 security/keys/keyctl_pkey.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Comments

kernel test robot July 22, 2020, 6:42 a.m. UTC | #1
Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on security/next-testing]
[also build test WARNING on linus/master dhowells-fs/fscache-next v5.8-rc6 next-20200721]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/trix-redhat-com/KEYS-remove-redundant-memsets/20200721-221633
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git next-testing
config: microblaze-randconfig-r022-20200719 (attached as .config)
compiler: microblaze-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=microblaze 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   security/keys/keyctl_pkey.c: In function 'keyctl_pkey_params_get_2':
>> security/keys/keyctl_pkey.c:122:8: warning: 'uparams.key_id' is used uninitialized in this function [-Wuninitialized]
     122 |  ret = keyctl_pkey_params_get(uparams.key_id, _info, params);
         |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +122 security/keys/keyctl_pkey.c

00d60fd3b93219 David Howells 2018-10-09  108  
00d60fd3b93219 David Howells 2018-10-09  109  /*
00d60fd3b93219 David Howells 2018-10-09  110   * Get parameters from userspace.  Callers must always call the free function
00d60fd3b93219 David Howells 2018-10-09  111   * on params, even if an error is returned.
00d60fd3b93219 David Howells 2018-10-09  112   */
00d60fd3b93219 David Howells 2018-10-09  113  static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params,
00d60fd3b93219 David Howells 2018-10-09  114  				    const char __user *_info,
00d60fd3b93219 David Howells 2018-10-09  115  				    int op,
00d60fd3b93219 David Howells 2018-10-09  116  				    struct kernel_pkey_params *params)
00d60fd3b93219 David Howells 2018-10-09  117  {
00d60fd3b93219 David Howells 2018-10-09  118  	struct keyctl_pkey_params uparams;
00d60fd3b93219 David Howells 2018-10-09  119  	struct kernel_pkey_query info;
00d60fd3b93219 David Howells 2018-10-09  120  	int ret;
00d60fd3b93219 David Howells 2018-10-09  121  
00d60fd3b93219 David Howells 2018-10-09 @122  	ret = keyctl_pkey_params_get(uparams.key_id, _info, params);
00d60fd3b93219 David Howells 2018-10-09  123  	if (ret < 0)
00d60fd3b93219 David Howells 2018-10-09  124  		return ret;
00d60fd3b93219 David Howells 2018-10-09  125  
00d60fd3b93219 David Howells 2018-10-09  126  	ret = params->key->type->asym_query(params, &info);
00d60fd3b93219 David Howells 2018-10-09  127  	if (ret < 0)
00d60fd3b93219 David Howells 2018-10-09  128  		return ret;
00d60fd3b93219 David Howells 2018-10-09  129  
bb67c86855f477 Tom Rix       2020-07-21  130  	if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
bb67c86855f477 Tom Rix       2020-07-21  131  		return -EFAULT;
bb67c86855f477 Tom Rix       2020-07-21  132  
00d60fd3b93219 David Howells 2018-10-09  133  	switch (op) {
00d60fd3b93219 David Howells 2018-10-09  134  	case KEYCTL_PKEY_ENCRYPT:
00d60fd3b93219 David Howells 2018-10-09  135  	case KEYCTL_PKEY_DECRYPT:
00d60fd3b93219 David Howells 2018-10-09  136  		if (uparams.in_len  > info.max_enc_size ||
00d60fd3b93219 David Howells 2018-10-09  137  		    uparams.out_len > info.max_dec_size)
00d60fd3b93219 David Howells 2018-10-09  138  			return -EINVAL;
00d60fd3b93219 David Howells 2018-10-09  139  		break;
00d60fd3b93219 David Howells 2018-10-09  140  	case KEYCTL_PKEY_SIGN:
00d60fd3b93219 David Howells 2018-10-09  141  	case KEYCTL_PKEY_VERIFY:
00d60fd3b93219 David Howells 2018-10-09  142  		if (uparams.in_len  > info.max_sig_size ||
00d60fd3b93219 David Howells 2018-10-09  143  		    uparams.out_len > info.max_data_size)
00d60fd3b93219 David Howells 2018-10-09  144  			return -EINVAL;
00d60fd3b93219 David Howells 2018-10-09  145  		break;
00d60fd3b93219 David Howells 2018-10-09  146  	default:
00d60fd3b93219 David Howells 2018-10-09  147  		BUG();
00d60fd3b93219 David Howells 2018-10-09  148  	}
00d60fd3b93219 David Howells 2018-10-09  149  
00d60fd3b93219 David Howells 2018-10-09  150  	params->in_len  = uparams.in_len;
00d60fd3b93219 David Howells 2018-10-09  151  	params->out_len = uparams.out_len;
00d60fd3b93219 David Howells 2018-10-09  152  	return 0;
00d60fd3b93219 David Howells 2018-10-09  153  }
00d60fd3b93219 David Howells 2018-10-09  154  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
David Howells July 22, 2020, 8:01 a.m. UTC | #2
trix@redhat.com wrote:

> -	if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
> -		return -EFAULT;
> -
>  	ret = keyctl_pkey_params_get(uparams.key_id, _info, params);

Erm...  uparams is used on the very next statement after the copy_from_user().

David
diff mbox series

Patch

diff --git a/security/keys/keyctl_pkey.c b/security/keys/keyctl_pkey.c
index 931d8dfb4a7f..60b504681388 100644
--- a/security/keys/keyctl_pkey.c
+++ b/security/keys/keyctl_pkey.c
@@ -119,12 +119,6 @@  static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 	struct kernel_pkey_query info;
 	int ret;
 
-	memset(params, 0, sizeof(*params));
-	params->encoding = "raw";
-
-	if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
-		return -EFAULT;
-
 	ret = keyctl_pkey_params_get(uparams.key_id, _info, params);
 	if (ret < 0)
 		return ret;
@@ -133,6 +127,9 @@  static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 	if (ret < 0)
 		return ret;
 
+	if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
+		return -EFAULT;
+
 	switch (op) {
 	case KEYCTL_PKEY_ENCRYPT:
 	case KEYCTL_PKEY_DECRYPT:
@@ -166,8 +163,6 @@  long keyctl_pkey_query(key_serial_t id,
 	struct kernel_pkey_query res;
 	long ret;
 
-	memset(&params, 0, sizeof(params));
-
 	ret = keyctl_pkey_params_get(id, _info, &params);
 	if (ret < 0)
 		goto error;