diff mbox

[1/2] Add infrastructure for pointer validation

Message ID 20160718033525.GB5809@balbir.ozlabs.ibm.com
State Changes Requested
Headers show

Commit Message

Balbir Singh July 18, 2016, 3:35 a.m. UTC
If the kernel called an OPAL API with vmalloc'd address
or any other address range in real mode, we would hit
a problem with aliasing. Since the top 4 bits are ignored
in real mode, pointers from 0xc.. and 0xd.. (and other ranges)
could collide and lead to hard to solve bugs. This patch
adds the infrastructure for pointer validation and a simple
test case for testing the API

Signed-off-by: Balbir Singh <bsingharora@gmail.com>
---
 core/test/Makefile.check |  1 +
 core/test/run-api-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/opal-api.h       | 23 ++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 core/test/run-api-test.c

Comments

Stewart Smith Aug. 9, 2016, 7:17 a.m. UTC | #1
Balbir Singh <bsingharora@gmail.com> writes:
> If the kernel called an OPAL API with vmalloc'd address
> or any other address range in real mode, we would hit
> a problem with aliasing. Since the top 4 bits are ignored
> in real mode, pointers from 0xc.. and 0xd.. (and other ranges)
> could collide and lead to hard to solve bugs. This patch
> adds the infrastructure for pointer validation and a simple
> test case for testing the API
>
> Signed-off-by: Balbir Singh <bsingharora@gmail.com>
> ---
>  core/test/Makefile.check |  1 +
>  core/test/run-api-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
>  include/opal-api.h       | 23 ++++++++++++++++++++++
>  3 files changed, 75 insertions(+)
>  create mode 100644 core/test/run-api-test.c

Finally got a chance to poke at this and think about it.

I like it.

I have a few thoughts that'd be great to address before merging,
although I'm rather keen to merge.

> --- /dev/null
> +++ b/core/test/run-api-test.c
> @@ -0,0 +1,51 @@
> +/* Copyright 2014-2015 IBM Corp.

Should be to 2016.

> diff --git a/include/opal-api.h b/include/opal-api.h
> index c86244b..e717ca3 100644
> --- a/include/opal-api.h
> +++ b/include/opal-api.h
> @@ -213,6 +213,9 @@
>
>  #ifndef __ASSEMBLY__
>
> +#include <stdbool.h>
> +#include <types.h>
> +
>  /* Other enums */
>  enum OpalVendorApiTokens {
>  	OPAL_START_VENDOR_API_RANGE = 1000, OPAL_END_VENDOR_API_RANGE = 1999
> @@ -1043,6 +1046,26 @@ enum {
>  	OPAL_PCI_TCE_KILL_ALL,
>  };
>
> +extern unsigned long top_of_ram;
> +
> +/*
> + * Returns true if the address is valid, false otherwise
> + *
> + * Checks if the passed address belongs to real address space
> + * or 0xc000... kernel address space. It also checks that
> + * addr <= total physical memory
> + */
> +static inline bool opal_addr_valid(const void *addr)
> +{
> +	unsigned long val = (unsigned long)addr;
> +	if ((val >> 60) != 0xc && (val >> 60) != 0x0)

I think it would be good to point to section 5.7 of the ISA as to where
the 60 comes from. We *could* check for a implementation specific value
too, P8 seems to be 50 bits (although other procs are
different). Although I'm not sure if there's any real value to be had
from that?

> +		return false;
> +	val &= ~0xf000000000000000;
> +	if (val > top_of_ram)
> +		return false;
> +	return true;
> +}

I'm wondering how we can hook this up to prlog(PR_ERROR) or something,
and then have FWTS annotation for it so that fwts would pick it up from
the skiboot log... maybe a macro around it?
Balbir Singh Aug. 10, 2016, 1:17 a.m. UTC | #2
On 09/08/16 17:17, Stewart Smith wrote:
> Balbir Singh <bsingharora@gmail.com> writes:
>> If the kernel called an OPAL API with vmalloc'd address
>> or any other address range in real mode, we would hit
>> a problem with aliasing. Since the top 4 bits are ignored
>> in real mode, pointers from 0xc.. and 0xd.. (and other ranges)
>> could collide and lead to hard to solve bugs. This patch
>> adds the infrastructure for pointer validation and a simple
>> test case for testing the API
>>
>> Signed-off-by: Balbir Singh <bsingharora@gmail.com>
>> ---
>>  core/test/Makefile.check |  1 +
>>  core/test/run-api-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  include/opal-api.h       | 23 ++++++++++++++++++++++
>>  3 files changed, 75 insertions(+)
>>  create mode 100644 core/test/run-api-test.c
> 
> Finally got a chance to poke at this and think about it.
> 
> I like it.
> 
> I have a few thoughts that'd be great to address before merging,
> although I'm rather keen to merge.
> 
>> --- /dev/null
>> +++ b/core/test/run-api-test.c
>> @@ -0,0 +1,51 @@
>> +/* Copyright 2014-2015 IBM Corp.
> 
> Should be to 2016.
> 
I'll fix this

>> diff --git a/include/opal-api.h b/include/opal-api.h
>> index c86244b..e717ca3 100644
>> --- a/include/opal-api.h
>> +++ b/include/opal-api.h
>> @@ -213,6 +213,9 @@
>>
>>  #ifndef __ASSEMBLY__
>>
>> +#include <stdbool.h>
>> +#include <types.h>
>> +
>>  /* Other enums */
>>  enum OpalVendorApiTokens {
>>  	OPAL_START_VENDOR_API_RANGE = 1000, OPAL_END_VENDOR_API_RANGE = 1999
>> @@ -1043,6 +1046,26 @@ enum {
>>  	OPAL_PCI_TCE_KILL_ALL,
>>  };
>>
>> +extern unsigned long top_of_ram;
>> +
>> +/*
>> + * Returns true if the address is valid, false otherwise
>> + *
>> + * Checks if the passed address belongs to real address space
>> + * or 0xc000... kernel address space. It also checks that
>> + * addr <= total physical memory
>> + */
>> +static inline bool opal_addr_valid(const void *addr)
>> +{
>> +	unsigned long val = (unsigned long)addr;
>> +	if ((val >> 60) != 0xc && (val >> 60) != 0x0)
> 
> I think it would be good to point to section 5.7 of the ISA as to where
> the 60 comes from. We *could* check for a implementation specific value
> too, P8 seems to be 50 bits (although other procs are
> different). Although I'm not sure if there's any real value to be had
> from that?


I'll add a reference to the ISA, BTW in the kernel we assume 60 bits and I think it should be fine. I think checking for top 4 bits and size of ram should be sufficient. Does doing the implementation specific check gain us any more?
> 
>> +		return false;
>> +	val &= ~0xf000000000000000;
>> +	if (val > top_of_ram)
>> +		return false;
>> +	return true;
>> +}
> 
> I'm wondering how we can hook this up to prlog(PR_ERROR) or something,
> and then have FWTS annotation for it so that fwts would pick it up from
> the skiboot log... maybe a macro around it?
> 

Good idea, I would like do that incrementally, I'm trying to understand how to integrate the error with FWTS, so that the error log makes sense.

Thanks for the review,
Balbir Singh
Stewart Smith Aug. 10, 2016, 1:45 a.m. UTC | #3
Balbir Singh <bsingharora@gmail.com> writes:
> On 09/08/16 17:17, Stewart Smith wrote:
>> Balbir Singh <bsingharora@gmail.com> writes:
>>> If the kernel called an OPAL API with vmalloc'd address
>>> or any other address range in real mode, we would hit
>>> a problem with aliasing. Since the top 4 bits are ignored
>>> in real mode, pointers from 0xc.. and 0xd.. (and other ranges)
>>> could collide and lead to hard to solve bugs. This patch
>>> adds the infrastructure for pointer validation and a simple
>>> test case for testing the API
>>>
>>> Signed-off-by: Balbir Singh <bsingharora@gmail.com>
>>> ---
>>>  core/test/Makefile.check |  1 +
>>>  core/test/run-api-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
>>>  include/opal-api.h       | 23 ++++++++++++++++++++++
>>>  3 files changed, 75 insertions(+)
>>>  create mode 100644 core/test/run-api-test.c
>> 
>> Finally got a chance to poke at this and think about it.
>> 
>> I like it.
>> 
>> I have a few thoughts that'd be great to address before merging,
>> although I'm rather keen to merge.
>> 
>>> --- /dev/null
>>> +++ b/core/test/run-api-test.c
>>> @@ -0,0 +1,51 @@
>>> +/* Copyright 2014-2015 IBM Corp.
>> 
>> Should be to 2016.
>> 
> I'll fix this
>
>>> diff --git a/include/opal-api.h b/include/opal-api.h
>>> index c86244b..e717ca3 100644
>>> --- a/include/opal-api.h
>>> +++ b/include/opal-api.h
>>> @@ -213,6 +213,9 @@
>>>
>>>  #ifndef __ASSEMBLY__
>>>
>>> +#include <stdbool.h>
>>> +#include <types.h>
>>> +
>>>  /* Other enums */
>>>  enum OpalVendorApiTokens {
>>>  	OPAL_START_VENDOR_API_RANGE = 1000, OPAL_END_VENDOR_API_RANGE = 1999
>>> @@ -1043,6 +1046,26 @@ enum {
>>>  	OPAL_PCI_TCE_KILL_ALL,
>>>  };
>>>
>>> +extern unsigned long top_of_ram;
>>> +
>>> +/*
>>> + * Returns true if the address is valid, false otherwise
>>> + *
>>> + * Checks if the passed address belongs to real address space
>>> + * or 0xc000... kernel address space. It also checks that
>>> + * addr <= total physical memory
>>> + */
>>> +static inline bool opal_addr_valid(const void *addr)
>>> +{
>>> +	unsigned long val = (unsigned long)addr;
>>> +	if ((val >> 60) != 0xc && (val >> 60) != 0x0)
>> 
>> I think it would be good to point to section 5.7 of the ISA as to where
>> the 60 comes from. We *could* check for a implementation specific value
>> too, P8 seems to be 50 bits (although other procs are
>> different). Although I'm not sure if there's any real value to be had
>> from that?
>
>
> I'll add a reference to the ISA, BTW in the kernel we assume 60 bits
> and I think it should be fine. I think checking for top 4 bits and
> size of ram should be sufficient. Does doing the implementation
> specific check gain us any more?

I don't think so... I guess we could end up erroring out rather than
checkstopping?

>> 
>>> +		return false;
>>> +	val &= ~0xf000000000000000;
>>> +	if (val > top_of_ram)
>>> +		return false;
>>> +	return true;
>>> +}
>> 
>> I'm wondering how we can hook this up to prlog(PR_ERROR) or something,
>> and then have FWTS annotation for it so that fwts would pick it up from
>> the skiboot log... maybe a macro around it?
>> 
>
> Good idea, I would like do that incrementally, I'm trying to
> understand how to integrate the error with FWTS, so that the error log
> makes sense.

I'm pretty sure that error logs making sense is rather non-traditional :)
diff mbox

Patch

diff --git a/core/test/Makefile.check b/core/test/Makefile.check
index b24bc21..cc3b47a 100644
--- a/core/test/Makefile.check
+++ b/core/test/Makefile.check
@@ -20,6 +20,7 @@  CORE_TEST := core/test/run-device \
 CORE_TEST_NOSTUB := core/test/run-console-log
 CORE_TEST_NOSTUB += core/test/run-console-log-buf-overrun
 CORE_TEST_NOSTUB += core/test/run-console-log-pr_fmt
+CORE_TEST_NOSTUB += core/test/run-api-test
 
 LCOV_EXCLUDE += $(CORE_TEST:%=%.c) core/test/stubs.c
 LCOV_EXCLUDE += $(CORE_TEST_NOSTUB:%=%.c) /usr/include/*
diff --git a/core/test/run-api-test.c b/core/test/run-api-test.c
new file mode 100644
index 0000000..9bf2195
--- /dev/null
+++ b/core/test/run-api-test.c
@@ -0,0 +1,51 @@ 
+/* Copyright 2014-2015 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *	http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * For now it just validates that addresses passed are sane and test the
+ * wrapper that validates addresses
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <compiler.h>
+#include <opal-api.h>
+
+#define __TEST__
+unsigned long top_of_ram;	/* Fake it here */
+int main(void)
+{
+	unsigned long addr = 0xd000000000000000;
+
+	top_of_ram = 16ULL * 1024 * 1024 * 1024; /* 16 GB */
+	assert(opal_addr_valid((void *)addr) == false);
+
+	addr = 0xc000000000000000;
+	assert(opal_addr_valid((void *)addr) == true);
+
+	addr = 0x0;
+	assert(opal_addr_valid((void *)addr) == true);
+
+	addr = ~0;
+	assert(opal_addr_valid((void *)addr) == false);
+
+	addr = top_of_ram + 1;
+	assert(opal_addr_valid((void *)addr) == false);
+	return 0;
+}
diff --git a/include/opal-api.h b/include/opal-api.h
index c86244b..e717ca3 100644
--- a/include/opal-api.h
+++ b/include/opal-api.h
@@ -213,6 +213,9 @@ 
 
 #ifndef __ASSEMBLY__
 
+#include <stdbool.h>
+#include <types.h>
+
 /* Other enums */
 enum OpalVendorApiTokens {
 	OPAL_START_VENDOR_API_RANGE = 1000, OPAL_END_VENDOR_API_RANGE = 1999
@@ -1043,6 +1046,26 @@  enum {
 	OPAL_PCI_TCE_KILL_ALL,
 };
 
+extern unsigned long top_of_ram;
+
+/*
+ * Returns true if the address is valid, false otherwise
+ *
+ * Checks if the passed address belongs to real address space
+ * or 0xc000... kernel address space. It also checks that
+ * addr <= total physical memory
+ */
+static inline bool opal_addr_valid(const void *addr)
+{
+	unsigned long val = (unsigned long)addr;
+	if ((val >> 60) != 0xc && (val >> 60) != 0x0)
+		return false;
+	val &= ~0xf000000000000000;
+	if (val > top_of_ram)
+		return false;
+	return true;
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __OPAL_API_H */