Skip to contentSkip to conversation

x86/sgx: missing kref_put() in sgx_encl_mm_add() error path

BUG Dingisoul 3 participants 4 messages Mar 30 – Apr 8 Intel SGX

lore
Share & tools
lore

Dingisoul

author
#0 lore raw

Hi Kernel maintainers,

We found a possible refcount leak in sgx_encl_mm_add().

In this function, a reference to encl->refcount is taken before registering the MMU notifier:

/* Grab a refcount for the encl_mm->encl reference: */ kref_get(&encl->refcount); // 1. Reference acquired here. encl_mm->encl = encl;

ret = __mmu_notifier_register(&encl_mm->mmu_notifier, mm);

if (ret) {
	kfree(encl_mm);     
	return ret;         // 2. Returns without kref_put.
}

If __mmu_notifier_register() fails, the function frees encl_mm but does not drop the reference acquired by kref_get(&encl->refcount). This seems to leak one reference to encl.

Please let us know if the kref_put is unnecessary here.

Thanks.

#1 lore raw
Quoted 3 lines · Dingisoul

If __mmu_notifier_register() fails, the function frees encl_mm but does not drop the reference acquired by kref_get(&encl->refcount). This seems to leak one reference to encl.

Yep it looks like a leak.

Quoted 1 line

Please let us know if the kref_put is unnecessary here.

The easiest way to fix it is to just move the kref_get() until after the notifier has been registered. There's no risk of encl going away in this context, so it doesn't matter when the kref_get() happens as long as it _does_ happen.

author
#2 lore raw

Hi Dave,

Thanks for the confirmation and the suggestion.

Moving the kref_get() after the successful mmu_notifier registration looks great and keeps the error path clean.

Should we send a formal patch?

maintainer
#3 lore raw
In reply to Dingisoul
Quoted 16 lines · Dingisoul

Hi Kernel maintainers,

We found a possible refcount leak in sgx_encl_mm_add().

In this function, a reference to encl->refcount is taken before registering the MMU notifier:

/* Grab a refcount for the encl_mm->encl reference: */ kref_get(&encl->refcount); // 1. Reference acquired here. encl_mm->encl = encl;

ret = __mmu_notifier_register(&encl_mm->mmu_notifier, mm);

if (ret) {
	kfree(encl_mm);     
	return ret;         // 2. Returns without kref_put.
}
Quoted 5 lines

If __mmu_notifier_register() fails, the function frees encl_mm but does not drop the reference acquired by kref_get(&encl->refcount). This seems to leak one reference to encl.

Please let us know if the kref_put is unnecessary here.

Thanks.

It is a leak as encl's, stored in file, will retain increased refcount.

BR, Jarkko