[bug report] evm: Check also if *tfm is an error pointer in init_desc()

Dan Carpenter dan.carpenter at oracle.com
Tue May 12 13:04:11 UTC 2020


On Tue, May 12, 2020 at 12:45:06PM +0000, Roberto Sassu wrote:
> > From: owner-linux-security-module at vger.kernel.org [mailto:owner-linux-
> > security-module at vger.kernel.org] On Behalf Of Dan Carpenter
> > Sent: Tuesday, May 12, 2020 2:34 PM
> > On Tue, May 12, 2020 at 11:31:53AM +0000, Roberto Sassu wrote:
> > > > From: Dan Carpenter [mailto:dan.carpenter at oracle.com]
> > > > Sent: Tuesday, May 12, 2020 12:48 PM
> > > >
> > > > Hello Roberto Sassu,
> > > >
> > > > The patch 53de3b080d5e: "evm: Check also if *tfm is an error pointer
> > > > in init_desc()" from Apr 27, 2020, leads to the following static
> > > > checker warning:
> > > >
> > > > 	security/integrity/evm/evm_crypto.c:119 init_desc()
> > > > 	error: '*tfm' dereferencing possible ERR_PTR()
> > > >
> > > > security/integrity/evm/evm_crypto.c
> > > >     89
> > > >     90                  tfm = &evm_tfm[hash_algo];
> > > >     91                  algo = hash_algo_name[hash_algo];
> > > >     92          }
> > > >     93
> > > >     94          if (IS_ERR_OR_NULL(*tfm)) {
> > > >
> > > > This used to be a "if (!*tfm)" check.
> > > >
> > > >     95                  mutex_lock(&mutex);
> > > >     96                  if (*tfm)
> > > >     97                          goto out;
> > > >
> > > > Then we test again with the lock held.  But in the new code if "*tfm"
> > > > is an error pointer then we jump directly to the unlock and crash on the
> > > > next line.  I can't see how the commit would fix anything.
> > >
> > > Hello Dan
> > >
> > > you are right. The fix should be applied in both places.
> > >
> > > if (!IS_ERR_OR_NULL(*tfm))
> > > 	goto out;
> > 
> > No.  I was wrong.
> > 
> > >
> > > >     98                  *tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
> > > >     99                  if (IS_ERR(*tfm)) {
> > > >    100                          rc = PTR_ERR(*tfm);
> > > >    101                          pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
> > > >    102                          *tfm = NULL;
> > > >    103                          mutex_unlock(&mutex);
> > > >    104                          return ERR_PTR(rc);
> > > >    105                  }
> > > >    106                  if (type == EVM_XATTR_HMAC) {
> > > >    107                          rc = crypto_shash_setkey(*tfm, evmkey,
> > evmkey_len);
> > > >    108                          if (rc) {
> > > >    109                                  crypto_free_shash(*tfm);
> > > >    110                                  *tfm = NULL;
> > > >    111                                  mutex_unlock(&mutex);
> > > >    112                                  return ERR_PTR(rc);
> > > >    113                          }
> > > >    114                  }
> > > >    115  out:
> > > >    116                  mutex_unlock(&mutex);
> > > >    117          }
> > > >    118
> > > >    119          desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> > > >                                                                      ^^^^
> > > > I don't understand how using *tfm outside of a lock is safe at all
> > > > anyway.
> > >
> > > I think the purpose of the mutex is just to  prevent two concurrent
> > > allocations. Later, it should not be a problem, as *tfm is never freed.
> > >
> > 
> > Actually by the time we take the lock then *tfm is either valid or NULL
> > so this code works.  It's confusing though.
> 
> static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
> {
>         return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
> }
> 
> CPU#1			CPU#2
> 			*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
> unlikely(!ptr)
> 			*tfm = NULL;
> IS_ERR_VALUE((unsigned long)ptr);
> 
> desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> 
> Could this happen?

Yeah.  Huh.  That's true.  Good eyes.

I feel like this would be more clear as well if we used a temporary
variable instead of working directly on "*tfm".

regards,
dan carpenter



More information about the Linux-security-module-archive mailing list