Re: [PATCH 3/6] edac, mce_amd_inj: Modify flags attrigute to use string arguments

Aravind Gopalakrishnan
2015-06-01T19:17:17Z

On 5/29/2015 8:49 AM, Borislav Petkov wrote:
> On Wed, May 27, 2015 at 02:03:35PM -0500, Aravind Gopalakrishnan wrote:
>>
>> +#define MAX_FLAG_OPT_SIZE 10
> Why 10?
>
> This should be 2 and increased when another, longer injection type
> string gets introduced.
>

So I hit an issue when I have this as 2 and moving 'ret = cnt' statement
(in flags_write() below) to after the check for MAX_FLAG_OPT_SIZE.

The problem was, if I did echo hw > flags;
I would get an error on dmesg like so-
[ 78.692949] flags_write: Invalid flags value:

But the write would have actually gone through. i.e, if we do cat flags,
the output was 'hw'.

The issue seems to be that 'cnt' of flags_write() is 3 (after accounting
for a terminating NULL) when we enter the function (for this case),

Since we move 'ret = cnt;' statement to after the check, we end up
returning 2.
And since it is less than 'cnt', we re-enter flags_write() for the final
NULL character.
At this point, our comparisons with flags_options[] fail and we end up
returning -EINVAL from __set_inj().
Hence the error on dmesg..

The fix to this was simply having MAX_FLAG_OPT_SIZE as 3. In this case,
we already account for the NULL and flags_write()
returns the correct value upon success.
So I shall go ahead and make that change to MAX_FLAG_OPT_SIZE as 3 and
increase it in subsequent patch for the apic interrupts.

Thanks,
-Aravind.

>>
>> -DEFINE_SIMPLE_ATTRIBUTE(flags_fops, flags_get, flags_set, "%llu\n");
>> +static ssize_t flags_write(struct file *filp, const char __user *ubuf,
>> + size_t cnt, loff_t *ppos)
>> +{
>> + char buf[MAX_FLAG_OPT_SIZE + 1];
>> + int err;
>> + size_t ret;
>> +
>> + ret = cnt;
> You're assigning cnt to ret here...
>
>> +
>> + if (cnt > MAX_FLAG_OPT_SIZE)
>> + cnt = MAX_FLAG_OPT_SIZE;
> ... but correcting cnt afterwards. The assignment should be *after* that
> correction.
>
>> +
>> + if (copy_from_user(&buf, ubuf, cnt))
>> + return -EFAULT;
>> +
>> + buf[cnt] = 0;
>> +
>> + /* strip whitespaces.. */
>> + strstrip(buf);
>> +
>> + err = __set_inj(buf, cnt - 1);
>> + if (err) {
>> + pr_err("%s: Invalid flags value: %s\n", __func__, buf);
>> + return err;
>> + }
>> +
>> + *ppos += ret;
>> +
>> + return ret;
>> +}
>> +
>>