Re: [PATCH 01/15] sched: convert sighand_struct.count to refcount_t

From: Thomas Gleixner
Date: Fri Oct 20 2017 - 08:44:10 EST


On Fri, 20 Oct 2017, Elena Reshetova wrote:

> atomic_t variables are currently used to implement reference
> counters with the following properties:
> - counter is initialized to 1 using atomic_set()
> - a resource is freed upon counter reaching zero
> - once counter reaches zero, its further
> increments aren't allowed
> - counter schema uses basic atomic operations
> (set, inc, inc_not_zero, dec_and_test, etc.)
>
> Such atomic variables should be converted to a newly provided
> refcount_t type and API that prevents accidental counter overflows
> and underflows. This is important since overflows and underflows
> can lead to use-after-free situation and be exploitable.
>
> The variable sighand_struct.count is used as pure reference counter.

This still does not mention that atomic_t != recfcount_t ordering wise and
why you think that this does not matter in that use case.

And looking deeper:

> @@ -1381,7 +1381,7 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
> struct sighand_struct *sig;
>
> if (clone_flags & CLONE_SIGHAND) {
> - atomic_inc(&current->sighand->count);
> + refcount_inc(&current->sighand->count);
> return 0;

> void __cleanup_sighand(struct sighand_struct *sighand)
> {
> - if (atomic_dec_and_test(&sighand->count)) {
> + if (refcount_dec_and_test(&sighand->count)) {

How did you make sure that these atomic operations have no other
serialization effect and can be replaced with refcount?

I complained about that before and Peter explained it to you in great
length, but you just resend the same thing again. Where is the correctness
analysis? Seriously, for this kind of stuff it's not sufficient to use a
coccinelle script and copy boiler plate change logs and be done with it.

Thanks,

tglx