Re: [PATCH v9 5/8] ima: make process_buffer_measurement() generic

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/19 8:47 PM, Nayna Jain wrote:

Hi Nayna,


+void process_buffer_measurement(const void *buf, int size,
+   const char *eventname, enum ima_hooks func,
+   int pcr)
  {
int ret = 0;
struct ima_template_entry *entry = NULL;



+   if (func) {
+   security_task_getsecid(current, &secid);
+   action = ima_get_action(NULL, current_cred(), secid, 0, func,
+   &pcr, &template);
+   if (!(action & IMA_MEASURE))
+   return;
+   }


In your change set process_buffer_measurement is called with NONE for 
the parameter func. So ima_get_action (the above if block) will not be 
executed.


Wouldn't it better to update ima_get_action (and related functions) to 
handle the ima policy (func param)?


thanks,
 -lakshmi


Re: [PATCH v9 1/8] powerpc: detect the secure boot mode of the system

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/2019 8:47 PM, Nayna Jain wrote:

This patch defines a function to detect the secure boot state of a
PowerNV system.



+bool is_ppc_secureboot_enabled(void)
+{
+   struct device_node *node;
+   bool enabled = false;
+
+   node = of_find_compatible_node(NULL, NULL, "ibm,secvar-v1");
+   if (!of_device_is_available(node)) {
+   pr_err("Cannot find secure variable node in device tree; failing to 
secure state\n");
+   goto out;


Related to "goto out;" above:

Would of_find_compatible_node return NULL if the given node is not found?

If of_device_is_available returns false (say, because node is NULL or it 
does not find the specified node) would it be correct to call of_node_put?



+
+out:
+   of_node_put(node);


 -lakshmi


Re: [PATCH v9 2/8] powerpc/ima: add support to initialize ima policy rules

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/2019 8:47 PM, Nayna Jain wrote:


+/*
+ * The "secure_rules" are enabled only on "secureboot" enabled systems.
+ * These rules verify the file signatures against known good values.
+ * The "appraise_type=imasig|modsig" option allows the known good signature
+ * to be stored as an xattr or as an appended signature.
+ *
+ * To avoid duplicate signature verification as much as possible, the IMA
+ * policy rule for module appraisal is added only if CONFIG_MODULE_SIG_FORCE
+ * is not enabled.
+ */
+static const char *const secure_rules[] = {
+   "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig",
+#ifndef CONFIG_MODULE_SIG_FORCE
+   "appraise func=MODULE_CHECK appraise_type=imasig|modsig",
+#endif
+   NULL
+};


Is there any way to not use conditional compilation in the above array 
definition? Maybe define different functions to get "secure_rules" for 
when CONFIG_MODULE_SIG_FORCE is defined and when it is not defined.

Just a suggestion.

 -lakshmi


Re: [PATCH v9 3/8] powerpc: detect the trusted boot state of the system

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/2019 8:47 PM, Nayna Jain wrote:


+bool is_ppc_trustedboot_enabled(void)
+{
+   struct device_node *node;
+   bool enabled = false;
+
+   node = get_ppc_fw_sb_node();
+   enabled = of_property_read_bool(node, "trusted-enabled");


Can get_ppc_fw_sb_node return NULL?
Would of_property_read_bool handle the case when node is NULL?

 -lakshmi


Re: [PATCH v9 4/8] powerpc/ima: define trusted boot policy

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/2019 8:47 PM, Nayna Jain wrote:


+/*
+ * The "secure_and_trusted_rules" contains rules for both the secure boot and
+ * trusted boot. The "template=ima-modsig" option includes the appended
+ * signature, when available, in the IMA measurement list.
+ */
+static const char *const secure_and_trusted_rules[] = {
+   "measure func=KEXEC_KERNEL_CHECK template=ima-modsig",
+   "measure func=MODULE_CHECK template=ima-modsig",
+   "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig",
+#ifndef CONFIG_MODULE_SIG_FORCE
+   "appraise func=MODULE_CHECK appraise_type=imasig|modsig",
+#endif
+   NULL
+};


Same comment as earlier - any way to avoid using conditional compilation 
in C file?


 -lakshmi


Re: [PATCH v9 7/8] ima: check against blacklisted hashes for files with modsig

2019-10-24 Thread Lakshmi Ramasubramanian

On 10/23/2019 8:47 PM, Nayna Jain wrote:


+/*
+ * ima_check_blacklist - determine if the binary is blacklisted.
+ *
+ * Add the hash of the blacklisted binary to the measurement list, based
+ * on policy.
+ *
+ * Returns -EPERM if the hash is blacklisted.
+ */
+int ima_check_blacklist(struct integrity_iint_cache *iint,
+   const struct modsig *modsig, int pcr)
+{
+   enum hash_algo hash_algo;
+   const u8 *digest = NULL;
+   u32 digestsize = 0;
+   int rc = 0;
+
+   if (!(iint->flags & IMA_CHECK_BLACKLIST))
+   return 0;
+
+   if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
+   ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
+
+   rc = is_binary_blacklisted(digest, digestsize);
+   if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
+   process_buffer_measurement(digest, digestsize,
+  "blacklisted-hash", NONE,
+  pcr);
+   }


The enum value "NONE" is being passed to process_buffer_measurement to 
indicate that the check for required action based on ima policy is 
already done by ima_check_blacklist. Not sure, but this can cause 
confusion in the future when someone updates process_buffer_measurement.


Would it instead be better to add another parameter to 
process_buffer_measurement to indicate the above condition?


 -lakshmi


Re: [PATCH v5 1/4] powerpc/powernv: Add OPAL API interface to access secure variable

2019-10-25 Thread Lakshmi Ramasubramanian

On 10/24/19 5:47 PM, Nayna Jain wrote:



diff --git a/arch/powerpc/include/asm/opal-api.h 
b/arch/powerpc/include/asm/opal-api.h
index 378e3997845a..c1f25a760eb1 100644
--- a/arch/powerpc/include/asm/opal-api.h
+++ b/arch/powerpc/include/asm/opal-api.h
@@ -211,7 +211,10 @@
  #define OPAL_MPIPL_UPDATE 173
  #define OPAL_MPIPL_REGISTER_TAG   174
  #define OPAL_MPIPL_QUERY_TAG  175
-#define OPAL_LAST  175
+#define OPAL_SECVAR_GET176
+#define OPAL_SECVAR_GET_NEXT   177
+#define OPAL_SECVAR_ENQUEUE_UPDATE 178
+#define OPAL_LAST  178


Adjust indentation in the above #defines.


diff --git a/arch/powerpc/include/asm/opal.h b/arch/powerpc/include/asm/opal.h
index a0cf8fba4d12..9986ac34b8e2 100644
--- a/arch/powerpc/include/asm/opal.h
+++ b/arch/powerpc/include/asm/opal.h
@@ -298,6 +298,13 @@ int opal_sensor_group_clear(u32 group_hndl, int token);
  int opal_sensor_group_enable(u32 group_hndl, int token, bool enable);
  int opal_nx_coproc_init(uint32_t chip_id, uint32_t ct);
  
+int opal_secvar_get(const char *key, uint64_t key_len, u8 *data,

+   uint64_t *data_size);
+int opal_secvar_get_next(const char *key, uint64_t *key_len,
+uint64_t key_buf_size);
+int opal_secvar_enqueue_update(const char *key, uint64_t key_len, u8 *data,
+  uint64_t data_size);
+

Fix alignment of the parameters in the 2nd line.
Same comment in a few other files in this change set.



+
+static int opal_get_variable(const char *key, uint64_t ksize,
+u8 *data, uint64_t *dsize)
+{
+   int rc;
+
+   if (!key || !dsize)
+   return -EINVAL;
+
+   *dsize = cpu_to_be64(*dsize);
+
+   rc = opal_secvar_get(key, ksize, data, dsize);
+
+   *dsize = be64_to_cpu(*dsize);


Is it ok to update dsize even if return code (rc) from opal_secvar_get 
is an error? Just wanted to confirm.



+
+   *keylen = cpu_to_be64(*keylen);
+
+   rc = opal_secvar_get_next(key, keylen, keybufsize);
+
+   *keylen = be64_to_cpu(*keylen);

Same comment as above.


+
+   set_secvar_ops(&opal_secvar_ops);

Does this set function return status?


+
+   return 0;
+}


Re: [PATCH v5 2/4] powerpc: expose secure variables to userspace via sysfs

2019-10-25 Thread Lakshmi Ramasubramanian

On 10/24/19 5:47 PM, Nayna Jain wrote:


+static ssize_t size_show(struct kobject *kobj, struct kobj_attribute *attr,
+char *buf)
+{
+   uint64_t dsize;
+   int rc;
+
+   rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
+   if (rc) {
+   pr_err("Error retrieving variable size %d\n", rc);
+   return rc;
+   }
+
+   rc = sprintf(buf, "%llu\n", dsize);
+
+   return rc;
+}

nit: change it to "return sprintf(buf, "%llu\n", dsize);" instead.


+
+static ssize_t data_read(struct file *filep, struct kobject *kobj,
+struct bin_attribute *attr, char *buf, loff_t off,
+size_t count)
+{
+   uint64_t dsize;
+   char *data;
+   int rc;
+
+   rc = secvar_ops->get(kobj->name, strlen(kobj->name) + 1, NULL, &dsize);
+   if (rc) {
+   pr_err("Error getting variable size %d\n", rc);
+   return rc;
+   }
+   pr_debug("dsize is %llu\n", dsize);
+
+   data = kzalloc(dsize, GFP_KERNEL);

Is there any MAX\MIN limit on dsize that can be returned by secvar_ops?
Is it ok to not validate the dsize

+
+static ssize_t update_write(struct file *filep, struct kobject *kobj,
+   struct bin_attribute *attr, char *buf, loff_t off,
+   size_t count)
+{
+   int rc;
+
+   pr_debug("count is %ld\n", count);
+   rc = secvar_ops->set(kobj->name, strlen(kobj->name)+1, buf, count);
+   if (rc) {
+   pr_err("Error setting the variable %s\n", kobj->name);
+   return rc;
+   }
+
+   return count;
+}
Return value from this function can be a count (of bytes in buf?) or 
error code. Could cause confusion.

+
+static int secvar_sysfs_load(void)
+{
+   char *name;
+   uint64_t namesize = 0;
+   struct kobject *kobj;
+   int rc;
+
+   name = kzalloc(NAME_MAX_SIZE, GFP_KERNEL);
+   if (!name)
+   return -ENOMEM;
+
+   do {
+   rc = secvar_ops->get_next(name, &namesize, NAME_MAX_SIZE);
+   if (rc) {
+   if (rc != -ENOENT)
+   pr_err("error getting secvar from firmware 
%d\n",
+   rc);
+   break;
+   }
+
+   kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
+   if (!kobj)
+   return -ENOMEM;


Memory allocated for "name" is leaked in this case.


+
+   kobject_init(kobj, &secvar_ktype);
+
+   rc = kobject_add(kobj, &secvar_kset->kobj, "%s", name);
+   if (rc) {
+   pr_warn("kobject_add error %d for attribute: %s\n", rc,
+   name);
+   kobject_put(kobj);
+   kobj = NULL;
+   }
+
+   if (kobj)
+   kobject_uevent(kobj, KOBJ_ADD);
+
+   } while (!rc);
+
+   kfree(name);
+   return rc;
+}


Re: [PATCH v5 4/4] powerpc: load firmware trusted keys/hashes into kernel keyring

2019-10-25 Thread Lakshmi Ramasubramanian

On 10/24/19 5:58 PM, Nayna Jain wrote:


+
+/*
+ * Get a certificate list blob from the named secure variable.
+ */
+static __init void *get_cert_list(u8 *key, unsigned long keylen, uint64_t 
*size)
+{
+   int rc;
+   void *db;
+
+   rc = secvar_ops->get(key, keylen, NULL, size);
+   if (rc) {
+   pr_err("Couldn't get size: %d\n", rc);
+   return NULL;
+   }
+
+   db = kmalloc(*size, GFP_KERNEL);


Is there a MIN\MAX limit on size that should be validated here before 
memory allocation?



+   if (!db)
+   return NULL;
+
+   rc = secvar_ops->get(key, keylen, db, size);
+   if (rc) {
+   kfree(db);
+   pr_err("Error reading db var: %d\n", rc);
+   return NULL;

nit: set db to NULL and return from the end of the function.


+   }
+
+   return db;
+}




Re: [PATCH v9 5/8] ima: make process_buffer_measurement() generic

2019-10-25 Thread Lakshmi Ramasubramanian




On 10/25/2019 10:24 AM, Nayna Jain wrote:


On 10/24/19 10:20 AM, Lakshmi Ramasubramanian wrote:

On 10/23/19 8:47 PM, Nayna Jain wrote:

Hi Nayna,


+void process_buffer_measurement(const void *buf, int size,
+    const char *eventname, enum ima_hooks func,
+    int pcr)
  {
  int ret = 0;
  struct ima_template_entry *entry = NULL;



+    if (func) {
+    security_task_getsecid(current, &secid);
+    action = ima_get_action(NULL, current_cred(), secid, 0, func,
+    &pcr, &template);
+    if (!(action & IMA_MEASURE))
+    return;
+    }


In your change set process_buffer_measurement is called with NONE for 
the parameter func. So ima_get_action (the above if block) will not be 
executed.


Wouldn't it better to update ima_get_action (and related functions) to 
handle the ima policy (func param)?



The idea is to use ima-buf template for the auxiliary measurement 
record. The auxiliary measurement record is an additional record to the 
one already created based on the existing policy. When func is passed as 
NONE, it represents it is an additional record. I am not sure what you 
mean by updating ima_get_action, it is already handling the ima policy.




I was referring to using "func" in process_buffer_measurement to 
determine ima action. In my opinion, process_buffer_measurement should 
be generic.


ima_get_action() should instead determine the required ima action, 
template, pcr, etc. based on "func" passed to it.


thanks,
 -lakshmi



Re: [PATCH v9 2/8] powerpc/ima: add support to initialize ima policy rules

2019-10-25 Thread Lakshmi Ramasubramanian

On 10/25/2019 10:02 AM, Nayna Jain wrote:

>> Is there any way to not use conditional compilation in
>> the above array definition? Maybe define different functions to get
>> "secure_rules" for when CONFIG_MODULE_SIG_FORCE is defined and when
>> it is not defined.
>
> How will you decide which function to be called ?

Define the array in the C file:

const char *const secure_rules_kernel_check[] = {
   "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig",
   NULL
};

const char *const secure_rules_kernel_module_check[] = {
   "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig|modsig",
   "appraise func=MODULE_CHECK appraise_type=imasig|modsig",
   NULL
};

And, in the header file :

extern const char *const secure_rules_kernel_check;
extern const char *const secure_rules_kernel_module_check;

#ifdef CONFIG_MODULE_SIG_FORCE
const char *secure_rules() { return secure_rules_kernel_check; }
#else
const char *secure_rules() { return secure_rules_kernel_module_check;}
#endif // #ifdef CONFIG_MODULE_SIG_FORCE

If you want to avoid duplication, secure_rules_kernel_check and 
secure_rules_kernel_module_check could be defined in separate C files 
and conditionally compiled (in Makefile).



I was just trying to suggest the guidelines given in
"Section 21) Conditional Compilation" in coding-style.rst.

It says:
Whenever possible don't use preprocessor conditionals (#ifdef, #if) in 
.c files;...


Feel free to do what you think is appropriate.

thanks,
 -lakshmi




Re: [PATCH v9 5/8] ima: make process_buffer_measurement() generic

2019-10-30 Thread Lakshmi Ramasubramanian

On 10/23/19 8:47 PM, Nayna Jain wrote:

Hi Nayna,


process_buffer_measurement() is limited to measuring the kexec boot
command line. This patch makes process_buffer_measurement() more
generic, allowing it to measure other types of buffer data (e.g.
blacklisted binary hashes or key hashes).


Now that process_buffer_measurement() is being made generic to measure 
any buffer, it would be good to add a tag to indicate what type of 
buffer is being measured.


For example, if the buffer is kexec command line the log could look like:

 "kexec_cmdline: "

Similarly, if the buffer is blacklisted binary hash:

 "blacklist hash: ".

If the buffer is key hash:

 ": key data".

This would greatly help the consumer of the IMA log to know the type of 
data represented in each IMA log entry.


thanks,
 -lakshmi


Re: [PATCH v10 5/9] ima: make process_buffer_measurement() generic

2019-10-31 Thread Lakshmi Ramasubramanian

On 10/30/19 8:31 PM, Mimi Zohar wrote:


  void ima_kexec_cmdline(const void *buf, int size)
  {
-   u32 secid;
-
-   if (buf && size != 0) {
-   security_task_getsecid(current, &secid);
+   if (buf && size != 0)


Open brace { is missing in the above if statement.


process_buffer_measurement(buf, size, "kexec-cmdline",
-  current_cred(), secid);
-   }
+  KEXEC_CMDLINE, 0);
  }


 -lakshmi


Re: [PATCH v10 5/9] ima: make process_buffer_measurement() generic

2019-10-31 Thread Lakshmi Ramasubramanian

On 10/31/19 10:02 AM, Lakshmi Ramasubramanian wrote:


On 10/30/19 8:31 PM, Mimi Zohar wrote:


  void ima_kexec_cmdline(const void *buf, int size)
  {
-    u32 secid;
-
-    if (buf && size != 0) {
-    security_task_getsecid(current, &secid);
+    if (buf && size != 0)


Open brace { is missing in the above if statement.


My mistake -
I now see that the braces {} have been removed in the if statement since 
there is only line body  the call to process_buffer_measurement()


 -lakshmi




  process_buffer_measurement(buf, size, "kexec-cmdline",
-   current_cred(), secid);
-    }
+   KEXEC_CMDLINE, 0);
  }


  -lakshmi




Re: [PATCH v6 1/4] powerpc/powernv: Add OPAL API interface to access secure variable

2019-11-05 Thread Lakshmi Ramasubramanian

On 11/5/2019 12:24 AM, Eric Richter wrote:


From: Nayna Jain 

The X.509 certificates trusted by the platform and required to secure boot
the OS kernel are wrapped in secure variables, which are controlled by
OPAL.

This patch adds firmware/kernel interface to read and write OPAL secure
variables based on the unique key.


I feel splitting this patch into smaller set of changes would make it 
easier to review. For instance roughly as below:


 1, opal-api.h which adds the #defines  OPAL_SECVAR_ and the API signature.
 2, secvar.h then adds secvar_operations struct
 3, powerpc/kernel for the Interface definitions
 4, powernv/opal-secvar.c for the API implementations
 5, powernv/opal-call.c for the API calls
 6, powernv/opal.c for the secvar init calls.


diff --git a/arch/powerpc/include/asm/opal-api.h 
b/arch/powerpc/include/asm/opal-api.h
index 378e3997845a..c1f25a760eb1 100644
--- a/arch/powerpc/include/asm/opal-api.h
+++ b/arch/powerpc/include/asm/opal-api.h
@@ -211,7 +211,10 @@
  #define OPAL_MPIPL_UPDATE 173
  #define OPAL_MPIPL_REGISTER_TAG   174
  #define OPAL_MPIPL_QUERY_TAG  175
-#define OPAL_LAST  175
+#define OPAL_SECVAR_GET176
+#define OPAL_SECVAR_GET_NEXT   177
+#define OPAL_SECVAR_ENQUEUE_UPDATE 178
+#define OPAL_LAST  178


Please fix the indentation for the #defines



+static int opal_get_variable(const char *key, uint64_t ksize,
+u8 *data, uint64_t *dsize)
+{
+   int rc;
+
+   if (!key || !dsize)
+   return -EINVAL;
+
+   *dsize = cpu_to_be64(*dsize);
+
+   rc = opal_secvar_get(key, ksize, data, dsize);
+
+   *dsize = be64_to_cpu(*dsize);


Should the return status (rc) from opal_secvar_get be checked before 
attempting to do the conversion (be64_to_cpu)?



+static int opal_get_next_variable(const char *key, uint64_t *keylen,
+ uint64_t keybufsize)
+{
+   int rc;
+
+   if (!key || !keylen)
+   return -EINVAL;
+
+   *keylen = cpu_to_be64(*keylen);
+
+   rc = opal_secvar_get_next(key, keylen, keybufsize);
+
+   *keylen = be64_to_cpu(*keylen);


Same comment as above - should rc be checke before attempting to convert?


+
+   return opal_status_to_err(rc);
+}
+
+static int opal_set_variable(const char *key, uint64_t ksize, u8 *data,
+uint64_t dsize)
+{
+   int rc;
+
+   if (!key || !data)
+   return -EINVAL;


Is the key and data received here from a trusted caller? If not, should 
there be some validation checks done here before enqueuing the data?


 -lakshmi



Re: [PATCH v6 2/4] powerpc: expose secure variables to userspace via sysfs

2019-11-05 Thread Lakshmi Ramasubramanian

On 11/5/2019 12:24 AM, Eric Richter wrote:


From: Nayna Jain 

PowerNV secure variables, which store the keys used for OS kernel
verification, are managed by the firmware. These secure variables need to
be accessed by the userspace for addition/deletion of the certificates.

This patch adds the sysfs interface to expose secure variables for PowerNV
secureboot. The users shall use this interface for manipulating
the keys stored in the secure variables.


Can this patch be split into smaller set of changes:
1, Definitions of attribute functions like backend_show, size_show, etc.
2, secvar_sysfs_load
3, secvar_sysfs_init
4, secvar_sysfs_exit


+static int secvar_sysfs_load(void)
+{
+   char *name;
+   uint64_t namesize = 0;
+   struct kobject *kobj;
+   int rc;
+
+   name = kzalloc(NAME_MAX_SIZE, GFP_KERNEL);
+   if (!name)
+   return -ENOMEM;
+
+   do {
+   rc = secvar_ops->get_next(name, &namesize, NAME_MAX_SIZE);
+   if (rc) {
+   if (rc != -ENOENT)
+   pr_err("error getting secvar from firmware 
%d\n",
+   rc);
+   break;
+   }
+
+   kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
+   if (!kobj) {
+   rc = -ENOMEM;
+   break;
+   }
+
+   kobject_init(kobj, &secvar_ktype);
+
+   rc = kobject_add(kobj, &secvar_kset->kobj, "%s", name);
+   if (rc) {
+   pr_warn("kobject_add error %d for attribute: %s\n", rc,
+   name);
+   kobject_put(kobj);
+   kobj = NULL;
+   }
+
+   if (kobj)
+   kobject_uevent(kobj, KOBJ_ADD);


kobject_event() will add kobj and free the memory when done using the 
object?


 -lakshmi


Re: [PATCH v9 0/4] powerpc: expose secure variables to the kernel and userspace

2019-11-11 Thread Lakshmi Ramasubramanian

On 11/10/19 7:10 PM, Nayna Jain wrote:

Hi Nayna,


In order to verify the OS kernel on PowerNV systems, secure boot requires
X.509 certificates trusted by the platform. These are stored in secure
variables controlled by OPAL, called OPAL secure variables. In order to
enable users to manage the keys, the secure variables need to be exposed
to userspace.
Are you planning to split the patches in this patch set into smaller 
chunks so that it is easier to code review and also perhaps make it 
easier when merging the changes?


Just a suggestion - but if, folks familiar with this code base don't 
have any objections, please feel free to ignore my comment.


Patch #1
 1, opal-api.h which adds the #defines  OPAL_SECVAR_ and the API signature.
 2, secvar.h then adds secvar_operations struct
 3, powerpc/kernel for the Interface definitions
 4, powernv/opal-secvar.c for the API implementations
 5, powernv/opal-call.c for the API calls
 6, powernv/opal.c for the secvar init calls.

Patch #2
1, Definitions of attribute functions like backend_show, size_show, etc.
2, secvar_sysfs_load
3, secvar_sysfs_init
4, secvar_sysfs_exit

thanks,
 -lakshmi


Re: [PATCH] ima: add a new CONFIG for loading arch-specific policies

2020-02-26 Thread Lakshmi Ramasubramanian

Hi Nayna,


+
+config IMA_SECURE_AND_OR_TRUSTED_BOOT
+   bool
+   depends on IMA
+   depends on IMA_ARCH_POLICY
+   default n
+   help
+  This option is selected by architectures to enable secure and/or
+  trusted boot based on IMA runtime policies.



Why is the default for this new config "n"?
Is there any reason to not turn on this config if both IMA and 
IMA_ARCH_POLICY are set to y?


thanks,
 -lakshmi



Re: [PATCH v10 0/9] powerpc: Enabling IMA arch specific secure boot policies

2019-12-09 Thread Lakshmi Ramasubramanian

Hi Mimi,

On 10/30/2019 8:31 PM, Mimi Zohar wrote:


This patchset extends the previous version[1] by adding support for
checking against a blacklist of binary hashes.

The IMA subsystem supports custom, built-in, arch-specific policies to
define the files to be measured and appraised. These policies are honored
based on priority, where arch-specific policy is the highest and custom
is the lowest.


Has this change been signed off and merged for the next update of the 
kernel (v5.5)?


thanks,
 -lakshmi


Re: [PATCH v19 00/13] Carry forward IMA measurement log on kexec on ARM64

2021-03-02 Thread Lakshmi Ramasubramanian

On 3/2/21 7:06 AM, Rob Herring wrote:

On Sun, Feb 21, 2021 at 11:49 AM Lakshmi Ramasubramanian
 wrote:


On kexec file load Integrity Measurement Architecture (IMA) subsystem
may verify the IMA signature of the kernel and initramfs, and measure
it.  The command line parameters passed to the kernel in the kexec call
may also be measured by IMA.  A remote attestation service can verify
a TPM quote based on the TPM event log, the IMA measurement list, and
the TPM PCR data.  This can be achieved only if the IMA measurement log
is carried over from the current kernel to the next kernel across
the kexec call.

powerpc already supports carrying forward the IMA measurement log on
kexec.  This patch set adds support for carrying forward the IMA
measurement log on kexec on ARM64.

This patch set moves the platform independent code defined for powerpc
such that it can be reused for other platforms as well.  A chosen node
"linux,ima-kexec-buffer" is added to the DTB for ARM64 to hold
the address and the size of the memory reserved to carry
the IMA measurement log.

This patch set has been tested for ARM64 platform using QEMU.
I would like help from the community for testing this change on powerpc.
Thanks.

This patch set is based on
commit f31e3386a4e9 ("ima: Free IMA measurement buffer after kexec syscall")
in https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git
"ima-kexec-fixes" branch.


[...]



Lakshmi Ramasubramanian (10):
   kexec: Move ELF fields to struct kimage
   arm64: Use ELF fields defined in 'struct kimage'
   powerpc: Use ELF fields defined in 'struct kimage'
   x86: Use ELF fields defined in 'struct kimage'
   powerpc: Move ima buffer fields to struct kimage
   powerpc: Enable passing IMA log to next kernel on kexec
   powerpc: Move arch independent ima kexec functions to
 drivers/of/kexec.c
   kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT
   powerpc: Delete unused function delete_fdt_mem_rsv()
   arm64: Enable passing IMA log to next kernel on kexec

Rob Herring (3):
   of: Add a common kexec FDT setup function
   arm64: Use common of_kexec_alloc_and_setup_fdt()
   powerpc: Use common of_kexec_alloc_and_setup_fdt()

  arch/arm64/Kconfig |   1 +
  arch/arm64/include/asm/kexec.h |   4 -
  arch/arm64/kernel/machine_kexec_file.c | 194 +--
  arch/powerpc/Kconfig   |   2 +-
  arch/powerpc/include/asm/ima.h |  30 --
  arch/powerpc/include/asm/kexec.h   |  14 +-
  arch/powerpc/kexec/Makefile|   7 -
  arch/powerpc/kexec/elf_64.c|  30 +-
  arch/powerpc/kexec/file_load.c | 183 +-
  arch/powerpc/kexec/file_load_64.c  |  21 +-
  arch/powerpc/kexec/ima.c   | 219 
  arch/x86/include/asm/kexec.h   |   5 -
  arch/x86/kernel/crash.c|  14 +-
  arch/x86/kernel/kexec-bzimage64.c  |   2 +-
  arch/x86/kernel/machine_kexec_64.c |   4 +-
  drivers/of/Makefile|   6 +
  drivers/of/kexec.c | 458 +
  include/linux/kexec.h  |   8 +
  include/linux/of.h |   7 +
  security/integrity/ima/ima.h   |   4 -
  security/integrity/ima/ima_kexec.c |   9 +-
  21 files changed, 539 insertions(+), 683 deletions(-)
  delete mode 100644 arch/powerpc/include/asm/ima.h
  delete mode 100644 arch/powerpc/kexec/ima.c
  create mode 100644 drivers/of/kexec.c


I fixed up the Fixes tags and applied for 5.13.



Thanks a lot Rob.

 -lakshmi




Re: [PATCH v2] powerpc/kexec_file: Restore FDT size estimation for kdump kernel

2021-03-09 Thread Lakshmi Ramasubramanian

On 3/9/21 6:08 PM, Rob Herring wrote:

Hi Rob,


On Fri, Feb 19, 2021 at 6:52 PM Thiago Jung Bauermann
 wrote:


Commit 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump
kernel") fixed how elf64_load() estimates the FDT size needed by the
crashdump kernel.

At the same time, commit 130b2d59cec0 ("powerpc: Use common
of_kexec_alloc_and_setup_fdt()") changed the same code to use the generic
function of_kexec_alloc_and_setup_fdt() to calculate the FDT size. That
change made the code overestimate it a bit by counting twice the space
required for the kernel command line and /chosen properties.

Therefore change kexec_fdt_totalsize_ppc64() to calculate just the extra
space needed by the kdump kernel, and change the function name so that it
better reflects what the function is now doing.

Signed-off-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
  arch/powerpc/include/asm/kexec.h  |  2 +-
  arch/powerpc/kexec/elf_64.c   |  2 +-
  arch/powerpc/kexec/file_load_64.c | 26 --
  3 files changed, 10 insertions(+), 20 deletions(-)


I ended up delaying the referenced series til 5.13, but have applied
it now. Can I get an ack from the powerpc maintainers on this one?
I'll fixup the commit log to make sense given the commit id's aren't
valid.


I checked the change applied in linux-next branch and also Device Tree's 
for-next branch - it looks like v1 of Thiago's patch has been applied. 
Could you please pick up the v2 patch?


thanks,
 -lakshmi




Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-15 Thread Lakshmi Ramasubramanian

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
---
  arch/powerpc/kexec/elf_64.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..0051440c1f77 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -32,7 +32,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
int ret;
unsigned long kernel_load_addr;
unsigned long initrd_load_addr = 0, fdt_load_addr;
-   void *fdt;
+   void *fdt = NULL;
const void *slave_code;
struct elfhdr ehdr;
char *modified_cmdline = NULL;



thanks,
 -lakshmi


Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-16 Thread Lakshmi Ramasubramanian

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.


I'm a huge fan of initialising local variables! But I'm struggling to
find the code path that will lead to an uninit fdt being returned...

The out label reads in part:

/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're going to
return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt, but 
instead sets it in the arch specific field (please see the link to the 
updated elf_64.c below).


https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next



(btw, it does look like we might leak fdt if we have an error after we
successfully kmalloc it.)

Am I missing something? Can you link to the report for the kernel test
robot or from Dan?


/*
 * Once FDT buffer has been successfully passed to 
kexec_add_buffer(),
 * the FDT buffer address is saved in image->arch.fdt. In that 
case,

 * the memory cannot be freed here in case of any other error.
 */
if (ret && !image->arch.fdt)
kvfree(fdt);

return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless it has 
already been passed to kexec_add_buffer().


thanks,
 -lakshmi



FWIW, I think it's worth including this patch _anyway_ because initing
local variables is good practice, but I'm just not sure on the
justification.


Why is it good practice?

It defeats -Wuninitialized. So you're guaranteed to be returning
something initialised, but not necessarily initialised to the right
value.

In a case like this NULL seems like a safe choice, but it's still wrong.
The function is meant to return a pointer to the successfully allocated
fdt, or an ERR_PTR() value. NULL is neither of those.

I agree there are security reasons that initialising stack variables is
desirable, but I think that should be handled by the compiler, not at
the source level.

cheers





Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-19 Thread Lakshmi Ramasubramanian

On 4/19/21 4:30 PM, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.


I'm a huge fan of initialising local variables! But I'm struggling to
find the code path that will lead to an uninit fdt being returned...

The out label reads in part:

/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're going to
return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt, but
instead sets it in the arch specific field (please see the link to the
updated elf_64.c below).

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next



(btw, it does look like we might leak fdt if we have an error after we
successfully kmalloc it.)

Am I missing something? Can you link to the report for the kernel test
robot or from Dan?


/*
   * Once FDT buffer has been successfully passed to
kexec_add_buffer(),
   * the FDT buffer address is saved in image->arch.fdt. In that
case,
   * the memory cannot be freed here in case of any other error.
   */
  if (ret && !image->arch.fdt)
  kvfree(fdt);

  return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless it has
already been passed to kexec_add_buffer().


It feels like the root of the problem is that the kvfree of fdt is in
the wrong place. It's only allocated later in the function, so the error
path should reflect that. Something like the patch below.

cheers


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	fdt_pack(fdt);
  
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,

kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */

image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
  
+	goto out;

+
+out_free_fdt:
+   kvfree(fdt);
  out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
  
-	/*

-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
  }
  



This looks good to me. Thanks Michael.

I'll post the updated patch shortly.

 -lakshmi



Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-19 Thread Lakshmi Ramasubramanian

On 4/19/21 10:00 PM, Dan Carpenter wrote:

On Tue, Apr 20, 2021 at 09:30:16AM +1000, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.


I'm a huge fan of initialising local variables! But I'm struggling to
find the code path that will lead to an uninit fdt being returned...

The out label reads in part:

/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're going to
return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt, but
instead sets it in the arch specific field (please see the link to the
updated elf_64.c below).

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next



(btw, it does look like we might leak fdt if we have an error after we
successfully kmalloc it.)

Am I missing something? Can you link to the report for the kernel test
robot or from Dan?


/*
   * Once FDT buffer has been successfully passed to
kexec_add_buffer(),
   * the FDT buffer address is saved in image->arch.fdt. In that
case,
   * the memory cannot be freed here in case of any other error.
   */
  if (ret && !image->arch.fdt)
  kvfree(fdt);

  return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless it has
already been passed to kexec_add_buffer().


It feels like the root of the problem is that the kvfree of fdt is in
the wrong place. It's only allocated later in the function, so the error
path should reflect that. Something like the patch below.

cheers


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	fdt_pack(fdt);
  
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,

kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */

image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
  
+	goto out;


This will leak.  It would need to be something like:

if (ret) {
pr_err("Error setting up the purgatory.\n");
goto out_free_fdt;
}
Once "fdt" buffer is successfully passed to kexec_add_buffer() it cannot 
be freed here - it will be freed when the kexec cleanup function is called.




goto out;

But we should also fix the uninitialized variable of "elf_info" if
kexec_build_elf_info() fails.


kexec_build_elf_info() frees elf_info and zeroes it in error paths, 
except when elf_read_ehdr() fails. So, I think it is better to 
initialize the local variable "elf_info" before calling 
kexec_build_elf_info().


memset(&elf_info, 0, sizeof(elf_info));

thanks,
 -lakshmi




+
+out_free_fdt:
+   kvfree(fdt);
  out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
  
-	/*

-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
  }


regards,
dan carpenter





Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-20 Thread Lakshmi Ramasubramanian

On 4/20/21 6:06 AM, Rob Herring wrote:

On Tue, Apr 20, 2021 at 12:20 AM Lakshmi Ramasubramanian
 wrote:


On 4/19/21 10:00 PM, Dan Carpenter wrote:

On Tue, Apr 20, 2021 at 09:30:16AM +1000, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.


I'm a huge fan of initialising local variables! But I'm struggling to
find the code path that will lead to an uninit fdt being returned...

The out label reads in part:

   /* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
   return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're going to
return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt, but
instead sets it in the arch specific field (please see the link to the
updated elf_64.c below).

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next



(btw, it does look like we might leak fdt if we have an error after we
successfully kmalloc it.)

Am I missing something? Can you link to the report for the kernel test
robot or from Dan?


/*
* Once FDT buffer has been successfully passed to
kexec_add_buffer(),
* the FDT buffer address is saved in image->arch.fdt. In that
case,
* the memory cannot be freed here in case of any other error.
*/
   if (ret && !image->arch.fdt)
   kvfree(fdt);

   return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless it has
already been passed to kexec_add_buffer().


It feels like the root of the problem is that the kvfree of fdt is in
the wrong place. It's only allocated later in the function, so the error
path should reflect that. Something like the patch below.

cheers


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
  ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
initrd_len, cmdline);
  if (ret)
-goto out;
+goto out_free_fdt;

  fdt_pack(fdt);

@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
  kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  ret = kexec_add_buffer(&kbuf);
  if (ret)
-goto out;
+goto out_free_fdt;

  /* FDT will be freed in arch_kimage_file_post_load_cleanup */
  image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
  if (ret)
  pr_err("Error setting up the purgatory.\n");

+goto out;


This will leak.  It would need to be something like:

   if (ret) {
   pr_err("Error setting up the purgatory.\n");
   goto out_free_fdt;
   }

Once "fdt" buffer is successfully passed to kexec_add_buffer() it cannot
be freed here - it will be freed when the kexec cleanup function is called.


That may be the case currently, but really if a function returns an
error it should have undone anything it did like memory allocations. I
don't think you should do that to fix this issue, but it would be a
good clean-up.



I agree - in case of an error the function should do a proper clean-up.
Just to be clear - for now, I will leave this as is. Correct?

In my patch, I will do the following changes:

 => Free "fdt" when possible (as Michael had suggested in his patch)
 => Zero out "elf_info" struct at the start of the function.

thanks,
 -lakshmi





Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-20 Thread Lakshmi Ramasubramanian

On 4/20/21 7:42 AM, Lakshmi Ramasubramanian wrote:

On 4/20/21 6:06 AM, Rob Herring wrote:

On Tue, Apr 20, 2021 at 12:20 AM Lakshmi Ramasubramanian
 wrote:


On 4/19/21 10:00 PM, Dan Carpenter wrote:

On Tue, Apr 20, 2021 at 09:30:16AM +1000, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.

There are a few "goto out;" statements before the local 
variable "fdt"
is initialized through the call to 
of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being 
passed
to kvfree() in this function if there is an error before the 
call to

of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.

I'm a huge fan of initialising local variables! But I'm 
struggling to
find the code path that will lead to an uninit fdt being 
returned...


The out label reads in part:

   /* Make kimage_file_post_load_cleanup free the fdt buffer for 
us. */

   return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're 
going to

return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt, 
but
instead sets it in the arch specific field (please see the link to 
the

updated elf_64.c below).

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next 





(btw, it does look like we might leak fdt if we have an error 
after we

successfully kmalloc it.)

Am I missing something? Can you link to the report for the 
kernel test

robot or from Dan?


/*
    * Once FDT buffer has been successfully passed to
kexec_add_buffer(),
    * the FDT buffer address is saved in image->arch.fdt. 
In that

case,
    * the memory cannot be freed here in case of any other 
error.

    */
   if (ret && !image->arch.fdt)
   kvfree(fdt);

   return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless 
it has

already been passed to kexec_add_buffer().


It feels like the root of the problem is that the kvfree of fdt is in
the wrong place. It's only allocated later in the function, so the 
error

path should reflect that. Something like the patch below.

cheers


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, 
char *kernel_buf,

  ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
    initrd_len, cmdline);
  if (ret)
-    goto out;
+    goto out_free_fdt;

  fdt_pack(fdt);

@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, 
char *kernel_buf,

  kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  ret = kexec_add_buffer(&kbuf);
  if (ret)
-    goto out;
+    goto out_free_fdt;

  /* FDT will be freed in arch_kimage_file_post_load_cleanup */
  image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, 
char *kernel_buf,

  if (ret)
  pr_err("Error setting up the purgatory.\n");

+    goto out;


This will leak.  It would need to be something like:

   if (ret) {
   pr_err("Error setting up the purgatory.\n");
   goto out_free_fdt;
   }

Once "fdt" buffer is successfully passed to kexec_add_buffer() it cannot
be freed here - it will be freed when the kexec cleanup function is 
called.


That may be the case currently, but really if a function returns an
error it should have undone anything it did like memory allocations. I
don't think you should do that to fix this issue, but it would be a
good clean-up.



I agree - in case of an error the function should do a proper clean-up.
Just to be clear - for now, I will leave this as is. Correct?

In my patch, I will do the following changes:

  => Free "fdt" when possible (as Michael had suggested in his patch)
  => Zero out "elf_info" struct at the start of the function.



Instead of zeroing out "elf_info", I think it would be better to return 
an error immediately, instead of the "goto out;", if 
kexec_build_elf_info() fails.


   ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
   if (ret)
 return ERR_PTR(ret);

 -lakshmi



Re: [PATCH] powerpc: Initialize local variable fdt to NULL in elf64_load()

2021-04-20 Thread Lakshmi Ramasubramanian

On 4/20/21 8:47 AM, Rob Herring wrote:

On Tue, Apr 20, 2021 at 10:04 AM Lakshmi Ramasubramanian
 wrote:


On 4/20/21 7:42 AM, Lakshmi Ramasubramanian wrote:

On 4/20/21 6:06 AM, Rob Herring wrote:

On Tue, Apr 20, 2021 at 12:20 AM Lakshmi Ramasubramanian
 wrote:


On 4/19/21 10:00 PM, Dan Carpenter wrote:

On Tue, Apr 20, 2021 at 09:30:16AM +1000, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

On 4/16/21 2:05 AM, Michael Ellerman wrote:


Daniel Axtens  writes:

On 4/15/21 12:14 PM, Lakshmi Ramasubramanian wrote:

Sorry - missed copying device-tree and powerpc mailing lists.


There are a few "goto out;" statements before the local
variable "fdt"
is initialized through the call to
of_kexec_alloc_and_setup_fdt() in
elf64_load(). This will result in an uninitialized "fdt" being
passed
to kvfree() in this function if there is an error before the
call to
of_kexec_alloc_and_setup_fdt().

Initialize the local variable "fdt" to NULL.


I'm a huge fan of initialising local variables! But I'm
struggling to
find the code path that will lead to an uninit fdt being
returned...

The out label reads in part:

/* Make kimage_file_post_load_cleanup free the fdt buffer for
us. */
return ret ? ERR_PTR(ret) : fdt;

As far as I can tell, any time we get a non-zero ret, we're
going to
return an error pointer rather than the uninitialised value...


As Dan pointed out, the new code is in linux-next.

I have copied the new one below - the function doesn't return fdt,
but
instead sets it in the arch specific field (please see the link to
the
updated elf_64.c below).

https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next




(btw, it does look like we might leak fdt if we have an error
after we
successfully kmalloc it.)

Am I missing something? Can you link to the report for the
kernel test
robot or from Dan?


/*
 * Once FDT buffer has been successfully passed to
kexec_add_buffer(),
 * the FDT buffer address is saved in image->arch.fdt.
In that
case,
 * the memory cannot be freed here in case of any other
error.
 */
if (ret && !image->arch.fdt)
kvfree(fdt);

return ret ? ERR_PTR(ret) : NULL;

In case of an error, the memory allocated for fdt is freed unless
it has
already been passed to kexec_add_buffer().


It feels like the root of the problem is that the kvfree of fdt is in
the wrong place. It's only allocated later in the function, so the
error
path should reflect that. Something like the patch below.

cheers


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image,
char *kernel_buf,
   ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
 initrd_len, cmdline);
   if (ret)
-goto out;
+goto out_free_fdt;

   fdt_pack(fdt);

@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image,
char *kernel_buf,
   kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
   ret = kexec_add_buffer(&kbuf);
   if (ret)
-goto out;
+goto out_free_fdt;

   /* FDT will be freed in arch_kimage_file_post_load_cleanup */
   image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image,
char *kernel_buf,
   if (ret)
   pr_err("Error setting up the purgatory.\n");

+goto out;


This will leak.  It would need to be something like:

if (ret) {
pr_err("Error setting up the purgatory.\n");
goto out_free_fdt;
}

Once "fdt" buffer is successfully passed to kexec_add_buffer() it cannot
be freed here - it will be freed when the kexec cleanup function is
called.


That may be the case currently, but really if a function returns an
error it should have undone anything it did like memory allocations. I
don't think you should do that to fix this issue, but it would be a
good clean-up.



I agree - in case of an error the function should do a proper clean-up.
Just to be clear - for now, I will leave this as is. Correct?


Yes.

okay.




In my patch, I will do the following changes:

   => Free "fdt" when possible (as Michael had suggested in his patch)
   => Zero out "elf_info" struct at the start of the function.



Instead of zeroing out "elf_info", I think it would be better to return
an error immediately, instead of the "goto out;", if
kexec_build_elf_info() fails.

 ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
 if (ret)
   return ERR_PTR(ret);


I thought kexec_build_elf_info() can return an error and allocat

[PATCH 1/2] powerpc: Free fdt on error in elf64_load()

2021-04-20 Thread Lakshmi Ramasubramanian
There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load().  This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is
saved in the arch specific kimage struct, free the fdt.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
Suggested-by: Michael Ellerman 
---
 arch/powerpc/kexec/elf_64.c | 16 ++--
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
 
fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
 
/* FDT will be freed in arch_kimage_file_post_load_cleanup */
image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
 
+   goto out;
+
+out_free_fdt:
+   kvfree(fdt);
 out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
 
-   /*
-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
 }
 
-- 
2.31.0



[PATCH 2/2] powerpc: If kexec_build_elf_info() fails return immediately from elf64_load()

2021-04-20 Thread Lakshmi Ramasubramanian
Uninitialized local variable "elf_info" would be passed to
kexec_free_elf_info() if kexec_build_elf_info() returns an error
in elf64_load().

If kexec_build_elf_info() returns an error, return the error
immediately.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: Dan Carpenter 
---
 arch/powerpc/kexec/elf_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 02662e72c53d..eeb258002d1e 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -45,7 +45,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
 
ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
if (ret)
-   goto out;
+   return ERR_PTR(ret);
 
if (image->type == KEXEC_TYPE_CRASH) {
/* min & max buffer values for kdump case */
-- 
2.31.0



Re: [PATCH 1/2] powerpc: Free fdt on error in elf64_load()

2021-04-21 Thread Lakshmi Ramasubramanian

On 4/20/21 10:35 PM, Santosh Sivaraj wrote:
Hi Santosh,




There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load().  This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is
saved in the arch specific kimage struct, free the fdt.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
Suggested-by: Michael Ellerman 
---
  arch/powerpc/kexec/elf_64.c | 16 ++--
  1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;


Shouldn't there be a goto out_free_fdt if fdt_open_into fails?


You are likely looking at elf_64.c in the mainline branch. The patch I 
have submitted is based on Rob's device-tree for-next branch. Please see 
the link below:


https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git/tree/arch/powerpc/kexec/elf_64.c?h=for-next



  
  	fdt_pack(fdt);
  
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,

kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */

image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
  
+	goto out;

+
+out_free_fdt:
+   kvfree(fdt);


Can just use kfree here?

"fdt" is allocated through kvmalloc(). So it is freed using kvfree.

thanks,
 -lakshmi


  out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
  
-	/*

-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
  }
  
--

2.31.0




Re: [PATCH 1/2] powerpc: Free fdt on error in elf64_load()

2021-04-21 Thread Lakshmi Ramasubramanian

On 4/21/21 12:18 AM, Michael Ellerman wrote:

Lakshmi Ramasubramanian  writes:

There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load().  This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is
saved in the arch specific kimage struct, free the fdt.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
Suggested-by: Michael Ellerman 


I basically sent you the diff, so this should probably be:

   Reported-by: kernel test robot 
   Reported-by: Dan Carpenter 
   Signed-off-by: Michael Ellerman 
   Signed-off-by: Lakshmi Ramasubramanian 

Otherwise looks good to me, thanks for turning it into a proper patch
and submitting it.


I will submit the patch again with the above change.
Thanks for reviewing the patch.

Could you please review [PATCH 2/2] as well?

thanks,
 -lakshmi



cheers



diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	fdt_pack(fdt);
  
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,

kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */

image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
  
+	goto out;

+
+out_free_fdt:
+   kvfree(fdt);
  out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
  
-	/*

-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
  }
  
--

2.31.0




[PATCH v2 1/2] powerpc: Free fdt on error in elf64_load()

2021-04-21 Thread Lakshmi Ramasubramanian
There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load().  This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is
saved in the arch specific kimage struct, free the fdt.

Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
Signed-off-by: Michael Ellerman 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/kexec/elf_64.c | 16 ++--
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
 
fdt_pack(fdt);
 
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
 
/* FDT will be freed in arch_kimage_file_post_load_cleanup */
image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
 
+   goto out;
+
+out_free_fdt:
+   kvfree(fdt);
 out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
 
-   /*
-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
 }
 
-- 
2.31.0



[PATCH v2 2/2] powerpc: If kexec_build_elf_info() fails return immediately from elf64_load()

2021-04-21 Thread Lakshmi Ramasubramanian
Uninitialized local variable "elf_info" would be passed to
kexec_free_elf_info() if kexec_build_elf_info() returns an error
in elf64_load().

If kexec_build_elf_info() returns an error, return the error
immediately.

Signed-off-by: Lakshmi Ramasubramanian 
Reported-by: Dan Carpenter 
Reviewed-by: Michael Ellerman 
---
 arch/powerpc/kexec/elf_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 02662e72c53d..eeb258002d1e 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -45,7 +45,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
 
ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
if (ret)
-   goto out;
+   return ERR_PTR(ret);
 
if (image->type == KEXEC_TYPE_CRASH) {
/* min & max buffer values for kdump case */
-- 
2.31.0



Re: [PATCH v2 1/2] powerpc: Free fdt on error in elf64_load()

2021-04-23 Thread Lakshmi Ramasubramanian

On 4/21/21 9:36 AM, Lakshmi Ramasubramanian wrote:

Hi Dan,


There are a few "goto out;" statements before the local variable "fdt"
is initialized through the call to of_kexec_alloc_and_setup_fdt() in
elf64_load().  This will result in an uninitialized "fdt" being passed
to kvfree() in this function if there is an error before the call to
of_kexec_alloc_and_setup_fdt().

If there is any error after fdt is allocated, but before it is
saved in the arch specific kimage struct, free the fdt.

Reported-by: kernel test robot 
Reported-by: Dan Carpenter 
Signed-off-by: Michael Ellerman 
Signed-off-by: Lakshmi Ramasubramanian 
---
  arch/powerpc/kexec/elf_64.c | 16 ++--
  1 file changed, 6 insertions(+), 10 deletions(-)



Please review this patch and Patch 2/2.

thanks,
 -lakshmi


diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index 5a569bb51349..02662e72c53d 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -114,7 +114,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	fdt_pack(fdt);
  
@@ -125,7 +125,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf,

kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
-   goto out;
+   goto out_free_fdt;
  
  	/* FDT will be freed in arch_kimage_file_post_load_cleanup */

image->arch.fdt = fdt;
@@ -140,18 +140,14 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
if (ret)
pr_err("Error setting up the purgatory.\n");
  
+	goto out;

+
+out_free_fdt:
+   kvfree(fdt);
  out:
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
  
-	/*

-* Once FDT buffer has been successfully passed to kexec_add_buffer(),
-* the FDT buffer address is saved in image->arch.fdt. In that case,
-* the memory cannot be freed here in case of any other error.
-*/
-   if (ret && !image->arch.fdt)
-   kvfree(fdt);
-
return ret ? ERR_PTR(ret) : NULL;
  }
  





[PATCH v15 00/10] Carry forward IMA measurement log on kexec on ARM64

2021-01-15 Thread Lakshmi Ramasubramanian
 kernel test bot for ARM64
arch_ima_add_kexec_buffer() - moved this function to a new file
namely arch/arm64/kernel/ima_kexec.c

v4:
  - Submitting the patch series on behalf of the original author
Prakhar Srivastava 
  - Moved FDT_PROP_IMA_KEXEC_BUFFER ("linux,ima-kexec-buffer") to
libfdt.h so that it can be shared by multiple platforms.

v3:
Breakup patches further into separate patches.
  - Refactoring non architecture specific code out of powerpc
  - Update powerpc related code to use fdt functions
  - Update IMA buffer read related code to use of functions
  - Add support to store the memory information of the IMA
measurement logs to be carried forward.
  - Update the property strings to align with documented nodes
https://github.com/devicetree-org/dt-schema/pull/46

v2:
  Break patches into separate patches.
  - Powerpc related Refactoring
  - Updating the docuemntation for chosen node
  - Updating arm64 to support IMA buffer pass

v1:
  Refactoring carrying over IMA measuremnet logs over Kexec. This patch
moves the non-architecture specific code out of powerpc and adds to
security/ima.(Suggested by Thiago)
  Add Documentation regarding the ima-kexec-buffer node in the chosen
node documentation

v0:
  Add a layer of abstraction to use the memory reserved by device tree
for ima buffer pass.
  Add support for ima buffer pass using reserved memory for arm64 kexec.
Update the arch sepcific code path in kexec file load to store the
ima buffer in the reserved memory. The same reserved memory is read
on kexec or cold boot.

Lakshmi Ramasubramanian (6):
  ima: Move arch_ima_add_kexec_buffer() to ima
  powerpc: Move arch independent ima kexec functions to
drivers/of/kexec.c
  kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT
  powerpc: Delete unused function delete_fdt_mem_rsv()
  arm64: Call kmalloc() to allocate DTB buffer
  arm64: Add IMA log information in kimage used for kexec

Rob Herring (4):
  powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem
  of: Add a common kexec FDT setup function
  arm64: Use common of_kexec_setup_new_fdt()
  powerpc: Use common of_kexec_setup_new_fdt()

 arch/arm64/Kconfig |   1 +
 arch/arm64/include/asm/kexec.h |   5 +
 arch/arm64/kernel/machine_kexec_file.c | 135 +---
 arch/powerpc/Kconfig   |   2 +-
 arch/powerpc/include/asm/ima.h |  30 --
 arch/powerpc/include/asm/kexec.h   |   6 +-
 arch/powerpc/kexec/Makefile|   7 -
 arch/powerpc/kexec/file_load.c | 184 +--
 arch/powerpc/kexec/file_load_64.c  |   8 +-
 arch/powerpc/kexec/ima.c   | 219 -
 drivers/of/Makefile|   1 +
 drivers/of/kexec.c | 429 +
 include/linux/of.h |   7 +
 security/integrity/ima/ima.h   |   4 -
 security/integrity/ima/ima_kexec.c |  22 ++
 15 files changed, 484 insertions(+), 576 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c
 create mode 100644 drivers/of/kexec.c

-- 
2.30.0



[PATCH v15 02/10] of: Add a common kexec FDT setup function

2021-01-15 Thread Lakshmi Ramasubramanian
From: Rob Herring 

Both arm64 and powerpc do essentially the same FDT /chosen setup for
kexec.  The differences are either omissions that arm64 should have
or additional properties that will be ignored.  The setup code can be
combined and shared by both powerpc and arm64.

The differences relative to the arm64 version:
 - If /chosen doesn't exist, it will be created (should never happen).
 - Any old dtb and initrd reserved memory will be released.
 - The new initrd and elfcorehdr are marked reserved.
 - "linux,booted-from-kexec" is set.

The differences relative to the powerpc version:
 - "kaslr-seed" and "rng-seed" may be set.
 - "linux,elfcorehdr" is set.
 - Any existing "linux,usable-memory-range" is removed.

Combine the code for setting up the /chosen node in the FDT and updating
the memory reservation for kexec, for powerpc and arm64, in
of_kexec_setup_new_fdt() and move it to "drivers/of/kexec.c".

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 drivers/of/Makefile |   1 +
 drivers/of/kexec.c  | 236 
 include/linux/of.h  |   5 +
 3 files changed, 242 insertions(+)
 create mode 100644 drivers/of/kexec.c

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 6e1e5212f058..8ce11955afde 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -13,5 +13,6 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
 obj-$(CONFIG_OF_RESOLVE)  += resolver.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o
 obj-$(CONFIG_OF_NUMA) += of_numa.o
+obj-$(CONFIG_KEXEC_FILE) += kexec.o
 
 obj-$(CONFIG_OF_UNITTEST) += unittest-data/
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
new file mode 100644
index ..4afd3cc1c04a
--- /dev/null
+++ b/drivers/of/kexec.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Arm Limited
+ *
+ * Based on arch/arm64/kernel/machine_kexec_file.c:
+ *  Copyright (C) 2018 Linaro Limited
+ *
+ * And arch/powerpc/kexec/file_load.c:
+ *  Copyright (C) 2016  IBM Corporation
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* relevant device tree properties */
+#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
+#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
+#define FDT_PROP_INITRD_START  "linux,initrd-start"
+#define FDT_PROP_INITRD_END"linux,initrd-end"
+#define FDT_PROP_BOOTARGS  "bootargs"
+#define FDT_PROP_KASLR_SEED"kaslr-seed"
+#define FDT_PROP_RNG_SEED  "rng-seed"
+#define RNG_SEED_SIZE  128
+
+/**
+ * fdt_find_and_del_mem_rsv - delete memory reservation with given address and 
size
+ *
+ * @fdt:   Flattened device tree for the current kernel.
+ * @start: Starting address of the reserved memory.
+ * @size:  Size of the reserved memory.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned 
long size)
+{
+   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
+
+   for (i = 0; i < num_rsvs; i++) {
+   u64 rsv_start, rsv_size;
+
+   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
+   if (ret) {
+   pr_err("Malformed device tree.\n");
+   return -EINVAL;
+   }
+
+   if (rsv_start == start && rsv_size == size) {
+   ret = fdt_del_mem_rsv(fdt, i);
+   if (ret) {
+   pr_err("Error deleting device tree 
reservation.\n");
+   return -EINVAL;
+   }
+
+   return 0;
+   }
+   }
+
+   return -ENOENT;
+}
+
+/*
+ * of_kexec_setup_new_fdt - modify /chosen and memory reservation for the next 
kernel
+ *
+ * @image: kexec image being loaded.
+ * @fdt:   Flattened device tree for the next kernel.
+ * @initrd_load_addr:  Address where the next initrd will be loaded.
+ * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:   Command line for the next kernel, or NULL if there will
+ * be none.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+int of_kexec_setup_new_fdt(const struct kimage *image, void *fdt,
+  unsigned long initrd_load_addr, unsigned long 
initrd_len,
+  const char *cmdline)
+{
+   int ret, chosen_node;
+   const void *prop;
+
+   /* Remove memory reservation for the current device tree. */
+   ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
+  fdt_totalsize(initial_boot_params));
+   if (ret == -EINVAL)
+ 

[PATCH v15 01/10] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem

2021-01-15 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The architecture specific field, elfcorehdr_addr in struct kimage_arch,
that holds the address of the buffer in memory for ELF core header for
powerpc has a different name than the one used for arm64.  This makes
it hard to have a common code for setting up the device tree for
kexec system call.

Rename elfcorehdr_addr to elf_headers_mem to align with arm64 name so
common code can use it.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h  | 2 +-
 arch/powerpc/kexec/file_load.c| 4 ++--
 arch/powerpc/kexec/file_load_64.c | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 55d6ede30c19..dbf09d2f36d0 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -108,7 +108,7 @@ struct kimage_arch {
unsigned long backup_start;
void *backup_buf;
 
-   unsigned long elfcorehdr_addr;
+   unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
 
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 9a232bc36c8f..e452b11df631 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -45,7 +45,7 @@ char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
return NULL;
 
elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
-   image->arch.elfcorehdr_addr);
+   image->arch.elf_headers_mem);
 
if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
pr_err("Appending elfcorehdr= exceeds cmdline size\n");
@@ -263,7 +263,7 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
 * Avoid elfcorehdr from being stomped on in kdump kernel by
 * setting up memory reserve map.
 */
-   ret = fdt_add_mem_rsv(fdt, image->arch.elfcorehdr_addr,
+   ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
  image->arch.elf_headers_sz);
if (ret) {
pr_err("Error reserving elfcorehdr memory: %s\n",
diff --git a/arch/powerpc/kexec/file_load_64.c 
b/arch/powerpc/kexec/file_load_64.c
index c69bcf9b547a..a05c19b3cc60 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -815,7 +815,7 @@ static int load_elfcorehdr_segment(struct kimage *image, 
struct kexec_buf *kbuf)
goto out;
}
 
-   image->arch.elfcorehdr_addr = kbuf->mem;
+   image->arch.elf_headers_mem = kbuf->mem;
image->arch.elf_headers_sz = headers_sz;
image->arch.elf_headers = headers;
 out:
@@ -851,7 +851,7 @@ int load_crashdump_segments_ppc64(struct kimage *image,
return ret;
}
pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
-image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz);
+image->arch.elf_headers_mem, kbuf->bufsz, kbuf->memsz);
 
return 0;
 }
-- 
2.30.0



[PATCH v15 03/10] arm64: Use common of_kexec_setup_new_fdt()

2021-01-15 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_setup_new_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_setup_new_fdt() to setup the device tree
and update the memory reservation for kexec for arm64.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
Acked-by: Will Deacon 
---
 arch/arm64/kernel/machine_kexec_file.c | 123 +
 1 file changed, 3 insertions(+), 120 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 5b0e67b93cdc..7de9c47dee7c 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -15,23 +15,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
-
-/* relevant device tree properties */
-#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
-#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
-#define FDT_PROP_INITRD_START  "linux,initrd-start"
-#define FDT_PROP_INITRD_END"linux,initrd-end"
-#define FDT_PROP_BOOTARGS  "bootargs"
-#define FDT_PROP_KASLR_SEED"kaslr-seed"
-#define FDT_PROP_RNG_SEED  "rng-seed"
-#define RNG_SEED_SIZE  128
 
 const struct kexec_file_ops * const kexec_file_loaders[] = {
&kexec_image_ops,
@@ -50,112 +39,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
return kexec_image_post_load_cleanup_default(image);
 }
 
-static int setup_dtb(struct kimage *image,
-unsigned long initrd_load_addr, unsigned long initrd_len,
-char *cmdline, void *dtb)
-{
-   int off, ret;
-
-   ret = fdt_path_offset(dtb, "/chosen");
-   if (ret < 0)
-   goto out;
-
-   off = ret;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-   ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-
-   if (image->type == KEXEC_TYPE_CRASH) {
-   /* add linux,elfcorehdr */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_KEXEC_ELFHDR,
-   image->arch.elf_headers_mem,
-   image->arch.elf_headers_sz);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-
-   /* add linux,usable-memory-range */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_MEM_RANGE,
-   crashk_res.start,
-   crashk_res.end - crashk_res.start + 1);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-   }
-
-   /* add bootargs */
-   if (cmdline) {
-   ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add initrd-* */
-   if (initrd_load_addr) {
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
- initrd_load_addr);
-   if (ret)
-   goto out;
-
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
- initrd_load_addr + initrd_len);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add kaslr-seed */
-   ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
-   if (ret == -FDT_ERR_NOTFOUND)
-   ret = 0;
-   else if (ret)
-   goto out;
-
-   if (rng_is_initialized()) {
-   u64 seed = get_random_u64();
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
-   if (ret)
-   goto out;
-   } else {
-   pr_notice("RNG is not initialised: omitting \"%s\" property\n",
-   FDT_PROP_KASLR_SEED);
-   }
-
-   /* add rng-seed */
-   if (rng_is_initialized())

[PATCH v15 04/10] powerpc: Use common of_kexec_setup_new_fdt()

2021-01-15 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_setup_new_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_setup_new_fdt() to setup the device tree
and update the memory reservation for kexec for powerpc.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/kexec/file_load.c | 125 ++---
 1 file changed, 6 insertions(+), 119 deletions(-)

diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index e452b11df631..956bcb2d1ec2 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -16,6 +16,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -156,132 +157,18 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
  unsigned long initrd_load_addr, unsigned long initrd_len,
  const char *cmdline)
 {
-   int ret, chosen_node;
-   const void *prop;
-
-   /* Remove memory reservation for the current device tree. */
-   ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
-fdt_totalsize(initial_boot_params));
-   if (ret == 0)
-   pr_debug("Removed old device tree reservation.\n");
-   else if (ret != -ENOENT)
-   return ret;
-
-   chosen_node = fdt_path_offset(fdt, "/chosen");
-   if (chosen_node == -FDT_ERR_NOTFOUND) {
-   chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
- "chosen");
-   if (chosen_node < 0) {
-   pr_err("Error creating /chosen.\n");
-   return -EINVAL;
-   }
-   } else if (chosen_node < 0) {
-   pr_err("Malformed device tree: error reading /chosen.\n");
-   return -EINVAL;
-   }
-
-   /* Did we boot using an initrd? */
-   prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
-   if (prop) {
-   uint64_t tmp_start, tmp_end, tmp_size;
-
-   tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-   prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
-   if (!prop) {
-   pr_err("Malformed device tree.\n");
-   return -EINVAL;
-   }
-   tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-   /*
-* kexec reserves exact initrd size, while firmware may
-* reserve a multiple of PAGE_SIZE, so check for both.
-*/
-   tmp_size = tmp_end - tmp_start;
-   ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
-   if (ret == -ENOENT)
-   ret = delete_fdt_mem_rsv(fdt, tmp_start,
-round_up(tmp_size, PAGE_SIZE));
-   if (ret == 0)
-   pr_debug("Removed old initrd reservation.\n");
-   else if (ret != -ENOENT)
-   return ret;
-
-   /* If there's no new initrd, delete the old initrd's info. */
-   if (initrd_len == 0) {
-   ret = fdt_delprop(fdt, chosen_node,
- "linux,initrd-start");
-   if (ret) {
-   pr_err("Error deleting linux,initrd-start.\n");
-   return -EINVAL;
-   }
-
-   ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
-   if (ret) {
-   pr_err("Error deleting linux,initrd-end.\n");
-   return -EINVAL;
-   }
-   }
-   }
-
-   if (initrd_len) {
-   ret = fdt_setprop_u64(fdt, chosen_node,
- "linux,initrd-start",
- initrd_load_addr);
-   if (ret < 0)
-   goto err;
-
-   /* initrd-end is the first address after the initrd image. */
-   ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
- initrd_load_addr + initrd_len);
-   if (ret < 0)
-   goto err;
-
-   ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
-   if (ret) {
-   pr_err("Error reserving initrd memory: %s\n",
-  fdt_strerror(ret));
-   return -EINVAL;
-   }

[PATCH v15 09/10] arm64: Call kmalloc() to allocate DTB buffer

2021-01-15 Thread Lakshmi Ramasubramanian
create_dtb() function allocates kernel virtual memory for
the device tree blob (DTB).  This is not consistent with other
architectures, such as powerpc, which calls kmalloc() for allocating
memory for the DTB.

Call kmalloc() to allocate memory for the DTB, and kfree() to free
the allocated memory.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/arm64/kernel/machine_kexec_file.c | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 7de9c47dee7c..51c40143d6fa 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -29,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-   vfree(image->arch.dtb);
+   kfree(image->arch.dtb);
image->arch.dtb = NULL;
 
vfree(image->arch.elf_headers);
@@ -59,19 +59,21 @@ static int create_dtb(struct kimage *image,
+ cmdline_len + DTB_EXTRA_SPACE;
 
for (;;) {
-   buf = vmalloc(buf_size);
+   buf = kmalloc(buf_size, GFP_KERNEL);
if (!buf)
return -ENOMEM;
 
/* duplicate a device tree blob */
ret = fdt_open_into(initial_boot_params, buf, buf_size);
-   if (ret)
+   if (ret) {
+   kfree(buf);
return -EINVAL;
+   }
 
ret = of_kexec_setup_new_fdt(image, buf, initrd_load_addr,
 initrd_len, cmdline);
if (ret) {
-   vfree(buf);
+   kfree(buf);
if (ret == -ENOMEM) {
/* unlikely, but just in case */
buf_size += DTB_EXTRA_SPACE;
@@ -217,6 +219,6 @@ int load_other_segments(struct kimage *image,
return 0;
 
 out_err:
-   vfree(dtb);
+   kfree(dtb);
return ret;
 }
-- 
2.30.0



[PATCH v15 07/10] kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT

2021-01-15 Thread Lakshmi Ramasubramanian
fdt_appendprop_addrrange() function adds a property, with the given name,
to the device tree at the given node offset, and also sets the address
and size of the property.  This function should be used to add
"linux,ima-kexec-buffer" property to the device tree and set the address
and size of the IMA measurement buffer, instead of using custom function.

Use fdt_appendprop_addrrange() to add  "linux,ima-kexec-buffer" property
to the device tree.  This property holds the address and size of
the IMA measurement buffer that needs to be passed from the current
kernel to the next kernel across kexec system call.

Remove custom code that is used in setup_ima_buffer() to add
"linux,ima-kexec-buffer" property to the device tree.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
 drivers/of/kexec.c | 57 --
 1 file changed, 5 insertions(+), 52 deletions(-)

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 450fa6128001..49183da2fc34 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -210,36 +210,6 @@ int ima_free_kexec_buffer(void)
 }
 
 #ifdef CONFIG_IMA_KEXEC
-/**
- * write_number - Convert number to big-endian format
- *
- * @p: Buffer to write the number to
- * @value: Number to convert
- * @cells: Number of cells
- *
- * Return: 0 on success, or negative errno on error.
- */
-static int write_number(void *p, u64 value, int cells)
-{
-   if (cells == 1) {
-   u32 tmp;
-
-   if (value > U32_MAX)
-   return -EINVAL;
-
-   tmp = cpu_to_be32(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else if (cells == 2) {
-   u64 tmp;
-
-   tmp = cpu_to_be64(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else
-   return -EINVAL;
-
-   return 0;
-}
-
 /**
  * setup_ima_buffer - add IMA buffer information to the fdt
  * @image: kexec image being loaded.
@@ -251,32 +221,15 @@ static int write_number(void *p, u64 value, int cells)
 static int setup_ima_buffer(const struct kimage *image, void *fdt,
int chosen_node)
 {
-   int ret, addr_cells, size_cells, entry_size;
-   u8 value[16];
+   int ret;
 
if (!image->arch.ima_buffer_size)
return 0;
 
-   ret = get_addr_size_cells(&addr_cells, &size_cells);
-   if (ret)
-   return ret;
-
-   entry_size = 4 * (addr_cells + size_cells);
-
-   if (entry_size > sizeof(value))
-   return -EINVAL;
-
-   ret = write_number(value, image->arch.ima_buffer_addr, addr_cells);
-   if (ret)
-   return ret;
-
-   ret = write_number(value + 4 * addr_cells, image->arch.ima_buffer_size,
-  size_cells);
-   if (ret)
-   return ret;
-
-   ret = fdt_setprop(fdt, chosen_node, "linux,ima-kexec-buffer", value,
- entry_size);
+   ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
+  "linux,ima-kexec-buffer",
+  image->arch.ima_buffer_addr,
+  image->arch.ima_buffer_size);
if (ret < 0)
return -EINVAL;
 
-- 
2.30.0



[PATCH v15 08/10] powerpc: Delete unused function delete_fdt_mem_rsv()

2021-01-15 Thread Lakshmi Ramasubramanian
delete_fdt_mem_rsv() defined in "arch/powerpc/kexec/file_load.c"
has been renamed to fdt_find_and_del_mem_rsv(), and moved to
"drivers/of/kexec.c".

Remove delete_fdt_mem_rsv() in "arch/powerpc/kexec/file_load.c".

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h |  1 -
 arch/powerpc/kexec/file_load.c   | 32 
 2 files changed, 33 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 11679fc2cadc..b8c270e08481 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -123,7 +123,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
 struct kexec_buf;
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 5dd3a9c45a2d..036c8fb48fc3 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -108,35 +108,3 @@ int setup_purgatory(struct kimage *image, const void 
*slave_code,
 
return 0;
 }
-
-/**
- * delete_fdt_mem_rsv - delete memory reservation with given address and size
- *
- * Return: 0 on success, or negative errno on error.
- */
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
-{
-   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
-
-   for (i = 0; i < num_rsvs; i++) {
-   uint64_t rsv_start, rsv_size;
-
-   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
-   if (ret) {
-   pr_err("Malformed device tree.\n");
-   return -EINVAL;
-   }
-
-   if (rsv_start == start && rsv_size == size) {
-   ret = fdt_del_mem_rsv(fdt, i);
-   if (ret) {
-   pr_err("Error deleting device tree 
reservation.\n");
-   return -EINVAL;
-   }
-
-   return 0;
-   }
-   }
-
-   return -ENOENT;
-}
-- 
2.30.0



[PATCH v15 10/10] arm64: Add IMA log information in kimage used for kexec

2021-01-15 Thread Lakshmi Ramasubramanian
Address and size of the buffer containing the IMA measurement log need
to be passed from the current kernel to the next kernel on kexec.

Add address and size fields to "struct kimage_arch" for ARM64 platform
to hold the address and size of the IMA measurement log buffer.

Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
 arch/arm64/Kconfig | 1 +
 arch/arm64/include/asm/kexec.h | 5 +
 2 files changed, 6 insertions(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 1d466addb078..ea7f7fe3dccd 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1094,6 +1094,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h
index d24b527e8c00..2bd19ccb6c43 100644
--- a/arch/arm64/include/asm/kexec.h
+++ b/arch/arm64/include/asm/kexec.h
@@ -100,6 +100,11 @@ struct kimage_arch {
void *elf_headers;
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
+
+#ifdef CONFIG_IMA_KEXEC
+   phys_addr_t ima_buffer_addr;
+   size_t ima_buffer_size;
+#endif
 };
 
 extern const struct kexec_file_ops kexec_image_ops;
-- 
2.30.0



[PATCH v15 06/10] powerpc: Move arch independent ima kexec functions to drivers/of/kexec.c

2021-01-15 Thread Lakshmi Ramasubramanian
The functions defined in "arch/powerpc/kexec/ima.c" handle setting up
and freeing the resources required to carry over the IMA measurement
list from the current kernel to the next kernel across kexec system call.
These functions do not have architecture specific code, but are
currently limited to powerpc.

Move setup_ima_buffer() call into of_kexec_setup_new_fdt() defined in
"drivers/of/kexec.c".  Call of_kexec_setup_new_fdt() from
setup_new_fdt_ppc64() and remove setup_new_fdt() in
"arch/powerpc/kexec/file_load.c".

Move the remaining architecture independent functions from
"arch/powerpc/kexec/ima.c" to "drivers/of/kexec.c".
Delete "arch/powerpc/kexec/ima.c" and "arch/powerpc/include/asm/ima.h".
Remove references to the deleted files in powerpc and in ima.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/Kconfig   |   2 +-
 arch/powerpc/include/asm/ima.h |  27 
 arch/powerpc/include/asm/kexec.h   |   3 -
 arch/powerpc/kexec/Makefile|   7 -
 arch/powerpc/kexec/file_load.c |  35 -
 arch/powerpc/kexec/file_load_64.c  |   4 +-
 arch/powerpc/kexec/ima.c   | 202 
 drivers/of/kexec.c | 240 +
 include/linux/of.h |   2 +
 security/integrity/ima/ima.h   |   4 -
 security/integrity/ima/ima_kexec.c |   1 +
 11 files changed, 247 insertions(+), 280 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index e9f13fe08492..4ddd17215ecf 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -548,7 +548,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
-   select HAVE_IMA_KEXEC
+   select HAVE_IMA_KEXEC if IMA
select BUILD_BIN2C
select KEXEC_ELF
depends on PPC64
diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
deleted file mode 100644
index 51f64fd06c19..
--- a/arch/powerpc/include/asm/ima.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _ASM_POWERPC_IMA_H
-#define _ASM_POWERPC_IMA_H
-
-struct kimage;
-
-int ima_get_kexec_buffer(void **addr, size_t *size);
-int ima_free_kexec_buffer(void);
-
-#ifdef CONFIG_IMA
-void remove_ima_buffer(void *fdt, int chosen_node);
-#else
-static inline void remove_ima_buffer(void *fdt, int chosen_node) {}
-#endif
-
-#ifdef CONFIG_IMA_KEXEC
-int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
-#else
-static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
-  int chosen_node)
-{
-   remove_ima_buffer(fdt, chosen_node);
-   return 0;
-}
-#endif /* CONFIG_IMA_KEXEC */
-
-#endif /* _ASM_POWERPC_IMA_H */
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index dbf09d2f36d0..11679fc2cadc 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -123,9 +123,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int setup_new_fdt(const struct kimage *image, void *fdt,
- unsigned long initrd_load_addr, unsigned long initrd_len,
- const char *cmdline);
 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
diff --git a/arch/powerpc/kexec/Makefile b/arch/powerpc/kexec/Makefile
index 4aff6846c772..b6c52608cb49 100644
--- a/arch/powerpc/kexec/Makefile
+++ b/arch/powerpc/kexec/Makefile
@@ -9,13 +9,6 @@ obj-$(CONFIG_PPC32)+= relocate_32.o
 
 obj-$(CONFIG_KEXEC_FILE)   += file_load.o ranges.o file_load_$(BITS).o 
elf_$(BITS).o
 
-ifdef CONFIG_HAVE_IMA_KEXEC
-ifdef CONFIG_IMA
-obj-y  += ima.o
-endif
-endif
-
-
 # Disable GCOV, KCOV & sanitizers in odd or sensitive code
 GCOV_PROFILE_core_$(BITS).o := n
 KCOV_INSTRUMENT_core_$(BITS).o := n
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 956bcb2d1ec2..5dd3a9c45a2d 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -20,7 +20,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #define SLAVE_CODE_SIZE256 /* First 0x100 bytes */
 
@@ -141,37 +140,3 @@ int delete_fdt_mem_rsv(void *fdt, unsigned long start, 
unsigned long size)
 
return -ENOENT;
 }
-
-/*
- * setup_new_fdt - modify /chosen and memory reservation for the next kernel
- * @image: kexec image being loaded.
- * @fdt:   Flattened device tree for the next kernel.
-

[PATCH v15 05/10] ima: Move arch_ima_add_kexec_buffer() to ima

2021-01-15 Thread Lakshmi Ramasubramanian
arch_ima_add_kexec_buffer() defined in "arch/powerpc/kexec/ima.c"
sets up the address and size of the IMA measurement list in
the architecture specific fields in kimage struct.  This function does not
have architecture specific code, but is currently limited to powerpc.

Move arch_ima_add_kexec_buffer() to ima.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Acked-by: Mimi Zohar 
Reviewed-by: Thiago Jung Bauermann 
---
 arch/powerpc/include/asm/ima.h |  3 ---
 arch/powerpc/kexec/ima.c   | 17 -
 security/integrity/ima/ima_kexec.c | 21 +
 3 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
index ead488cf3981..51f64fd06c19 100644
--- a/arch/powerpc/include/asm/ima.h
+++ b/arch/powerpc/include/asm/ima.h
@@ -14,9 +14,6 @@ static inline void remove_ima_buffer(void *fdt, int 
chosen_node) {}
 #endif
 
 #ifdef CONFIG_IMA_KEXEC
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size);
-
 int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
 #else
 static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
diff --git a/arch/powerpc/kexec/ima.c b/arch/powerpc/kexec/ima.c
index 720e50e490b6..7378d59c0c1e 100644
--- a/arch/powerpc/kexec/ima.c
+++ b/arch/powerpc/kexec/ima.c
@@ -128,23 +128,6 @@ void remove_ima_buffer(void *fdt, int chosen_node)
 }
 
 #ifdef CONFIG_IMA_KEXEC
-/**
- * arch_ima_add_kexec_buffer - do arch-specific steps to add the IMA buffer
- *
- * Architectures should use this function to pass on the IMA buffer
- * information to the next kernel.
- *
- * Return: 0 on success, negative errno on error.
- */
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size)
-{
-   image->arch.ima_buffer_addr = load_addr;
-   image->arch.ima_buffer_size = size;
-
-   return 0;
-}
-
 static int write_number(void *p, u64 value, int cells)
 {
if (cells == 1) {
diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 121de3e04af2..95513c97ce8f 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -71,6 +71,27 @@ static int ima_dump_measurement_list(unsigned long 
*buffer_size, void **buffer,
return ret;
 }
 
+/**
+ * arch_ima_add_kexec_buffer - do arch-specific steps to add the IMA buffer
+ *
+ * @image: kimage struct to set IMA buffer data
+ * @load_addr: Starting address where IMA buffer is loaded at
+ * @size: Number of bytes in the IMA buffer
+ *
+ * Architectures should use this function to pass on the IMA buffer
+ * information to the next kernel.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+static int arch_ima_add_kexec_buffer(struct kimage *image,
+unsigned long load_addr, size_t size)
+{
+   image->arch.ima_buffer_addr = load_addr;
+   image->arch.ima_buffer_size = size;
+
+   return 0;
+}
+
 /*
  * Called during kexec_file_load so that IMA can add a segment to the kexec
  * image for the measurement list for the next kernel.
-- 
2.30.0



[PATCH 2/2] ima: Free IMA measurement buffer after kexec syscall

2021-01-21 Thread Lakshmi Ramasubramanian
IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  This buffer is not freed before
completing the kexec system call resulting in memory leak.

Add ima_buffer field in "struct kimage" to store the virtual address
of the buffer allocated for the IMA measurement list.
Free the memory allocated for the IMA measurement list in
kimage_file_post_load_cleanup() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
 include/linux/kexec.h  | 5 +
 kernel/kexec_file.c| 5 +
 security/integrity/ima/ima_kexec.c | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 9e93bef52968..5f61389f5f36 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -300,6 +300,11 @@ struct kimage {
/* Information for loading purgatory */
struct purgatory_info purgatory_info;
 #endif
+
+#ifdef CONFIG_IMA_KEXEC
+   /* Virtual address of IMA measurement buffer for kexec syscall */
+   void *ima_buffer;
+#endif
 };
 
 /* kexec interface functions */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index b02086d70492..5c3447cf7ad5 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -166,6 +166,11 @@ void kimage_file_post_load_cleanup(struct kimage *image)
vfree(pi->sechdrs);
pi->sechdrs = NULL;
 
+#ifdef CONFIG_IMA_KEXEC
+   vfree(image->ima_buffer);
+   image->ima_buffer = NULL;
+#endif /* CONFIG_IMA_KEXEC */
+
/* See if architecture has anything to cleanup post load */
arch_kimage_file_post_load_cleanup(image);
 
diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 212145008a01..8eadd0674629 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -130,6 +130,8 @@ void ima_add_kexec_buffer(struct kimage *image)
return;
}
 
+   image->ima_buffer = kexec_buffer;
+
pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
 kbuf.mem);
 }
-- 
2.30.0



[PATCH 1/2] ima: Free IMA measurement buffer on error

2021-01-21 Thread Lakshmi Ramasubramanian
IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  In error code paths this memory
is not freed resulting in memory leak.

Free the memory allocated for the IMA measurement list in
the error code paths in ima_add_kexec_buffer() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
 security/integrity/ima/ima_kexec.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 121de3e04af2..212145008a01 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -119,12 +119,14 @@ void ima_add_kexec_buffer(struct kimage *image)
ret = kexec_add_buffer(&kbuf);
if (ret) {
pr_err("Error passing over kexec measurement buffer.\n");
+   vfree(kexec_buffer);
return;
}
 
ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
if (ret) {
pr_err("Error passing over kexec measurement buffer.\n");
+   vfree(kexec_buffer);
return;
}
 
-- 
2.30.0



Re: [PATCH v15 10/10] arm64: Add IMA log information in kimage used for kexec

2021-01-27 Thread Lakshmi Ramasubramanian

On 1/27/21 8:54 AM, Will Deacon wrote:

Hi Will,


On Fri, Jan 15, 2021 at 09:30:17AM -0800, Lakshmi Ramasubramanian wrote:

Address and size of the buffer containing the IMA measurement log need
to be passed from the current kernel to the next kernel on kexec.

Add address and size fields to "struct kimage_arch" for ARM64 platform
to hold the address and size of the IMA measurement log buffer.

Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
  arch/arm64/Kconfig | 1 +
  arch/arm64/include/asm/kexec.h | 5 +
  2 files changed, 6 insertions(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 1d466addb078..ea7f7fe3dccd 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1094,6 +1094,7 @@ config KEXEC
  config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h
index d24b527e8c00..2bd19ccb6c43 100644
--- a/arch/arm64/include/asm/kexec.h
+++ b/arch/arm64/include/asm/kexec.h
@@ -100,6 +100,11 @@ struct kimage_arch {
void *elf_headers;
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
+
+#ifdef CONFIG_IMA_KEXEC
+   phys_addr_t ima_buffer_addr;
+   size_t ima_buffer_size;
+#endif


Why do these need to be in the arch structure instead of 'struct kimage'?



Currently, only powerpc and, with this patch set, arm64 have support for 
carrying forward IMA measurement list across kexec system call. The 
above fields are used for tracking IMA measurement list.


Do you see a reason to move these fields to "struct kimage"?

thanks,
 -lakshmi


Re: [PATCH v15 10/10] arm64: Add IMA log information in kimage used for kexec

2021-01-27 Thread Lakshmi Ramasubramanian

On 1/27/21 10:02 AM, Will Deacon wrote:

On Wed, Jan 27, 2021 at 09:56:53AM -0800, Lakshmi Ramasubramanian wrote:

On 1/27/21 8:54 AM, Will Deacon wrote:

On Fri, Jan 15, 2021 at 09:30:17AM -0800, Lakshmi Ramasubramanian wrote:

Address and size of the buffer containing the IMA measurement log need
to be passed from the current kernel to the next kernel on kexec.

Add address and size fields to "struct kimage_arch" for ARM64 platform
to hold the address and size of the IMA measurement log buffer.

Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
   arch/arm64/Kconfig | 1 +
   arch/arm64/include/asm/kexec.h | 5 +
   2 files changed, 6 insertions(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 1d466addb078..ea7f7fe3dccd 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1094,6 +1094,7 @@ config KEXEC
   config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h
index d24b527e8c00..2bd19ccb6c43 100644
--- a/arch/arm64/include/asm/kexec.h
+++ b/arch/arm64/include/asm/kexec.h
@@ -100,6 +100,11 @@ struct kimage_arch {
void *elf_headers;
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
+
+#ifdef CONFIG_IMA_KEXEC
+   phys_addr_t ima_buffer_addr;
+   size_t ima_buffer_size;
+#endif


Why do these need to be in the arch structure instead of 'struct kimage'?



Currently, only powerpc and, with this patch set, arm64 have support for
carrying forward IMA measurement list across kexec system call. The above
fields are used for tracking IMA measurement list.

Do you see a reason to move these fields to "struct kimage"?


If they're gated on CONFIG_IMA_KEXEC, then it seems harmless for them to
be added to the shared structure. Or are you saying that there are
architectures which have CONFIG_IMA_KEXEC but do not want these fields?



As far as I know, there are no other architectures that define 
CONFIG_IMA_KEXEC, but do not use these fields.


Mimi - please correct me if I am wrong.

thanks,
 -lakshmi






Re: [PATCH v15 09/10] arm64: Call kmalloc() to allocate DTB buffer

2021-01-27 Thread Lakshmi Ramasubramanian

On 1/27/21 7:52 PM, Thiago Jung Bauermann wrote:


Will Deacon  writes:


On Wed, Jan 27, 2021 at 09:59:38AM -0800, Lakshmi Ramasubramanian wrote:

On 1/27/21 8:52 AM, Will Deacon wrote:

Hi Will,


On Fri, Jan 15, 2021 at 09:30:16AM -0800, Lakshmi Ramasubramanian wrote:

create_dtb() function allocates kernel virtual memory for
the device tree blob (DTB).  This is not consistent with other
architectures, such as powerpc, which calls kmalloc() for allocating
memory for the DTB.

Call kmalloc() to allocate memory for the DTB, and kfree() to free
the allocated memory.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
   arch/arm64/kernel/machine_kexec_file.c | 12 +++-
   1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 7de9c47dee7c..51c40143d6fa 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -29,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
   int arch_kimage_file_post_load_cleanup(struct kimage *image)
   {
-   vfree(image->arch.dtb);
+   kfree(image->arch.dtb);
image->arch.dtb = NULL;
vfree(image->arch.elf_headers);
@@ -59,19 +59,21 @@ static int create_dtb(struct kimage *image,
+ cmdline_len + DTB_EXTRA_SPACE;
for (;;) {
-   buf = vmalloc(buf_size);
+   buf = kmalloc(buf_size, GFP_KERNEL);


Is there a functional need for this patch? I build the 'dtbs' target just
now and sdm845-db845c.dtb is approaching 100K, which feels quite large
for kmalloc().


Changing the allocation from vmalloc() to kmalloc() would help us further
consolidate the DTB setup code for powerpc and arm64.


Ok, but at the risk of allocation failure. Can powerpc use vmalloc()
instead?


I believe this patch stems from this suggestion by Rob Herring:


This could be taken a step further and do the allocation of the new
FDT. The difference is arm64 uses vmalloc and powerpc uses kmalloc. The
arm64 version also retries with a bigger allocation. That seems
unnecessary.


in 
https://lore.kernel.org/linux-integrity/20201211221006.1052453-3-r...@kernel.org/

The problem is that this patch implements only part of the suggestion,
which isn't useful in itself. So the patch series should either drop
this patch or consolidate the FDT allocation between the arches.

I just tested on powernv and pseries platforms and powerpc can use
vmalloc for the FDT buffer.



Thanks for verifying on powerpc platform Thiago.

I'll update the patch to do the following:

=> Use vmalloc for FDT buffer allocation on powerpc
=> Keep vmalloc for arm64, but remove the retry on allocation.
=> Also, there was a memory leak of FDT buffer in the error code path on 
arm64, which I'll fix as well.


Did I miss anything?

thanks,
 -lakshmi


Re: [PATCH v15 10/10] arm64: Add IMA log information in kimage used for kexec

2021-01-27 Thread Lakshmi Ramasubramanian

On 1/27/21 3:13 PM, Will Deacon wrote:

On Wed, Jan 27, 2021 at 01:31:02PM -0500, Mimi Zohar wrote:

On Wed, 2021-01-27 at 10:24 -0800, Lakshmi Ramasubramanian wrote:

On 1/27/21 10:02 AM, Will Deacon wrote:

On Wed, Jan 27, 2021 at 09:56:53AM -0800, Lakshmi Ramasubramanian wrote:

On 1/27/21 8:54 AM, Will Deacon wrote:

On Fri, Jan 15, 2021 at 09:30:17AM -0800, Lakshmi Ramasubramanian wrote:

Address and size of the buffer containing the IMA measurement log need
to be passed from the current kernel to the next kernel on kexec.

Add address and size fields to "struct kimage_arch" for ARM64 platform
to hold the address and size of the IMA measurement log buffer.

Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
arch/arm64/Kconfig | 1 +
arch/arm64/include/asm/kexec.h | 5 +
2 files changed, 6 insertions(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 1d466addb078..ea7f7fe3dccd 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1094,6 +1094,7 @@ config KEXEC
config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h
index d24b527e8c00..2bd19ccb6c43 100644
--- a/arch/arm64/include/asm/kexec.h
+++ b/arch/arm64/include/asm/kexec.h
@@ -100,6 +100,11 @@ struct kimage_arch {
void *elf_headers;
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
+
+#ifdef CONFIG_IMA_KEXEC
+   phys_addr_t ima_buffer_addr;
+   size_t ima_buffer_size;
+#endif


Why do these need to be in the arch structure instead of 'struct kimage'?



Currently, only powerpc and, with this patch set, arm64 have support for
carrying forward IMA measurement list across kexec system call. The above
fields are used for tracking IMA measurement list.

Do you see a reason to move these fields to "struct kimage"?


If they're gated on CONFIG_IMA_KEXEC, then it seems harmless for them to
be added to the shared structure. Or are you saying that there are
architectures which have CONFIG_IMA_KEXEC but do not want these fields?



As far as I know, there are no other architectures that define
CONFIG_IMA_KEXEC, but do not use these fields.


Yes, CONFIG_IMA_KEXEC enables "carrying the IMA measurement list across
a soft boot".   The only arch that currently carries the IMA
measurement across kexec is powerpc.


Ok, in which case this sounds like it should be in the shared structure, no?



Ok - I'll move the IMA kexec buffer fields from "struct kimage_arch" to 
"struct kimage" for both powerpc and arm64.


thanks,
 -lakshmi




Re: [PATCH v15 09/10] arm64: Call kmalloc() to allocate DTB buffer

2021-01-27 Thread Lakshmi Ramasubramanian

On 1/27/21 8:14 PM, Thiago Jung Bauermann wrote:


Lakshmi Ramasubramanian  writes:


On 1/27/21 7:52 PM, Thiago Jung Bauermann wrote:

Will Deacon  writes:


On Wed, Jan 27, 2021 at 09:59:38AM -0800, Lakshmi Ramasubramanian wrote:

On 1/27/21 8:52 AM, Will Deacon wrote:

Hi Will,


On Fri, Jan 15, 2021 at 09:30:16AM -0800, Lakshmi Ramasubramanian wrote:

create_dtb() function allocates kernel virtual memory for
the device tree blob (DTB).  This is not consistent with other
architectures, such as powerpc, which calls kmalloc() for allocating
memory for the DTB.

Call kmalloc() to allocate memory for the DTB, and kfree() to free
the allocated memory.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
arch/arm64/kernel/machine_kexec_file.c | 12 +++-
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 7de9c47dee7c..51c40143d6fa 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -29,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
int arch_kimage_file_post_load_cleanup(struct kimage *image)
{
-   vfree(image->arch.dtb);
+   kfree(image->arch.dtb);
image->arch.dtb = NULL;
vfree(image->arch.elf_headers);
@@ -59,19 +59,21 @@ static int create_dtb(struct kimage *image,
+ cmdline_len + DTB_EXTRA_SPACE;
for (;;) {
-   buf = vmalloc(buf_size);
+   buf = kmalloc(buf_size, GFP_KERNEL);


Is there a functional need for this patch? I build the 'dtbs' target just
now and sdm845-db845c.dtb is approaching 100K, which feels quite large
for kmalloc().


Changing the allocation from vmalloc() to kmalloc() would help us further
consolidate the DTB setup code for powerpc and arm64.


Ok, but at the risk of allocation failure. Can powerpc use vmalloc()
instead?

I believe this patch stems from this suggestion by Rob Herring:


This could be taken a step further and do the allocation of the new
FDT. The difference is arm64 uses vmalloc and powerpc uses kmalloc. The
arm64 version also retries with a bigger allocation. That seems
unnecessary.

in
https://lore.kernel.org/linux-integrity/20201211221006.1052453-3-r...@kernel.org/
The problem is that this patch implements only part of the suggestion,
which isn't useful in itself. So the patch series should either drop
this patch or consolidate the FDT allocation between the arches.
I just tested on powernv and pseries platforms and powerpc can use
vmalloc for the FDT buffer.



Thanks for verifying on powerpc platform Thiago.

I'll update the patch to do the following:

=> Use vmalloc for FDT buffer allocation on powerpc
=> Keep vmalloc for arm64, but remove the retry on allocation.
=> Also, there was a memory leak of FDT buffer in the error code path on arm64,
which I'll fix as well.

Did I miss anything?


Yes, you missed the second part of Rob's suggestion I was mentioning,
which is factoring out the code which allocates the new FDT from both
arm64 and powerpc.



Sure - I'll address that.

thanks,
 -lakshmi



Re: [PATCH 2/2] ima: Free IMA measurement buffer after kexec syscall

2021-02-03 Thread Lakshmi Ramasubramanian

On 1/22/21 2:31 PM, Thiago Jung Bauermann wrote:


Lakshmi Ramasubramanian  writes:


IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  This buffer is not freed before
completing the kexec system call resulting in memory leak.

Add ima_buffer field in "struct kimage" to store the virtual address
of the buffer allocated for the IMA measurement list.
Free the memory allocated for the IMA measurement list in
kimage_file_post_load_cleanup() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")


Good catch.

Reviewed-by: Thiago Jung Bauermann 



Thanks Thiago.

 -lakshmi



Re: [PATCH 1/2] ima: Free IMA measurement buffer on error

2021-02-03 Thread Lakshmi Ramasubramanian

On 1/22/21 2:30 PM, Thiago Jung Bauermann wrote:


Hi Lakshmi,

Lakshmi Ramasubramanian  writes:


IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  In error code paths this memory
is not freed resulting in memory leak.

Free the memory allocated for the IMA measurement list in
the error code paths in ima_add_kexec_buffer() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
  security/integrity/ima/ima_kexec.c | 2 ++
  1 file changed, 2 insertions(+)

diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 121de3e04af2..212145008a01 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -119,12 +119,14 @@ void ima_add_kexec_buffer(struct kimage *image)
ret = kexec_add_buffer(&kbuf);
if (ret) {
pr_err("Error passing over kexec measurement buffer.\n");
+   vfree(kexec_buffer);
return;
}


This is a good catch.


Thanks.



  
  	ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);

if (ret) {
pr_err("Error passing over kexec measurement buffer.\n");
+   vfree(kexec_buffer);
return;
}


But this would cause problems, because the buffer is still there in the
kimage and would cause kimage_load_segment() to access invalid memory.

There's no function to undo a kexec_add_buffer() to avoid this problem,
so I'd suggest just accepting the leak in this case. Fortunately, the
current implementations of arch_ima_add_kexec_buffer() are very simple
and cannot fail, so this is a theoretical problem.



Agreed. I'll post a new patch with the above change removed.

thanks,
 -lakshmi



[PATCH v16 02/12] of: Add a common kexec FDT setup function

2021-02-04 Thread Lakshmi Ramasubramanian
From: Rob Herring 

Both arm64 and powerpc do essentially the same FDT /chosen setup for
kexec.  The differences are either omissions that arm64 should have
or additional properties that will be ignored.  The setup code can be
combined and shared by both powerpc and arm64.

The differences relative to the arm64 version:
 - If /chosen doesn't exist, it will be created (should never happen).
 - Any old dtb and initrd reserved memory will be released.
 - The new initrd and elfcorehdr are marked reserved.
 - "linux,booted-from-kexec" is set.

The differences relative to the powerpc version:
 - "kaslr-seed" and "rng-seed" may be set.
 - "linux,elfcorehdr" is set.
 - Any existing "linux,usable-memory-range" is removed.

Combine the code for setting up the /chosen node in the FDT and updating
the memory reservation for kexec, for powerpc and arm64, in
of_kexec_setup_new_fdt() and move it to "drivers/of/kexec.c".

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 drivers/of/Makefile |   1 +
 drivers/of/kexec.c  | 236 
 include/linux/of.h  |   4 +
 3 files changed, 241 insertions(+)
 create mode 100644 drivers/of/kexec.c

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 6e1e5212f058..8ce11955afde 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -13,5 +13,6 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o
 obj-$(CONFIG_OF_RESOLVE)  += resolver.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o
 obj-$(CONFIG_OF_NUMA) += of_numa.o
+obj-$(CONFIG_KEXEC_FILE) += kexec.o
 
 obj-$(CONFIG_OF_UNITTEST) += unittest-data/
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
new file mode 100644
index ..4afd3cc1c04a
--- /dev/null
+++ b/drivers/of/kexec.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Arm Limited
+ *
+ * Based on arch/arm64/kernel/machine_kexec_file.c:
+ *  Copyright (C) 2018 Linaro Limited
+ *
+ * And arch/powerpc/kexec/file_load.c:
+ *  Copyright (C) 2016  IBM Corporation
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* relevant device tree properties */
+#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
+#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
+#define FDT_PROP_INITRD_START  "linux,initrd-start"
+#define FDT_PROP_INITRD_END"linux,initrd-end"
+#define FDT_PROP_BOOTARGS  "bootargs"
+#define FDT_PROP_KASLR_SEED"kaslr-seed"
+#define FDT_PROP_RNG_SEED  "rng-seed"
+#define RNG_SEED_SIZE  128
+
+/**
+ * fdt_find_and_del_mem_rsv - delete memory reservation with given address and 
size
+ *
+ * @fdt:   Flattened device tree for the current kernel.
+ * @start: Starting address of the reserved memory.
+ * @size:  Size of the reserved memory.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned 
long size)
+{
+   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
+
+   for (i = 0; i < num_rsvs; i++) {
+   u64 rsv_start, rsv_size;
+
+   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
+   if (ret) {
+   pr_err("Malformed device tree.\n");
+   return -EINVAL;
+   }
+
+   if (rsv_start == start && rsv_size == size) {
+   ret = fdt_del_mem_rsv(fdt, i);
+   if (ret) {
+   pr_err("Error deleting device tree 
reservation.\n");
+   return -EINVAL;
+   }
+
+   return 0;
+   }
+   }
+
+   return -ENOENT;
+}
+
+/*
+ * of_kexec_setup_new_fdt - modify /chosen and memory reservation for the next 
kernel
+ *
+ * @image: kexec image being loaded.
+ * @fdt:   Flattened device tree for the next kernel.
+ * @initrd_load_addr:  Address where the next initrd will be loaded.
+ * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:   Command line for the next kernel, or NULL if there will
+ * be none.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+int of_kexec_setup_new_fdt(const struct kimage *image, void *fdt,
+  unsigned long initrd_load_addr, unsigned long 
initrd_len,
+  const char *cmdline)
+{
+   int ret, chosen_node;
+   const void *prop;
+
+   /* Remove memory reservation for the current device tree. */
+   ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
+  fdt_totalsize(initial_boot_params));
+   if (ret == -EINVAL)
+ 

[PATCH v16 01/12] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem

2021-02-04 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The architecture specific field, elfcorehdr_addr in struct kimage_arch,
that holds the address of the buffer in memory for ELF core header for
powerpc has a different name than the one used for arm64.  This makes
it hard to have a common code for setting up the device tree for
kexec system call.

Rename elfcorehdr_addr to elf_headers_mem to align with arm64 name so
common code can use it.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h  | 2 +-
 arch/powerpc/kexec/file_load.c| 4 ++--
 arch/powerpc/kexec/file_load_64.c | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 55d6ede30c19..dbf09d2f36d0 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -108,7 +108,7 @@ struct kimage_arch {
unsigned long backup_start;
void *backup_buf;
 
-   unsigned long elfcorehdr_addr;
+   unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
 
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 9a232bc36c8f..e452b11df631 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -45,7 +45,7 @@ char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
return NULL;
 
elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
-   image->arch.elfcorehdr_addr);
+   image->arch.elf_headers_mem);
 
if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
pr_err("Appending elfcorehdr= exceeds cmdline size\n");
@@ -263,7 +263,7 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
 * Avoid elfcorehdr from being stomped on in kdump kernel by
 * setting up memory reserve map.
 */
-   ret = fdt_add_mem_rsv(fdt, image->arch.elfcorehdr_addr,
+   ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
  image->arch.elf_headers_sz);
if (ret) {
pr_err("Error reserving elfcorehdr memory: %s\n",
diff --git a/arch/powerpc/kexec/file_load_64.c 
b/arch/powerpc/kexec/file_load_64.c
index c69bcf9b547a..a05c19b3cc60 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -815,7 +815,7 @@ static int load_elfcorehdr_segment(struct kimage *image, 
struct kexec_buf *kbuf)
goto out;
}
 
-   image->arch.elfcorehdr_addr = kbuf->mem;
+   image->arch.elf_headers_mem = kbuf->mem;
image->arch.elf_headers_sz = headers_sz;
image->arch.elf_headers = headers;
 out:
@@ -851,7 +851,7 @@ int load_crashdump_segments_ppc64(struct kimage *image,
return ret;
}
pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
-image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz);
+image->arch.elf_headers_mem, kbuf->bufsz, kbuf->memsz);
 
return 0;
 }
-- 
2.30.0



[PATCH v16 00/12] Carry forward IMA measurement log on kexec on ARM64

2021-02-04 Thread Lakshmi Ramasubramanian
l code in get_addr_size_cells() and
do_get_kexec_buffer() for powerpc.
  - Added fdt_add_mem_rsv() for ARM64 to reserve the memory for
the IMA log buffer during kexec.
  - Fixed the warning reported by kernel test bot for ARM64
arch_ima_add_kexec_buffer() - moved this function to a new file
namely arch/arm64/kernel/ima_kexec.c

v4:
  - Submitting the patch series on behalf of the original author
Prakhar Srivastava 
  - Moved FDT_PROP_IMA_KEXEC_BUFFER ("linux,ima-kexec-buffer") to
libfdt.h so that it can be shared by multiple platforms.

v3:
Breakup patches further into separate patches.
  - Refactoring non architecture specific code out of powerpc
  - Update powerpc related code to use fdt functions
  - Update IMA buffer read related code to use of functions
  - Add support to store the memory information of the IMA
measurement logs to be carried forward.
  - Update the property strings to align with documented nodes
https://github.com/devicetree-org/dt-schema/pull/46

v2:
  Break patches into separate patches.
  - Powerpc related Refactoring
  - Updating the docuemntation for chosen node
  - Updating arm64 to support IMA buffer pass

v1:
  Refactoring carrying over IMA measuremnet logs over Kexec. This patch
moves the non-architecture specific code out of powerpc and adds to
security/ima.(Suggested by Thiago)
  Add Documentation regarding the ima-kexec-buffer node in the chosen
node documentation

v0:
  Add a layer of abstraction to use the memory reserved by device tree
for ima buffer pass.
  Add support for ima buffer pass using reserved memory for arm64 kexec.
Update the arch sepcific code path in kexec file load to store the
ima buffer in the reserved memory. The same reserved memory is read
on kexec or cold boot.


Lakshmi Ramasubramanian (8):
  powerpc: Move ima buffer fields to struct kimage
  powerpc: Move arch independent ima kexec functions to
drivers/of/kexec.c
  kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT
  powerpc: Delete unused function delete_fdt_mem_rsv()
  of: Define functions to allocate and free FDT
  arm64: Use OF alloc and free functions for FDT
  powerpc: Use OF alloc and free for FDT
  arm64: Enable passing IMA log to next kernel on kexec

Rob Herring (4):
  powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem
  of: Add a common kexec FDT setup function
  arm64: Use common of_kexec_setup_new_fdt()
  powerpc: Use common of_kexec_setup_new_fdt()

 arch/arm64/Kconfig |   1 +
 arch/arm64/kernel/machine_kexec_file.c | 158 +---
 arch/powerpc/Kconfig   |   2 +-
 arch/powerpc/include/asm/ima.h |  30 --
 arch/powerpc/include/asm/kexec.h   |  11 +-
 arch/powerpc/kexec/Makefile|   7 -
 arch/powerpc/kexec/elf_64.c|  26 +-
 arch/powerpc/kexec/file_load.c | 184 +-
 arch/powerpc/kexec/file_load_64.c  |  11 +-
 arch/powerpc/kexec/ima.c   | 219 ---
 drivers/of/Makefile|   1 +
 drivers/of/kexec.c | 488 +
 include/linux/kexec.h  |   5 +
 include/linux/of.h |  15 +-
 security/integrity/ima/ima.h   |   4 -
 security/integrity/ima/ima_kexec.c |   3 +-
 16 files changed, 552 insertions(+), 613 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c
 create mode 100644 drivers/of/kexec.c

-- 
2.30.0



[PATCH v16 03/12] arm64: Use common of_kexec_setup_new_fdt()

2021-02-04 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_setup_new_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_setup_new_fdt() to setup the device tree
and update the memory reservation for kexec for arm64.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
Acked-by: Will Deacon 
---
 arch/arm64/kernel/machine_kexec_file.c | 123 +
 1 file changed, 3 insertions(+), 120 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 03210f644790..7da22bb7b9d5 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -15,23 +15,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
-
-/* relevant device tree properties */
-#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
-#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
-#define FDT_PROP_INITRD_START  "linux,initrd-start"
-#define FDT_PROP_INITRD_END"linux,initrd-end"
-#define FDT_PROP_BOOTARGS  "bootargs"
-#define FDT_PROP_KASLR_SEED"kaslr-seed"
-#define FDT_PROP_RNG_SEED  "rng-seed"
-#define RNG_SEED_SIZE  128
 
 const struct kexec_file_ops * const kexec_file_loaders[] = {
&kexec_image_ops,
@@ -50,112 +39,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
return kexec_image_post_load_cleanup_default(image);
 }
 
-static int setup_dtb(struct kimage *image,
-unsigned long initrd_load_addr, unsigned long initrd_len,
-char *cmdline, void *dtb)
-{
-   int off, ret;
-
-   ret = fdt_path_offset(dtb, "/chosen");
-   if (ret < 0)
-   goto out;
-
-   off = ret;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-   ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-
-   if (image->type == KEXEC_TYPE_CRASH) {
-   /* add linux,elfcorehdr */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_KEXEC_ELFHDR,
-   image->arch.elf_headers_mem,
-   image->arch.elf_headers_sz);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-
-   /* add linux,usable-memory-range */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_MEM_RANGE,
-   crashk_res.start,
-   crashk_res.end - crashk_res.start + 1);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-   }
-
-   /* add bootargs */
-   if (cmdline) {
-   ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add initrd-* */
-   if (initrd_load_addr) {
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
- initrd_load_addr);
-   if (ret)
-   goto out;
-
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
- initrd_load_addr + initrd_len);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add kaslr-seed */
-   ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
-   if (ret == -FDT_ERR_NOTFOUND)
-   ret = 0;
-   else if (ret)
-   goto out;
-
-   if (rng_is_initialized()) {
-   u64 seed = get_random_u64();
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
-   if (ret)
-   goto out;
-   } else {
-   pr_notice("RNG is not initialised: omitting \"%s\" property\n",
-   FDT_PROP_KASLR_SEED);
-   }
-
-   /* add rng-seed */
-   if (rng_is_initialized())

[PATCH v16 04/12] powerpc: Use common of_kexec_setup_new_fdt()

2021-02-04 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_setup_new_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_setup_new_fdt() to setup the device tree
and update the memory reservation for kexec for powerpc.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/kexec/file_load.c | 125 ++---
 1 file changed, 6 insertions(+), 119 deletions(-)

diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index e452b11df631..956bcb2d1ec2 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -16,6 +16,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -156,132 +157,18 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
  unsigned long initrd_load_addr, unsigned long initrd_len,
  const char *cmdline)
 {
-   int ret, chosen_node;
-   const void *prop;
-
-   /* Remove memory reservation for the current device tree. */
-   ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
-fdt_totalsize(initial_boot_params));
-   if (ret == 0)
-   pr_debug("Removed old device tree reservation.\n");
-   else if (ret != -ENOENT)
-   return ret;
-
-   chosen_node = fdt_path_offset(fdt, "/chosen");
-   if (chosen_node == -FDT_ERR_NOTFOUND) {
-   chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
- "chosen");
-   if (chosen_node < 0) {
-   pr_err("Error creating /chosen.\n");
-   return -EINVAL;
-   }
-   } else if (chosen_node < 0) {
-   pr_err("Malformed device tree: error reading /chosen.\n");
-   return -EINVAL;
-   }
-
-   /* Did we boot using an initrd? */
-   prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
-   if (prop) {
-   uint64_t tmp_start, tmp_end, tmp_size;
-
-   tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-   prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
-   if (!prop) {
-   pr_err("Malformed device tree.\n");
-   return -EINVAL;
-   }
-   tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-   /*
-* kexec reserves exact initrd size, while firmware may
-* reserve a multiple of PAGE_SIZE, so check for both.
-*/
-   tmp_size = tmp_end - tmp_start;
-   ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
-   if (ret == -ENOENT)
-   ret = delete_fdt_mem_rsv(fdt, tmp_start,
-round_up(tmp_size, PAGE_SIZE));
-   if (ret == 0)
-   pr_debug("Removed old initrd reservation.\n");
-   else if (ret != -ENOENT)
-   return ret;
-
-   /* If there's no new initrd, delete the old initrd's info. */
-   if (initrd_len == 0) {
-   ret = fdt_delprop(fdt, chosen_node,
- "linux,initrd-start");
-   if (ret) {
-   pr_err("Error deleting linux,initrd-start.\n");
-   return -EINVAL;
-   }
-
-   ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
-   if (ret) {
-   pr_err("Error deleting linux,initrd-end.\n");
-   return -EINVAL;
-   }
-   }
-   }
-
-   if (initrd_len) {
-   ret = fdt_setprop_u64(fdt, chosen_node,
- "linux,initrd-start",
- initrd_load_addr);
-   if (ret < 0)
-   goto err;
-
-   /* initrd-end is the first address after the initrd image. */
-   ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
- initrd_load_addr + initrd_len);
-   if (ret < 0)
-   goto err;
-
-   ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
-   if (ret) {
-   pr_err("Error reserving initrd memory: %s\n",
-  fdt_strerror(ret));
-   return -EINVAL;
-   }

[PATCH v16 05/12] powerpc: Move ima buffer fields to struct kimage

2021-02-04 Thread Lakshmi Ramasubramanian
The fields ima_buffer_addr and ima_buffer_size in "struct kimage_arch"
for powerpc are used to carry forward the IMA measurement list across
kexec system call.  These fields are not architecture specific, but are
currently limited to powerpc.

arch_ima_add_kexec_buffer() defined in "arch/powerpc/kexec/ima.c"
sets ima_buffer_addr and ima_buffer_size for the kexec system call.
This function does not have architecture specific code, but is
currently limited to powerpc.

Move ima_buffer_addr and ima_buffer_size to "struct kimage".
Rename arch_ima_add_kexec_buffer() to of_ima_add_kexec_buffer()
and move it in drivers/of/kexec.c.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Will Deacon 
---
 arch/powerpc/include/asm/ima.h |  3 ---
 arch/powerpc/include/asm/kexec.h   |  5 -
 arch/powerpc/kexec/ima.c   | 29 ++---
 drivers/of/kexec.c | 23 +++
 include/linux/kexec.h  |  5 +
 include/linux/of.h |  5 +
 security/integrity/ima/ima_kexec.c |  3 ++-
 7 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
index ead488cf3981..51f64fd06c19 100644
--- a/arch/powerpc/include/asm/ima.h
+++ b/arch/powerpc/include/asm/ima.h
@@ -14,9 +14,6 @@ static inline void remove_ima_buffer(void *fdt, int 
chosen_node) {}
 #endif
 
 #ifdef CONFIG_IMA_KEXEC
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size);
-
 int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
 #else
 static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index dbf09d2f36d0..2248dc27080b 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,11 +111,6 @@ struct kimage_arch {
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
-
-#ifdef CONFIG_IMA_KEXEC
-   phys_addr_t ima_buffer_addr;
-   size_t ima_buffer_size;
-#endif
 };
 
 char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
diff --git a/arch/powerpc/kexec/ima.c b/arch/powerpc/kexec/ima.c
index 720e50e490b6..ed38125e2f87 100644
--- a/arch/powerpc/kexec/ima.c
+++ b/arch/powerpc/kexec/ima.c
@@ -128,23 +128,6 @@ void remove_ima_buffer(void *fdt, int chosen_node)
 }
 
 #ifdef CONFIG_IMA_KEXEC
-/**
- * arch_ima_add_kexec_buffer - do arch-specific steps to add the IMA buffer
- *
- * Architectures should use this function to pass on the IMA buffer
- * information to the next kernel.
- *
- * Return: 0 on success, negative errno on error.
- */
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size)
-{
-   image->arch.ima_buffer_addr = load_addr;
-   image->arch.ima_buffer_size = size;
-
-   return 0;
-}
-
 static int write_number(void *p, u64 value, int cells)
 {
if (cells == 1) {
@@ -180,7 +163,7 @@ int setup_ima_buffer(const struct kimage *image, void *fdt, 
int chosen_node)
u8 value[16];
 
remove_ima_buffer(fdt, chosen_node);
-   if (!image->arch.ima_buffer_size)
+   if (!image->ima_buffer_size)
return 0;
 
ret = get_addr_size_cells(&addr_cells, &size_cells);
@@ -192,11 +175,11 @@ int setup_ima_buffer(const struct kimage *image, void 
*fdt, int chosen_node)
if (entry_size > sizeof(value))
return -EINVAL;
 
-   ret = write_number(value, image->arch.ima_buffer_addr, addr_cells);
+   ret = write_number(value, image->ima_buffer_addr, addr_cells);
if (ret)
return ret;
 
-   ret = write_number(value + 4 * addr_cells, image->arch.ima_buffer_size,
+   ret = write_number(value + 4 * addr_cells, image->ima_buffer_size,
   size_cells);
if (ret)
return ret;
@@ -206,13 +189,13 @@ int setup_ima_buffer(const struct kimage *image, void 
*fdt, int chosen_node)
if (ret < 0)
return -EINVAL;
 
-   ret = fdt_add_mem_rsv(fdt, image->arch.ima_buffer_addr,
- image->arch.ima_buffer_size);
+   ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr,
+ image->ima_buffer_size);
if (ret)
return -EINVAL;
 
pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n",
-image->arch.ima_buffer_addr, image->arch.ima_buffer_size);
+image->ima_buffer_addr, image->ima_buffer_size);
 
return 0;
 }
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 4afd3cc1c04a..efbf54f164fd 100644
--- a/drivers/of/kexec.c
+++ b/dri

[PATCH v16 06/12] powerpc: Move arch independent ima kexec functions to drivers/of/kexec.c

2021-02-04 Thread Lakshmi Ramasubramanian
The functions defined in "arch/powerpc/kexec/ima.c" handle setting up
and freeing the resources required to carry over the IMA measurement
list from the current kernel to the next kernel across kexec system call.
These functions do not have architecture specific code, but are
currently limited to powerpc.

Move setup_ima_buffer() call into of_kexec_setup_new_fdt() defined in
"drivers/of/kexec.c".  Call of_kexec_setup_new_fdt() from
setup_new_fdt_ppc64() and remove setup_new_fdt() in
"arch/powerpc/kexec/file_load.c".

Move the remaining architecture independent functions from
"arch/powerpc/kexec/ima.c" to "drivers/of/kexec.c".
Delete "arch/powerpc/kexec/ima.c" and "arch/powerpc/include/asm/ima.h".
Remove references to the deleted files in powerpc and in ima.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/Kconfig  |   2 +-
 arch/powerpc/include/asm/ima.h|  27 
 arch/powerpc/include/asm/kexec.h  |   3 -
 arch/powerpc/kexec/Makefile   |   7 -
 arch/powerpc/kexec/file_load.c|  35 -
 arch/powerpc/kexec/file_load_64.c |   4 +-
 arch/powerpc/kexec/ima.c  | 202 -
 drivers/of/kexec.c| 239 ++
 include/linux/of.h|   2 +
 security/integrity/ima/ima.h  |   4 -
 10 files changed, 245 insertions(+), 280 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 107bb4319e0e..d6e593ad270e 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -554,7 +554,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
-   select HAVE_IMA_KEXEC
+   select HAVE_IMA_KEXEC if IMA
select BUILD_BIN2C
select KEXEC_ELF
depends on PPC64
diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
deleted file mode 100644
index 51f64fd06c19..
--- a/arch/powerpc/include/asm/ima.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _ASM_POWERPC_IMA_H
-#define _ASM_POWERPC_IMA_H
-
-struct kimage;
-
-int ima_get_kexec_buffer(void **addr, size_t *size);
-int ima_free_kexec_buffer(void);
-
-#ifdef CONFIG_IMA
-void remove_ima_buffer(void *fdt, int chosen_node);
-#else
-static inline void remove_ima_buffer(void *fdt, int chosen_node) {}
-#endif
-
-#ifdef CONFIG_IMA_KEXEC
-int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
-#else
-static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
-  int chosen_node)
-{
-   remove_ima_buffer(fdt, chosen_node);
-   return 0;
-}
-#endif /* CONFIG_IMA_KEXEC */
-
-#endif /* _ASM_POWERPC_IMA_H */
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 2248dc27080b..939bc40dfa62 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -118,9 +118,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int setup_new_fdt(const struct kimage *image, void *fdt,
- unsigned long initrd_load_addr, unsigned long initrd_len,
- const char *cmdline);
 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
diff --git a/arch/powerpc/kexec/Makefile b/arch/powerpc/kexec/Makefile
index 4aff6846c772..b6c52608cb49 100644
--- a/arch/powerpc/kexec/Makefile
+++ b/arch/powerpc/kexec/Makefile
@@ -9,13 +9,6 @@ obj-$(CONFIG_PPC32)+= relocate_32.o
 
 obj-$(CONFIG_KEXEC_FILE)   += file_load.o ranges.o file_load_$(BITS).o 
elf_$(BITS).o
 
-ifdef CONFIG_HAVE_IMA_KEXEC
-ifdef CONFIG_IMA
-obj-y  += ima.o
-endif
-endif
-
-
 # Disable GCOV, KCOV & sanitizers in odd or sensitive code
 GCOV_PROFILE_core_$(BITS).o := n
 KCOV_INSTRUMENT_core_$(BITS).o := n
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 956bcb2d1ec2..5dd3a9c45a2d 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -20,7 +20,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #define SLAVE_CODE_SIZE256 /* First 0x100 bytes */
 
@@ -141,37 +140,3 @@ int delete_fdt_mem_rsv(void *fdt, unsigned long start, 
unsigned long size)
 
return -ENOENT;
 }
-
-/*
- * setup_new_fdt - modify /chosen and memory reservation for the next kernel
- * @image: kexec image being loaded.
- * @fdt:   Flattened device tree for the next kernel.
- * @initrd_load_addr:  Address where the next ini

[PATCH v16 07/12] kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT

2021-02-04 Thread Lakshmi Ramasubramanian
fdt_appendprop_addrrange() function adds a property, with the given name,
to the device tree at the given node offset, and also sets the address
and size of the property.  This function should be used to add
"linux,ima-kexec-buffer" property to the device tree and set the address
and size of the IMA measurement buffer, instead of using custom function.

Use fdt_appendprop_addrrange() to add  "linux,ima-kexec-buffer" property
to the device tree.  This property holds the address and size of
the IMA measurement buffer that needs to be passed from the current
kernel to the next kernel across kexec system call.

Remove custom code that is used in setup_ima_buffer() to add
"linux,ima-kexec-buffer" property to the device tree.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
 drivers/of/kexec.c | 57 --
 1 file changed, 5 insertions(+), 52 deletions(-)

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 7ee4f498ca19..5ae0e5d90f55 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -232,36 +232,6 @@ int of_ima_add_kexec_buffer(struct kimage *image,
return 0;
 }
 
-/**
- * write_number - Convert number to big-endian format
- *
- * @p: Buffer to write the number to
- * @value: Number to convert
- * @cells: Number of cells
- *
- * Return: 0 on success, or negative errno on error.
- */
-static int write_number(void *p, u64 value, int cells)
-{
-   if (cells == 1) {
-   u32 tmp;
-
-   if (value > U32_MAX)
-   return -EINVAL;
-
-   tmp = cpu_to_be32(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else if (cells == 2) {
-   u64 tmp;
-
-   tmp = cpu_to_be64(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else
-   return -EINVAL;
-
-   return 0;
-}
-
 /**
  * setup_ima_buffer - add IMA buffer information to the fdt
  * @image: kexec image being loaded.
@@ -273,32 +243,15 @@ static int write_number(void *p, u64 value, int cells)
 static int setup_ima_buffer(const struct kimage *image, void *fdt,
int chosen_node)
 {
-   int ret, addr_cells, size_cells, entry_size;
-   u8 value[16];
+   int ret;
 
if (!image->ima_buffer_size)
return 0;
 
-   ret = get_addr_size_cells(&addr_cells, &size_cells);
-   if (ret)
-   return ret;
-
-   entry_size = 4 * (addr_cells + size_cells);
-
-   if (entry_size > sizeof(value))
-   return -EINVAL;
-
-   ret = write_number(value, image->ima_buffer_addr, addr_cells);
-   if (ret)
-   return ret;
-
-   ret = write_number(value + 4 * addr_cells, image->ima_buffer_size,
-  size_cells);
-   if (ret)
-   return ret;
-
-   ret = fdt_setprop(fdt, chosen_node, "linux,ima-kexec-buffer", value,
- entry_size);
+   ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
+  "linux,ima-kexec-buffer",
+  image->ima_buffer_addr,
+  image->ima_buffer_size);
if (ret < 0)
return -EINVAL;
 
-- 
2.30.0



[PATCH v16 09/12] of: Define functions to allocate and free FDT

2021-02-04 Thread Lakshmi Ramasubramanian
Kernel components that use Flattened Device Tree (FDT) allocate kernel
memory and call fdt_open_into() to reorganize the tree into a form
suitable for the read-write operations.  These operations can be
combined into a single function to allocate and initialize the FDT
so the different architecures do not have to duplicate the code.

Define of_alloc_and_init_fdt() and of_free_fdt() in drivers/of/kexec.c
to allocate and initialize FDT, and to free the FDT buffer respectively.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Rob Herring 
Suggested-by: Joe Perches 
---
 drivers/of/kexec.c | 37 +
 include/linux/of.h |  2 ++
 2 files changed, 39 insertions(+)

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 5ae0e5d90f55..197e71104f47 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -11,6 +11,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -28,6 +29,42 @@
 #define FDT_PROP_RNG_SEED  "rng-seed"
 #define RNG_SEED_SIZE  128
 
+/**
+ * of_alloc_and_init_fdt - Allocate and initialize a Flattened device tree
+ *
+ * @fdt_size:  Flattened device tree size
+ *
+ * Return the allocated FDT buffer on success, or NULL on error.
+ */
+void *of_alloc_and_init_fdt(unsigned int fdt_size)
+{
+   void *fdt;
+   int ret;
+
+   fdt = kvmalloc(fdt_size, GFP_KERNEL);
+   if (!fdt)
+   return NULL;
+
+   ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
+   if (ret < 0) {
+   pr_err("Error setting up the new device tree.\n");
+   kvfree(fdt);
+   fdt = NULL;
+   }
+
+   return fdt;
+}
+
+/**
+ * of_free_fdt - Free the buffer for Flattened device tree
+ *
+ * @fdt:   Flattened device tree buffer to free
+ */
+void of_free_fdt(void *fdt)
+{
+   kvfree(fdt);
+}
+
 /**
  * fdt_find_and_del_mem_rsv - delete memory reservation with given address and 
size
  *
diff --git a/include/linux/of.h b/include/linux/of.h
index 19f77dd12507..9f0261761e28 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -563,6 +563,8 @@ struct kimage;
 int of_kexec_setup_new_fdt(const struct kimage *image, void *fdt,
   unsigned long initrd_load_addr, unsigned long 
initrd_len,
   const char *cmdline);
+void *of_alloc_and_init_fdt(unsigned int fdt_size);
+void of_free_fdt(void *fdt);
 
 #ifdef CONFIG_IMA_KEXEC
 int of_ima_add_kexec_buffer(struct kimage *image,
-- 
2.30.0



[PATCH v16 08/12] powerpc: Delete unused function delete_fdt_mem_rsv()

2021-02-04 Thread Lakshmi Ramasubramanian
delete_fdt_mem_rsv() defined in "arch/powerpc/kexec/file_load.c"
has been renamed to fdt_find_and_del_mem_rsv(), and moved to
"drivers/of/kexec.c".

Remove delete_fdt_mem_rsv() in "arch/powerpc/kexec/file_load.c".

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h |  1 -
 arch/powerpc/kexec/file_load.c   | 32 
 2 files changed, 33 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 939bc40dfa62..2c0be93d239a 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -118,7 +118,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
 struct kexec_buf;
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 5dd3a9c45a2d..036c8fb48fc3 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -108,35 +108,3 @@ int setup_purgatory(struct kimage *image, const void 
*slave_code,
 
return 0;
 }
-
-/**
- * delete_fdt_mem_rsv - delete memory reservation with given address and size
- *
- * Return: 0 on success, or negative errno on error.
- */
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
-{
-   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
-
-   for (i = 0; i < num_rsvs; i++) {
-   uint64_t rsv_start, rsv_size;
-
-   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
-   if (ret) {
-   pr_err("Malformed device tree.\n");
-   return -EINVAL;
-   }
-
-   if (rsv_start == start && rsv_size == size) {
-   ret = fdt_del_mem_rsv(fdt, i);
-   if (ret) {
-   pr_err("Error deleting device tree 
reservation.\n");
-   return -EINVAL;
-   }
-
-   return 0;
-   }
-   }
-
-   return -ENOENT;
-}
-- 
2.30.0



[PATCH v16 10/12] arm64: Use OF alloc and free functions for FDT

2021-02-04 Thread Lakshmi Ramasubramanian
of_alloc_and_init_fdt() and of_free_fdt() have been defined in
drivers/of/kexec.c to allocate and free memory for FDT.

Use of_alloc_and_init_fdt() and of_free_fdt() to allocate and
initialize the FDT, and to free the FDT respectively.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Rob Herring 
---
 arch/arm64/kernel/machine_kexec_file.c | 37 +++---
 1 file changed, 10 insertions(+), 27 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 7da22bb7b9d5..7d6cc478f73c 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -29,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-   vfree(image->arch.dtb);
+   of_free_fdt(image->arch.dtb);
image->arch.dtb = NULL;
 
vfree(image->arch.elf_headers);
@@ -57,36 +57,19 @@ static int create_dtb(struct kimage *image,
cmdline_len = cmdline ? strlen(cmdline) : 0;
buf_size = fdt_totalsize(initial_boot_params)
+ cmdline_len + DTB_EXTRA_SPACE;
-
-   for (;;) {
-   buf = vmalloc(buf_size);
-   if (!buf)
-   return -ENOMEM;
-
-   /* duplicate a device tree blob */
-   ret = fdt_open_into(initial_boot_params, buf, buf_size);
-   if (ret)
-   return -EINVAL;
-
-   ret = of_kexec_setup_new_fdt(image, buf, initrd_load_addr,
+   buf = of_alloc_and_init_fdt(buf_size);
+   if (!buf)
+   return -ENOMEM;
+   ret = of_kexec_setup_new_fdt(image, buf, initrd_load_addr,
 initrd_len, cmdline);
-   if (ret) {
-   vfree(buf);
-   if (ret == -ENOMEM) {
-   /* unlikely, but just in case */
-   buf_size += DTB_EXTRA_SPACE;
-   continue;
-   } else {
-   return ret;
-   }
-   }
-
+   if (!ret) {
/* trim it */
fdt_pack(buf);
*dtb = buf;
+   } else
+   of_free_fdt(buf);
 
-   return 0;
-   }
+   return ret;
 }
 
 static int prepare_elf_headers(void **addr, unsigned long *sz)
@@ -224,6 +207,6 @@ int load_other_segments(struct kimage *image,
 
 out_err:
image->nr_segments = orig_segments;
-   vfree(dtb);
+   of_free_fdt(dtb);
return ret;
 }
-- 
2.30.0



[PATCH v16 12/12] arm64: Enable passing IMA log to next kernel on kexec

2021-02-04 Thread Lakshmi Ramasubramanian
Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 05e17351e4f3..8a93573cebb6 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1093,6 +1093,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
-- 
2.30.0



[PATCH v16 11/12] powerpc: Use OF alloc and free for FDT

2021-02-04 Thread Lakshmi Ramasubramanian
of_alloc_and_init_fdt() and of_free_fdt() have been defined in
drivers/of/kexec.c to allocate and free memory for FDT.

Use of_alloc_and_init_fdt() and of_free_fdt() to allocate and
initialize the FDT, and to free the FDT respectively.

powerpc sets the FDT address in image_loader_data field in
"struct kimage" and the memory is freed in
kimage_file_post_load_cleanup().  This cleanup function uses kfree()
to free the memory. But since of_alloc_and_init_fdt() uses kvmalloc()
for allocation, the buffer needs to be freed using kvfree().

Define "fdt" field in "struct kimage_arch" for powerpc to store
the address of FDT, and free the memory in powerpc specific
arch_kimage_file_post_load_cleanup().

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Rob Herring 
Suggested-by: Thiago Jung Bauermann 
---
 arch/powerpc/include/asm/kexec.h  |  2 ++
 arch/powerpc/kexec/elf_64.c   | 26 --
 arch/powerpc/kexec/file_load_64.c |  3 +++
 3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 2c0be93d239a..d7d13cac4d31 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,6 +111,8 @@ struct kimage_arch {
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
+
+   void *fdt;
 };
 
 char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index d0e459bb2f05..51d2d8eb6c1b 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -32,7 +33,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
unsigned int fdt_size;
unsigned long kernel_load_addr;
unsigned long initrd_load_addr = 0, fdt_load_addr;
-   void *fdt;
+   void *fdt = NULL;
const void *slave_code;
struct elfhdr ehdr;
char *modified_cmdline = NULL;
@@ -103,18 +104,12 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
}
 
fdt_size = fdt_totalsize(initial_boot_params) * 2;
-   fdt = kmalloc(fdt_size, GFP_KERNEL);
+   fdt = of_alloc_and_init_fdt(fdt_size);
if (!fdt) {
pr_err("Not enough memory for the device tree.\n");
ret = -ENOMEM;
goto out;
}
-   ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
-   if (ret < 0) {
-   pr_err("Error setting up the new device tree.\n");
-   ret = -EINVAL;
-   goto out;
-   }
 
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
@@ -131,6 +126,10 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
ret = kexec_add_buffer(&kbuf);
if (ret)
goto out;
+
+   /* FDT will be freed in arch_kimage_file_post_load_cleanup */
+   image->arch.fdt = fdt;
+
fdt_load_addr = kbuf.mem;
 
pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
@@ -145,8 +144,15 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
 
-   /* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
-   return ret ? ERR_PTR(ret) : fdt;
+   /*
+* Once FDT buffer has been successfully passed to kexec_add_buffer(),
+* the FDT buffer address is saved in image->arch.fdt. In that case,
+* the memory cannot be freed here in case of any other error.
+*/
+   if (ret && !image->arch.fdt)
+   of_free_fdt(fdt);
+
+   return ret ? ERR_PTR(ret) : NULL;
 }
 
 const struct kexec_file_ops kexec_elf64_ops = {
diff --git a/arch/powerpc/kexec/file_load_64.c 
b/arch/powerpc/kexec/file_load_64.c
index 3cab318aa3b9..d9d5b5569a6d 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -1113,5 +1113,8 @@ int arch_kimage_file_post_load_cleanup(struct kimage 
*image)
image->arch.elf_headers = NULL;
image->arch.elf_headers_sz = 0;
 
+   of_free_fdt(image->arch.fdt);
+   image->arch.fdt = NULL;
+
return kexec_image_post_load_cleanup_default(image);
 }
-- 
2.30.0



[PATCH v2 2/2] ima: Free IMA measurement buffer after kexec syscall

2021-02-04 Thread Lakshmi Ramasubramanian
IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  This buffer is not freed before
completing the kexec system call resulting in memory leak.

Add ima_buffer field in "struct kimage" to store the virtual address
of the buffer allocated for the IMA measurement list.
Free the memory allocated for the IMA measurement list in
kimage_file_post_load_cleanup() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
 include/linux/kexec.h  | 5 +
 kernel/kexec_file.c| 5 +
 security/integrity/ima/ima_kexec.c | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 9e93bef52968..5f61389f5f36 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -300,6 +300,11 @@ struct kimage {
/* Information for loading purgatory */
struct purgatory_info purgatory_info;
 #endif
+
+#ifdef CONFIG_IMA_KEXEC
+   /* Virtual address of IMA measurement buffer for kexec syscall */
+   void *ima_buffer;
+#endif
 };
 
 /* kexec interface functions */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index b02086d70492..5c3447cf7ad5 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -166,6 +166,11 @@ void kimage_file_post_load_cleanup(struct kimage *image)
vfree(pi->sechdrs);
pi->sechdrs = NULL;
 
+#ifdef CONFIG_IMA_KEXEC
+   vfree(image->ima_buffer);
+   image->ima_buffer = NULL;
+#endif /* CONFIG_IMA_KEXEC */
+
/* See if architecture has anything to cleanup post load */
arch_kimage_file_post_load_cleanup(image);
 
diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 206ddcaa5c67..e29bea3dd4cc 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -129,6 +129,8 @@ void ima_add_kexec_buffer(struct kimage *image)
return;
}
 
+   image->ima_buffer = kexec_buffer;
+
pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
 kbuf.mem);
 }
-- 
2.30.0



[PATCH v2 1/2] ima: Free IMA measurement buffer on error

2021-02-04 Thread Lakshmi Ramasubramanian
IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  In error code paths this memory
is not freed resulting in memory leak.

Free the memory allocated for the IMA measurement list in
the error code paths in ima_add_kexec_buffer() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
 security/integrity/ima/ima_kexec.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/security/integrity/ima/ima_kexec.c 
b/security/integrity/ima/ima_kexec.c
index 121de3e04af2..206ddcaa5c67 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -119,6 +119,7 @@ void ima_add_kexec_buffer(struct kimage *image)
ret = kexec_add_buffer(&kbuf);
if (ret) {
pr_err("Error passing over kexec measurement buffer.\n");
+   vfree(kexec_buffer);
return;
}
 
-- 
2.30.0



Re: [PATCH v16 11/12] powerpc: Use OF alloc and free for FDT

2021-02-04 Thread Lakshmi Ramasubramanian

On 2/4/21 11:26 AM, Rob Herring wrote:

On Thu, Feb 4, 2021 at 10:42 AM Lakshmi Ramasubramanian
 wrote:


of_alloc_and_init_fdt() and of_free_fdt() have been defined in
drivers/of/kexec.c to allocate and free memory for FDT.

Use of_alloc_and_init_fdt() and of_free_fdt() to allocate and
initialize the FDT, and to free the FDT respectively.

powerpc sets the FDT address in image_loader_data field in
"struct kimage" and the memory is freed in
kimage_file_post_load_cleanup().  This cleanup function uses kfree()
to free the memory. But since of_alloc_and_init_fdt() uses kvmalloc()
for allocation, the buffer needs to be freed using kvfree().


You could just change the kexec core to call kvfree() instead.





Define "fdt" field in "struct kimage_arch" for powerpc to store
the address of FDT, and free the memory in powerpc specific
arch_kimage_file_post_load_cleanup().


However, given all the other buffers have an explicit field in kimage
or kimage_arch, changing powerpc is to match arm64 is better IMO.


Just to be clear:
I'll leave this as is - free FDT buffer in powerpc's 
arch_kimage_file_post_load_cleanup() to match arm64 behavior.


Will not change "kexec core" to call kvfree() - doing that change would 
require changing all architectures to use kvmalloc() for 
image_loader_data allocation.





Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Rob Herring 
Suggested-by: Thiago Jung Bauermann 
---
  arch/powerpc/include/asm/kexec.h  |  2 ++
  arch/powerpc/kexec/elf_64.c   | 26 --
  arch/powerpc/kexec/file_load_64.c |  3 +++
  3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 2c0be93d239a..d7d13cac4d31 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,6 +111,8 @@ struct kimage_arch {
 unsigned long elf_headers_mem;
 unsigned long elf_headers_sz;
 void *elf_headers;
+
+   void *fdt;
  };

  char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index d0e459bb2f05..51d2d8eb6c1b 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -19,6 +19,7 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #include 
  #include 
@@ -32,7 +33,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
 unsigned int fdt_size;
 unsigned long kernel_load_addr;
 unsigned long initrd_load_addr = 0, fdt_load_addr;
-   void *fdt;
+   void *fdt = NULL;
 const void *slave_code;
 struct elfhdr ehdr;
 char *modified_cmdline = NULL;
@@ -103,18 +104,12 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
 }

 fdt_size = fdt_totalsize(initial_boot_params) * 2;
-   fdt = kmalloc(fdt_size, GFP_KERNEL);
+   fdt = of_alloc_and_init_fdt(fdt_size);
 if (!fdt) {
 pr_err("Not enough memory for the device tree.\n");
 ret = -ENOMEM;
 goto out;
 }
-   ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
-   if (ret < 0) {
-   pr_err("Error setting up the new device tree.\n");
-   ret = -EINVAL;
-   goto out;
-   }

 ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,


The first thing this function does is call setup_new_fdt() which first
calls of_kexec_setup_new_fdt(). (Note, I really don't understand the
PPC code split. It looks like there's a 32-bit and 64-bit split, but
32-bit looks broken to me. Nothing ever calls setup_new_fdt() except
setup_new_fdt_ppc64()). The arm64 version is calling
of_alloc_and_init_fdt() and then of_kexec_setup_new_fdt() directly.

So we can just make of_alloc_and_init_fdt() also call
of_kexec_setup_new_fdt() (really, just tweak of_kexec_setup_new_fdt do
the alloc and copy). 

ok - will move fdt allocation into of_kexec_setup_new_fdt().

I don't think the architecture needs to pick the

size either. It's doubtful that either one is that sensitive to the
amount of extra space.

I am not clear about the above comment -
are you saying the architectures don't need to pass FDT size to the 
alloc function?


arm64 is adding command line string length and some extra space to the 
size computed from initial_boot_params for FDT Size:


buf_size = fdt_totalsize(initial_boot_params)
+ cmdline_len + DTB_EXTRA_SPACE;

powerpc is just using twice the size computed from initial_boot_params

fdt_size = fdt_totalsize(initial_boot_params) * 2;

I think it would be safe to let arm64 and powerpc pass the required FDT 
size, along with the other params to of_kexec_setup_new_fdt() - and in 
this function we allocate FDT and 

Re: [PATCH v16 11/12] powerpc: Use OF alloc and free for FDT

2021-02-04 Thread Lakshmi Ramasubramanian

On 2/4/21 3:36 PM, Rob Herring wrote:

On Thu, Feb 4, 2021 at 5:23 PM Lakshmi Ramasubramanian
 wrote:


On 2/4/21 11:26 AM, Rob Herring wrote:

On Thu, Feb 4, 2021 at 10:42 AM Lakshmi Ramasubramanian
 wrote:


of_alloc_and_init_fdt() and of_free_fdt() have been defined in
drivers/of/kexec.c to allocate and free memory for FDT.

Use of_alloc_and_init_fdt() and of_free_fdt() to allocate and
initialize the FDT, and to free the FDT respectively.

powerpc sets the FDT address in image_loader_data field in
"struct kimage" and the memory is freed in
kimage_file_post_load_cleanup().  This cleanup function uses kfree()
to free the memory. But since of_alloc_and_init_fdt() uses kvmalloc()
for allocation, the buffer needs to be freed using kvfree().


You could just change the kexec core to call kvfree() instead.





Define "fdt" field in "struct kimage_arch" for powerpc to store
the address of FDT, and free the memory in powerpc specific
arch_kimage_file_post_load_cleanup().


However, given all the other buffers have an explicit field in kimage
or kimage_arch, changing powerpc is to match arm64 is better IMO.


Just to be clear:
I'll leave this as is - free FDT buffer in powerpc's
arch_kimage_file_post_load_cleanup() to match arm64 behavior.


Yes.


ok




Will not change "kexec core" to call kvfree() - doing that change would
require changing all architectures to use kvmalloc() for
image_loader_data allocation.


Actually, no. AIUI, kvfree() can be used whether you used kvmalloc,
vmalloc, or kmalloc for the alloc.

That is good information. Thanks.




Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Rob Herring 
Suggested-by: Thiago Jung Bauermann 
---
   arch/powerpc/include/asm/kexec.h  |  2 ++
   arch/powerpc/kexec/elf_64.c   | 26 --
   arch/powerpc/kexec/file_load_64.c |  3 +++
   3 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 2c0be93d239a..d7d13cac4d31 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,6 +111,8 @@ struct kimage_arch {
  unsigned long elf_headers_mem;
  unsigned long elf_headers_sz;
  void *elf_headers;
+
+   void *fdt;
   };

   char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index d0e459bb2f05..51d2d8eb6c1b 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -19,6 +19,7 @@
   #include 
   #include 
   #include 
+#include 
   #include 
   #include 
   #include 
@@ -32,7 +33,7 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
  unsigned int fdt_size;
  unsigned long kernel_load_addr;
  unsigned long initrd_load_addr = 0, fdt_load_addr;
-   void *fdt;
+   void *fdt = NULL;
  const void *slave_code;
  struct elfhdr ehdr;
  char *modified_cmdline = NULL;
@@ -103,18 +104,12 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
  }

  fdt_size = fdt_totalsize(initial_boot_params) * 2;
-   fdt = kmalloc(fdt_size, GFP_KERNEL);
+   fdt = of_alloc_and_init_fdt(fdt_size);
  if (!fdt) {
  pr_err("Not enough memory for the device tree.\n");
  ret = -ENOMEM;
  goto out;
  }
-   ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
-   if (ret < 0) {
-   pr_err("Error setting up the new device tree.\n");
-   ret = -EINVAL;
-   goto out;
-   }

  ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,


The first thing this function does is call setup_new_fdt() which first
calls of_kexec_setup_new_fdt(). (Note, I really don't understand the
PPC code split. It looks like there's a 32-bit and 64-bit split, but
32-bit looks broken to me. Nothing ever calls setup_new_fdt() except
setup_new_fdt_ppc64()). The arm64 version is calling
of_alloc_and_init_fdt() and then of_kexec_setup_new_fdt() directly.

So we can just make of_alloc_and_init_fdt() also call
of_kexec_setup_new_fdt() (really, just tweak of_kexec_setup_new_fdt do
the alloc and copy).

ok - will move fdt allocation into of_kexec_setup_new_fdt().

I don't think the architecture needs to pick the

size either. It's doubtful that either one is that sensitive to the
amount of extra space.

I am not clear about the above comment -
are you saying the architectures don't need to pass FDT size to the
alloc function?

arm64 is adding command line string length and some extra space to the
size computed from initial_boot_params for FDT Size:

 buf_size = fdt_totalsize(initial_boot_params)
 + cmdline_len + DTB_EXTRA_SPACE;

powerpc is just using twice the size computed from initial_boot_params


Re: [PATCH v2 1/2] ima: Free IMA measurement buffer on error

2021-02-05 Thread Lakshmi Ramasubramanian

On 2/5/21 2:05 AM, Greg KH wrote:

On Thu, Feb 04, 2021 at 09:49:50AM -0800, Lakshmi Ramasubramanian wrote:

IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  In error code paths this memory
is not freed resulting in memory leak.

Free the memory allocated for the IMA measurement list in
the error code paths in ima_add_kexec_buffer() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
  security/integrity/ima/ima_kexec.c | 1 +
  1 file changed, 1 insertion(+)




This is not the correct way to submit patches for inclusion in the
stable kernel tree.  Please read:
 https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.





Thanks for the info Greg.

I will re-submit the two patches in the proper format.

 -lakshmi



Re: [PATCH v2 1/2] ima: Free IMA measurement buffer on error

2021-02-05 Thread Lakshmi Ramasubramanian

On 2/5/21 9:49 AM, Mimi Zohar wrote:

Hi Mimi,


On Fri, 2021-02-05 at 09:39 -0800, Lakshmi Ramasubramanian wrote:

On 2/5/21 2:05 AM, Greg KH wrote:

On Thu, Feb 04, 2021 at 09:49:50AM -0800, Lakshmi Ramasubramanian wrote:

IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  In error code paths this memory
is not freed resulting in memory leak.

Free the memory allocated for the IMA measurement list in
the error code paths in ima_add_kexec_buffer() function.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Tyler Hicks 
Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")
---
   security/integrity/ima/ima_kexec.c | 1 +
   1 file changed, 1 insertion(+)




This is not the correct way to submit patches for inclusion in the
stable kernel tree.  Please read:
  https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.





Thanks for the info Greg.

I will re-submit the two patches in the proper format.


No need.  I'm testing these patches now.  I'm not exactly sure what the
problem is.  Stable wasn't Cc'ed.  Is it that you sent the patch
directly to Greg or added "Fixes"?


I had not Cced stable, but had "Fixes" tag in the patch.

Fixes: 7b8589cc29e7 ("ima: on soft reboot, save the measurement list")

The problem is that the buffer allocated for forwarding the IMA 
measurement list is not freed - at the end of the kexec call and also in 
an error path. Please see the patch description for


[PATCH v2 2/2] ima: Free IMA measurement buffer after kexec syscall

IMA allocates kernel virtual memory to carry forward the measurement
list, from the current kernel to the next kernel on kexec system call,
in ima_add_kexec_buffer() function.  This buffer is not freed before
completing the kexec system call resulting in memory leak.

thanks,
 -lakshmi


[PATCH v17 00/10] Carry forward IMA measurement log on kexec on ARM64

2021-02-09 Thread Lakshmi Ramasubramanian
function when moving the arch independent code from powerpc to IMA
  - Reverted the change to use FDT functions in powerpc code and added
back the original code in get_addr_size_cells() and
do_get_kexec_buffer() for powerpc.
  - Added fdt_add_mem_rsv() for ARM64 to reserve the memory for
the IMA log buffer during kexec.
  - Fixed the warning reported by kernel test bot for ARM64
arch_ima_add_kexec_buffer() - moved this function to a new file
namely arch/arm64/kernel/ima_kexec.c

v4:
  - Submitting the patch series on behalf of the original author
Prakhar Srivastava 
  - Moved FDT_PROP_IMA_KEXEC_BUFFER ("linux,ima-kexec-buffer") to
libfdt.h so that it can be shared by multiple platforms.

v3:
Breakup patches further into separate patches.
  - Refactoring non architecture specific code out of powerpc
  - Update powerpc related code to use fdt functions
  - Update IMA buffer read related code to use of functions
  - Add support to store the memory information of the IMA
measurement logs to be carried forward.
  - Update the property strings to align with documented nodes
https://github.com/devicetree-org/dt-schema/pull/46

v2:
  Break patches into separate patches.
  - Powerpc related Refactoring
  - Updating the docuemntation for chosen node
  - Updating arm64 to support IMA buffer pass

v1:
  Refactoring carrying over IMA measuremnet logs over Kexec. This patch
moves the non-architecture specific code out of powerpc and adds to
security/ima.(Suggested by Thiago)
  Add Documentation regarding the ima-kexec-buffer node in the chosen
node documentation

v0:
  Add a layer of abstraction to use the memory reserved by device tree
for ima buffer pass.
  Add support for ima buffer pass using reserved memory for arm64 kexec.
Update the arch sepcific code path in kexec file load to store the
ima buffer in the reserved memory. The same reserved memory is read
on kexec or cold boot.

Lakshmi Ramasubramanian (6):
  powerpc: Move ima buffer fields to struct kimage
  powerpc: Enable passing IMA log to next kernel on kexec
  powerpc: Move arch independent ima kexec functions to
drivers/of/kexec.c
  kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT
  powerpc: Delete unused function delete_fdt_mem_rsv()
  arm64: Enable passing IMA log to next kernel on kexec

Rob Herring (4):
  powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem
  of: Add a common kexec FDT setup function
  arm64: Use common of_kexec_alloc_and_setup_fdt()
  powerpc: Use common of_kexec_alloc_and_setup_fdt()

 arch/arm64/Kconfig |   1 +
 arch/arm64/kernel/machine_kexec_file.c | 180 +-
 arch/powerpc/Kconfig   |   2 +-
 arch/powerpc/include/asm/ima.h |  30 --
 arch/powerpc/include/asm/kexec.h   |  12 +-
 arch/powerpc/kexec/Makefile|   7 -
 arch/powerpc/kexec/elf_64.c|  29 +-
 arch/powerpc/kexec/file_load.c | 183 +-
 arch/powerpc/kexec/file_load_64.c  |  11 +-
 arch/powerpc/kexec/ima.c   | 219 
 drivers/of/Makefile|   6 +
 drivers/of/kexec.c | 473 +
 include/linux/kexec.h  |   3 +
 include/linux/of.h |  20 ++
 security/integrity/ima/ima.h   |   4 -
 security/integrity/ima/ima_kexec.c |   3 +-
 16 files changed, 539 insertions(+), 644 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c
 create mode 100644 drivers/of/kexec.c

-- 
2.30.0



[PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-09 Thread Lakshmi Ramasubramanian
From: Rob Herring 

Both arm64 and powerpc do essentially the same FDT /chosen setup for
kexec.  The differences are either omissions that arm64 should have
or additional properties that will be ignored.  The setup code can be
combined and shared by both powerpc and arm64.

The differences relative to the arm64 version:
 - If /chosen doesn't exist, it will be created (should never happen).
 - Any old dtb and initrd reserved memory will be released.
 - The new initrd and elfcorehdr are marked reserved.
 - "linux,booted-from-kexec" is set.

The differences relative to the powerpc version:
 - "kaslr-seed" and "rng-seed" may be set.
 - "linux,elfcorehdr" is set.
 - Any existing "linux,usable-memory-range" is removed.

Combine the code for setting up the /chosen node in the FDT and updating
the memory reservation for kexec, for powerpc and arm64, in
of_kexec_alloc_and_setup_fdt() and move it to "drivers/of/kexec.c".

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
 drivers/of/Makefile |   6 ++
 drivers/of/kexec.c  | 258 
 include/linux/of.h  |  13 +++
 3 files changed, 277 insertions(+)
 create mode 100644 drivers/of/kexec.c

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 6e1e5212f058..c13b982084a3 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -14,4 +14,10 @@ obj-$(CONFIG_OF_RESOLVE)  += resolver.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o
 obj-$(CONFIG_OF_NUMA) += of_numa.o
 
+ifdef CONFIG_KEXEC_FILE
+ifdef CONFIG_OF_FLATTREE
+obj-y  += kexec.o
+endif
+endif
+
 obj-$(CONFIG_OF_UNITTEST) += unittest-data/
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
new file mode 100644
index ..469e09613cdd
--- /dev/null
+++ b/drivers/of/kexec.c
@@ -0,0 +1,258 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Arm Limited
+ *
+ * Based on arch/arm64/kernel/machine_kexec_file.c:
+ *  Copyright (C) 2018 Linaro Limited
+ *
+ * And arch/powerpc/kexec/file_load.c:
+ *  Copyright (C) 2016  IBM Corporation
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* relevant device tree properties */
+#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
+#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
+#define FDT_PROP_INITRD_START  "linux,initrd-start"
+#define FDT_PROP_INITRD_END"linux,initrd-end"
+#define FDT_PROP_BOOTARGS  "bootargs"
+#define FDT_PROP_KASLR_SEED"kaslr-seed"
+#define FDT_PROP_RNG_SEED  "rng-seed"
+#define RNG_SEED_SIZE  128
+
+/**
+ * fdt_find_and_del_mem_rsv - delete memory reservation with given address and 
size
+ *
+ * @fdt:   Flattened device tree for the current kernel.
+ * @start: Starting address of the reserved memory.
+ * @size:  Size of the reserved memory.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned 
long size)
+{
+   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
+
+   for (i = 0; i < num_rsvs; i++) {
+   u64 rsv_start, rsv_size;
+
+   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
+   if (ret) {
+   pr_err("Malformed device tree.\n");
+   return -EINVAL;
+   }
+
+   if (rsv_start == start && rsv_size == size) {
+   ret = fdt_del_mem_rsv(fdt, i);
+   if (ret) {
+   pr_err("Error deleting device tree 
reservation.\n");
+   return -EINVAL;
+   }
+
+   return 0;
+   }
+   }
+
+   return -ENOENT;
+}
+
+/*
+ * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
+ *
+ * @image: kexec image being loaded.
+ * @initrd_load_addr:  Address where the next initrd will be loaded.
+ * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:   Command line for the next kernel, or NULL if there will
+ * be none.
+ *
+ * Return: fdt on success, or NULL errno on error.
+ */
+void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
+  unsigned long initrd_load_addr,
+  unsigned long initrd_len,
+  const char *cmdline)
+{
+   void *fdt;
+   int ret, chosen_node;
+   const void *prop;
+   unsigned long fdt_size;
+
+   fdt_size = fdt_totalsize(initial_boot_params) +
+  (cmdline ? strlen(cmdline) : 0) +
+  FDT_EXTRA_SPACE;
+
+   fdt = kvmalloc(fdt_size, GFP_KERNEL);
+   if (!fdt)
+   return NULL;
+
+   ret =

[PATCH v17 01/10] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem

2021-02-09 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The architecture specific field, elfcorehdr_addr in struct kimage_arch,
that holds the address of the buffer in memory for ELF core header for
powerpc has a different name than the one used for arm64.  This makes
it hard to have a common code for setting up the device tree for
kexec system call.

Rename elfcorehdr_addr to elf_headers_mem to align with arm64 name so
common code can use it.

Signed-off-by: Rob Herring 
Reviewed-by: Thiago Jung Bauermann 
Reviewed-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h  | 2 +-
 arch/powerpc/kexec/file_load.c| 4 ++--
 arch/powerpc/kexec/file_load_64.c | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 55d6ede30c19..dbf09d2f36d0 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -108,7 +108,7 @@ struct kimage_arch {
unsigned long backup_start;
void *backup_buf;
 
-   unsigned long elfcorehdr_addr;
+   unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
 
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index 9a232bc36c8f..e452b11df631 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -45,7 +45,7 @@ char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
return NULL;
 
elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
-   image->arch.elfcorehdr_addr);
+   image->arch.elf_headers_mem);
 
if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
pr_err("Appending elfcorehdr= exceeds cmdline size\n");
@@ -263,7 +263,7 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
 * Avoid elfcorehdr from being stomped on in kdump kernel by
 * setting up memory reserve map.
 */
-   ret = fdt_add_mem_rsv(fdt, image->arch.elfcorehdr_addr,
+   ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
  image->arch.elf_headers_sz);
if (ret) {
pr_err("Error reserving elfcorehdr memory: %s\n",
diff --git a/arch/powerpc/kexec/file_load_64.c 
b/arch/powerpc/kexec/file_load_64.c
index c69bcf9b547a..a05c19b3cc60 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -815,7 +815,7 @@ static int load_elfcorehdr_segment(struct kimage *image, 
struct kexec_buf *kbuf)
goto out;
}
 
-   image->arch.elfcorehdr_addr = kbuf->mem;
+   image->arch.elf_headers_mem = kbuf->mem;
image->arch.elf_headers_sz = headers_sz;
image->arch.elf_headers = headers;
 out:
@@ -851,7 +851,7 @@ int load_crashdump_segments_ppc64(struct kimage *image,
return ret;
}
pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
-image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz);
+image->arch.elf_headers_mem, kbuf->bufsz, kbuf->memsz);
 
return 0;
 }
-- 
2.30.0



[PATCH v17 03/10] arm64: Use common of_kexec_alloc_and_setup_fdt()

2021-02-09 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_alloc_and_setup_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_alloc_and_setup_fdt() to setup the device tree
and update the memory reservation for kexec for arm64.

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/arm64/kernel/machine_kexec_file.c | 180 ++---
 1 file changed, 8 insertions(+), 172 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index 03210f644790..5a203f4f31fd 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -15,23 +15,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
-
-/* relevant device tree properties */
-#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
-#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
-#define FDT_PROP_INITRD_START  "linux,initrd-start"
-#define FDT_PROP_INITRD_END"linux,initrd-end"
-#define FDT_PROP_BOOTARGS  "bootargs"
-#define FDT_PROP_KASLR_SEED"kaslr-seed"
-#define FDT_PROP_RNG_SEED  "rng-seed"
-#define RNG_SEED_SIZE  128
 
 const struct kexec_file_ops * const kexec_file_loaders[] = {
&kexec_image_ops,
@@ -40,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-   vfree(image->arch.dtb);
+   kvfree(image->arch.dtb);
image->arch.dtb = NULL;
 
vfree(image->arch.elf_headers);
@@ -50,162 +39,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
return kexec_image_post_load_cleanup_default(image);
 }
 
-static int setup_dtb(struct kimage *image,
-unsigned long initrd_load_addr, unsigned long initrd_len,
-char *cmdline, void *dtb)
-{
-   int off, ret;
-
-   ret = fdt_path_offset(dtb, "/chosen");
-   if (ret < 0)
-   goto out;
-
-   off = ret;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-   ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-
-   if (image->type == KEXEC_TYPE_CRASH) {
-   /* add linux,elfcorehdr */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_KEXEC_ELFHDR,
-   image->arch.elf_headers_mem,
-   image->arch.elf_headers_sz);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-
-   /* add linux,usable-memory-range */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_MEM_RANGE,
-   crashk_res.start,
-   crashk_res.end - crashk_res.start + 1);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-   }
-
-   /* add bootargs */
-   if (cmdline) {
-   ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add initrd-* */
-   if (initrd_load_addr) {
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
- initrd_load_addr);
-   if (ret)
-   goto out;
-
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
- initrd_load_addr + initrd_len);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add kaslr-seed */
-   ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
-   if (ret == -FDT_ERR_NOTFOUND)
-   ret = 0;
-   else if (ret)
-   goto out;
-
-   if (rng_is_initialized()) {
-   u64 seed = get_random_u64();
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
-   if (ret)
-   

[PATCH v17 04/10] powerpc: Use common of_kexec_alloc_and_setup_fdt()

2021-02-09 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_alloc_and_setup_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_alloc_and_setup_fdt() to setup the device tree
and update the memory reservation for kexec for powerpc.

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h  |   1 +
 arch/powerpc/kexec/elf_64.c   |  29 ---
 arch/powerpc/kexec/file_load.c| 132 +-
 arch/powerpc/kexec/file_load_64.c |   3 +
 4 files changed, 25 insertions(+), 140 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index dbf09d2f36d0..bdd0ddb9ac4d 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,6 +111,7 @@ struct kimage_arch {
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
+   void *fdt;
 
 #ifdef CONFIG_IMA_KEXEC
phys_addr_t ima_buffer_addr;
diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index d0e459bb2f05..bfabd06f99b1 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -29,7 +30,6 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
unsigned long cmdline_len)
 {
int ret;
-   unsigned int fdt_size;
unsigned long kernel_load_addr;
unsigned long initrd_load_addr = 0, fdt_load_addr;
void *fdt;
@@ -102,19 +102,13 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
}
 
-   fdt_size = fdt_totalsize(initial_boot_params) * 2;
-   fdt = kmalloc(fdt_size, GFP_KERNEL);
+   fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
+  initrd_len, cmdline);
if (!fdt) {
pr_err("Not enough memory for the device tree.\n");
ret = -ENOMEM;
goto out;
}
-   ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
-   if (ret < 0) {
-   pr_err("Error setting up the new device tree.\n");
-   ret = -EINVAL;
-   goto out;
-   }
 
ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
  initrd_len, cmdline);
@@ -124,13 +118,17 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
fdt_pack(fdt);
 
kbuf.buffer = fdt;
-   kbuf.bufsz = kbuf.memsz = fdt_size;
+   kbuf.bufsz = kbuf.memsz = fdt_totalsize(fdt);
kbuf.buf_align = PAGE_SIZE;
kbuf.top_down = true;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
ret = kexec_add_buffer(&kbuf);
if (ret)
goto out;
+
+   /* FDT will be freed in arch_kimage_file_post_load_cleanup */
+   image->arch.fdt = fdt;
+
fdt_load_addr = kbuf.mem;
 
pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
@@ -145,8 +143,15 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
kfree(modified_cmdline);
kexec_free_elf_info(&elf_info);
 
-   /* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
-   return ret ? ERR_PTR(ret) : fdt;
+   /*
+* Once FDT buffer has been successfully passed to kexec_add_buffer(),
+* the FDT buffer address is saved in image->arch.fdt. In that case,
+* the memory cannot be freed here in case of any other error.
+*/
+   if (ret && !image->arch.fdt)
+   kvfree(fdt);
+
+   return ret ? ERR_PTR(ret) : NULL;
 }
 
 const struct kexec_file_ops kexec_elf64_ops = {
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index e452b11df631..d23e2969395c 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -156,135 +156,11 @@ int setup_new_fdt(const struct kimage *image, void *fdt,
  unsigned long initrd_load_addr, unsigned long initrd_len,
  const char *cmdline)
 {
-   int ret, chosen_node;
-   const void *prop;
-
-   /* Remove memory reservation for the current device tree. */
-   ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
-fdt_totalsize(initial_boot_params));
-   if (ret == 0)
-   pr_debug("Removed old device tree reservation.\n");
-   else if (ret != -ENOENT)
-   return ret;
-
-   chosen_node = fdt_path_offset(fdt, "/chosen");
-   if (chosen_node == -FDT_ERR_NOTFOUND) {
-   chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, 

[PATCH v17 05/10] powerpc: Move ima buffer fields to struct kimage

2021-02-09 Thread Lakshmi Ramasubramanian
The fields ima_buffer_addr and ima_buffer_size in "struct kimage_arch"
for powerpc are used to carry forward the IMA measurement list across
kexec system call.  These fields are not architecture specific, but are
currently limited to powerpc.

arch_ima_add_kexec_buffer() defined in "arch/powerpc/kexec/ima.c"
sets ima_buffer_addr and ima_buffer_size for the kexec system call.
This function does not have architecture specific code, but is
currently limited to powerpc.

Move ima_buffer_addr and ima_buffer_size to "struct kimage".
Rename arch_ima_add_kexec_buffer() to of_ima_add_kexec_buffer()
and move it in drivers/of/kexec.c.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Will Deacon 
---
 arch/powerpc/include/asm/ima.h |  3 ---
 arch/powerpc/include/asm/kexec.h   |  5 -
 arch/powerpc/kexec/ima.c   | 29 ++---
 drivers/of/kexec.c | 23 +++
 include/linux/kexec.h  |  3 +++
 include/linux/of.h |  5 +
 security/integrity/ima/ima_kexec.c |  3 ++-
 7 files changed, 39 insertions(+), 32 deletions(-)

diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
index ead488cf3981..51f64fd06c19 100644
--- a/arch/powerpc/include/asm/ima.h
+++ b/arch/powerpc/include/asm/ima.h
@@ -14,9 +14,6 @@ static inline void remove_ima_buffer(void *fdt, int 
chosen_node) {}
 #endif
 
 #ifdef CONFIG_IMA_KEXEC
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size);
-
 int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
 #else
 static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index bdd0ddb9ac4d..ecf88533d6b4 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -112,11 +112,6 @@ struct kimage_arch {
unsigned long elf_headers_sz;
void *elf_headers;
void *fdt;
-
-#ifdef CONFIG_IMA_KEXEC
-   phys_addr_t ima_buffer_addr;
-   size_t ima_buffer_size;
-#endif
 };
 
 char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
diff --git a/arch/powerpc/kexec/ima.c b/arch/powerpc/kexec/ima.c
index 720e50e490b6..ed38125e2f87 100644
--- a/arch/powerpc/kexec/ima.c
+++ b/arch/powerpc/kexec/ima.c
@@ -128,23 +128,6 @@ void remove_ima_buffer(void *fdt, int chosen_node)
 }
 
 #ifdef CONFIG_IMA_KEXEC
-/**
- * arch_ima_add_kexec_buffer - do arch-specific steps to add the IMA buffer
- *
- * Architectures should use this function to pass on the IMA buffer
- * information to the next kernel.
- *
- * Return: 0 on success, negative errno on error.
- */
-int arch_ima_add_kexec_buffer(struct kimage *image, unsigned long load_addr,
- size_t size)
-{
-   image->arch.ima_buffer_addr = load_addr;
-   image->arch.ima_buffer_size = size;
-
-   return 0;
-}
-
 static int write_number(void *p, u64 value, int cells)
 {
if (cells == 1) {
@@ -180,7 +163,7 @@ int setup_ima_buffer(const struct kimage *image, void *fdt, 
int chosen_node)
u8 value[16];
 
remove_ima_buffer(fdt, chosen_node);
-   if (!image->arch.ima_buffer_size)
+   if (!image->ima_buffer_size)
return 0;
 
ret = get_addr_size_cells(&addr_cells, &size_cells);
@@ -192,11 +175,11 @@ int setup_ima_buffer(const struct kimage *image, void 
*fdt, int chosen_node)
if (entry_size > sizeof(value))
return -EINVAL;
 
-   ret = write_number(value, image->arch.ima_buffer_addr, addr_cells);
+   ret = write_number(value, image->ima_buffer_addr, addr_cells);
if (ret)
return ret;
 
-   ret = write_number(value + 4 * addr_cells, image->arch.ima_buffer_size,
+   ret = write_number(value + 4 * addr_cells, image->ima_buffer_size,
   size_cells);
if (ret)
return ret;
@@ -206,13 +189,13 @@ int setup_ima_buffer(const struct kimage *image, void 
*fdt, int chosen_node)
if (ret < 0)
return -EINVAL;
 
-   ret = fdt_add_mem_rsv(fdt, image->arch.ima_buffer_addr,
- image->arch.ima_buffer_size);
+   ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr,
+ image->ima_buffer_size);
if (ret)
return -EINVAL;
 
pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n",
-image->arch.ima_buffer_addr, image->arch.ima_buffer_size);
+image->ima_buffer_addr, image->ima_buffer_size);
 
return 0;
 }
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 469e09613cdd..9f33d215b9f2 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.

[PATCH v17 06/10] powerpc: Enable passing IMA log to next kernel on kexec

2021-02-09 Thread Lakshmi Ramasubramanian
CONFIG_HAVE_IMA_KEXEC is enabled to indicate that the IMA measurement
log information is present in the device tree. This should be selected
only if CONFIG_IMA is enabled.

Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for powerpc.

Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Thiago Jung Bauermann 
---
 arch/powerpc/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 107bb4319e0e..d6e593ad270e 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -554,7 +554,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
-   select HAVE_IMA_KEXEC
+   select HAVE_IMA_KEXEC if IMA
select BUILD_BIN2C
select KEXEC_ELF
depends on PPC64
-- 
2.30.0



[PATCH v17 07/10] powerpc: Move arch independent ima kexec functions to drivers/of/kexec.c

2021-02-09 Thread Lakshmi Ramasubramanian
The functions defined in "arch/powerpc/kexec/ima.c" handle setting up
and freeing the resources required to carry over the IMA measurement
list from the current kernel to the next kernel across kexec system call.
These functions do not have architecture specific code, but are
currently limited to powerpc.

Move remove_ima_buffer() and setup_ima_buffer() calls into
of_kexec_alloc_and_setup_fdt() defined in "drivers/of/kexec.c".

Move the remaining architecture independent functions from
"arch/powerpc/kexec/ima.c" to "drivers/of/kexec.c".
Delete "arch/powerpc/kexec/ima.c" and "arch/powerpc/include/asm/ima.h".
Remove references to the deleted files and functions in powerpc and
in ima.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/ima.h|  27 
 arch/powerpc/include/asm/kexec.h  |   3 -
 arch/powerpc/kexec/Makefile   |   7 -
 arch/powerpc/kexec/file_load.c|  25 
 arch/powerpc/kexec/file_load_64.c |   4 -
 arch/powerpc/kexec/ima.c  | 202 -
 drivers/of/kexec.c| 239 ++
 include/linux/of.h|   2 +
 security/integrity/ima/ima.h  |   4 -
 9 files changed, 241 insertions(+), 272 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c

diff --git a/arch/powerpc/include/asm/ima.h b/arch/powerpc/include/asm/ima.h
deleted file mode 100644
index 51f64fd06c19..
--- a/arch/powerpc/include/asm/ima.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _ASM_POWERPC_IMA_H
-#define _ASM_POWERPC_IMA_H
-
-struct kimage;
-
-int ima_get_kexec_buffer(void **addr, size_t *size);
-int ima_free_kexec_buffer(void);
-
-#ifdef CONFIG_IMA
-void remove_ima_buffer(void *fdt, int chosen_node);
-#else
-static inline void remove_ima_buffer(void *fdt, int chosen_node) {}
-#endif
-
-#ifdef CONFIG_IMA_KEXEC
-int setup_ima_buffer(const struct kimage *image, void *fdt, int chosen_node);
-#else
-static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
-  int chosen_node)
-{
-   remove_ima_buffer(fdt, chosen_node);
-   return 0;
-}
-#endif /* CONFIG_IMA_KEXEC */
-
-#endif /* _ASM_POWERPC_IMA_H */
diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index ecf88533d6b4..2b87993f6e66 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -119,9 +119,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int setup_new_fdt(const struct kimage *image, void *fdt,
- unsigned long initrd_load_addr, unsigned long initrd_len,
- const char *cmdline);
 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
diff --git a/arch/powerpc/kexec/Makefile b/arch/powerpc/kexec/Makefile
index 4aff6846c772..b6c52608cb49 100644
--- a/arch/powerpc/kexec/Makefile
+++ b/arch/powerpc/kexec/Makefile
@@ -9,13 +9,6 @@ obj-$(CONFIG_PPC32)+= relocate_32.o
 
 obj-$(CONFIG_KEXEC_FILE)   += file_load.o ranges.o file_load_$(BITS).o 
elf_$(BITS).o
 
-ifdef CONFIG_HAVE_IMA_KEXEC
-ifdef CONFIG_IMA
-obj-y  += ima.o
-endif
-endif
-
-
 # Disable GCOV, KCOV & sanitizers in odd or sensitive code
 GCOV_PROFILE_core_$(BITS).o := n
 KCOV_INSTRUMENT_core_$(BITS).o := n
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index d23e2969395c..bd8b956aafc3 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -19,7 +19,6 @@
 #include 
 #include 
 #include 
-#include 
 
 #define SLAVE_CODE_SIZE256 /* First 0x100 bytes */
 
@@ -140,27 +139,3 @@ int delete_fdt_mem_rsv(void *fdt, unsigned long start, 
unsigned long size)
 
return -ENOENT;
 }
-
-/*
- * setup_new_fdt - modify /chosen and memory reservation for the next kernel
- * @image: kexec image being loaded.
- * @fdt:   Flattened device tree for the next kernel.
- * @initrd_load_addr:  Address where the next initrd will be loaded.
- * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
- * @cmdline:   Command line for the next kernel, or NULL if there will
- * be none.
- *
- * Return: 0 on success, or negative errno on error.
- */
-int setup_new_fdt(const struct kimage *image, void *fdt,
- unsigned long initrd_load_addr, unsigned long initrd_len,
- const char *cmdline)
-{
-   int ret;
-
-   ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen"))

[PATCH v17 08/10] kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT

2021-02-09 Thread Lakshmi Ramasubramanian
fdt_appendprop_addrrange() function adds a property, with the given name,
to the device tree at the given node offset, and also sets the address
and size of the property.  This function should be used to add
"linux,ima-kexec-buffer" property to the device tree and set the address
and size of the IMA measurement buffer, instead of using custom function.

Use fdt_appendprop_addrrange() to add  "linux,ima-kexec-buffer" property
to the device tree.  This property holds the address and size of
the IMA measurement buffer that needs to be passed from the current
kernel to the next kernel across kexec system call.

Remove custom code that is used in setup_ima_buffer() to add
"linux,ima-kexec-buffer" property to the device tree.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Reviewed-by: Thiago Jung Bauermann 
---
 drivers/of/kexec.c | 57 --
 1 file changed, 5 insertions(+), 52 deletions(-)

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index c601b5af4a88..c53746b9b168 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -232,36 +232,6 @@ int of_ima_add_kexec_buffer(struct kimage *image,
return 0;
 }
 
-/**
- * write_number - Convert number to big-endian format
- *
- * @p: Buffer to write the number to
- * @value: Number to convert
- * @cells: Number of cells
- *
- * Return: 0 on success, or negative errno on error.
- */
-static int write_number(void *p, u64 value, int cells)
-{
-   if (cells == 1) {
-   u32 tmp;
-
-   if (value > U32_MAX)
-   return -EINVAL;
-
-   tmp = cpu_to_be32(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else if (cells == 2) {
-   u64 tmp;
-
-   tmp = cpu_to_be64(value);
-   memcpy(p, &tmp, sizeof(tmp));
-   } else
-   return -EINVAL;
-
-   return 0;
-}
-
 /**
  * setup_ima_buffer - add IMA buffer information to the fdt
  * @image: kexec image being loaded.
@@ -273,32 +243,15 @@ static int write_number(void *p, u64 value, int cells)
 static int setup_ima_buffer(const struct kimage *image, void *fdt,
int chosen_node)
 {
-   int ret, addr_cells, size_cells, entry_size;
-   u8 value[16];
+   int ret;
 
if (!image->ima_buffer_size)
return 0;
 
-   ret = get_addr_size_cells(&addr_cells, &size_cells);
-   if (ret)
-   return ret;
-
-   entry_size = 4 * (addr_cells + size_cells);
-
-   if (entry_size > sizeof(value))
-   return -EINVAL;
-
-   ret = write_number(value, image->ima_buffer_addr, addr_cells);
-   if (ret)
-   return ret;
-
-   ret = write_number(value + 4 * addr_cells, image->ima_buffer_size,
-  size_cells);
-   if (ret)
-   return ret;
-
-   ret = fdt_setprop(fdt, chosen_node, "linux,ima-kexec-buffer", value,
- entry_size);
+   ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
+  "linux,ima-kexec-buffer",
+  image->ima_buffer_addr,
+  image->ima_buffer_size);
if (ret < 0)
return -EINVAL;
 
-- 
2.30.0



[PATCH v17 09/10] powerpc: Delete unused function delete_fdt_mem_rsv()

2021-02-09 Thread Lakshmi Ramasubramanian
delete_fdt_mem_rsv() defined in "arch/powerpc/kexec/file_load.c"
has been renamed to fdt_find_and_del_mem_rsv(), and moved to
"drivers/of/kexec.c".

Remove delete_fdt_mem_rsv() in "arch/powerpc/kexec/file_load.c".

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/powerpc/include/asm/kexec.h |  1 -
 arch/powerpc/kexec/file_load.c   | 32 
 2 files changed, 33 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index 2b87993f6e66..e543593da1e1 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -119,7 +119,6 @@ char *setup_kdump_cmdline(struct kimage *image, char 
*cmdline,
 int setup_purgatory(struct kimage *image, const void *slave_code,
const void *fdt, unsigned long kernel_load_addr,
unsigned long fdt_load_addr);
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size);
 
 #ifdef CONFIG_PPC64
 struct kexec_buf;
diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index bd8b956aafc3..6f75a45f14c5 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -107,35 +107,3 @@ int setup_purgatory(struct kimage *image, const void 
*slave_code,
 
return 0;
 }
-
-/**
- * delete_fdt_mem_rsv - delete memory reservation with given address and size
- *
- * Return: 0 on success, or negative errno on error.
- */
-int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
-{
-   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
-
-   for (i = 0; i < num_rsvs; i++) {
-   uint64_t rsv_start, rsv_size;
-
-   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
-   if (ret) {
-   pr_err("Malformed device tree.\n");
-   return -EINVAL;
-   }
-
-   if (rsv_start == start && rsv_size == size) {
-   ret = fdt_del_mem_rsv(fdt, i);
-   if (ret) {
-   pr_err("Error deleting device tree 
reservation.\n");
-   return -EINVAL;
-   }
-
-   return 0;
-   }
-   }
-
-   return -ENOENT;
-}
-- 
2.30.0



[PATCH v17 10/10] arm64: Enable passing IMA log to next kernel on kexec

2021-02-09 Thread Lakshmi Ramasubramanian
Update CONFIG_KEXEC_FILE to select CONFIG_HAVE_IMA_KEXEC, if CONFIG_IMA
is enabled, to indicate that the IMA measurement log information is
present in the device tree for ARM64.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Thiago Jung Bauermann 
---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 05e17351e4f3..8a93573cebb6 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -1093,6 +1093,7 @@ config KEXEC
 config KEXEC_FILE
bool "kexec file based system call"
select KEXEC_CORE
+   select HAVE_IMA_KEXEC if IMA
help
  This is new version of kexec system call. This system call is
  file based and takes file descriptors as system call argument
-- 
2.30.0



Re: [PATCH v17 00/10] Carry forward IMA measurement log on kexec on ARM64

2021-02-10 Thread Lakshmi Ramasubramanian

On 2/10/21 9:15 AM, Rob Herring wrote:

On Tue, Feb 09, 2021 at 10:21:50AM -0800, Lakshmi Ramasubramanian wrote:

On kexec file load Integrity Measurement Architecture (IMA) subsystem
may verify the IMA signature of the kernel and initramfs, and measure
it.  The command line parameters passed to the kernel in the kexec call
may also be measured by IMA.  A remote attestation service can verify
a TPM quote based on the TPM event log, the IMA measurement list, and
the TPM PCR data.  This can be achieved only if the IMA measurement log
is carried over from the current kernel to the next kernel across
the kexec call.

powerpc already supports carrying forward the IMA measurement log on
kexec.  This patch set adds support for carrying forward the IMA
measurement log on kexec on ARM64.

This patch set moves the platform independent code defined for powerpc
such that it can be reused for other platforms as well.  A chosen node
"linux,ima-kexec-buffer" is added to the DTB for ARM64 to hold
the address and the size of the memory reserved to carry
the IMA measurement log.

This patch set has been tested for ARM64 platform using QEMU.
I would like help from the community for testing this change on powerpc.
Thanks.

This patch set is based on
commit 96acc833dec8 ("ima: Free IMA measurement buffer after kexec syscall")
in https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git
"next-integrity" branch.


Is that a hard dependency still? Given this is now almost entirely
deleting arch code and adding drivers/of/ code, I was going to apply it.



I tried applying the patches in Linus' mainline branch -
PATCH #5 0005-powerpc-Move-ima-buffer-fields-to-struct-kimage.patch 
doesn't apply.


But if I apply the dependent patch set (link given below), all the 
patches in this patch set apply fine.


https://patchwork.kernel.org/project/linux-integrity/patch/20210204174951.25771-2-nra...@linux.microsoft.com/

thanks,
 -lakshmi




Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-10 Thread Lakshmi Ramasubramanian

On 2/10/21 9:23 AM, Rob Herring wrote:

On Tue, Feb 09, 2021 at 10:21:52AM -0800, Lakshmi Ramasubramanian wrote:

From: Rob Herring 

Both arm64 and powerpc do essentially the same FDT /chosen setup for
kexec.  The differences are either omissions that arm64 should have
or additional properties that will be ignored.  The setup code can be
combined and shared by both powerpc and arm64.

The differences relative to the arm64 version:
  - If /chosen doesn't exist, it will be created (should never happen).
  - Any old dtb and initrd reserved memory will be released.
  - The new initrd and elfcorehdr are marked reserved.
  - "linux,booted-from-kexec" is set.

The differences relative to the powerpc version:
  - "kaslr-seed" and "rng-seed" may be set.
  - "linux,elfcorehdr" is set.
  - Any existing "linux,usable-memory-range" is removed.

Combine the code for setting up the /chosen node in the FDT and updating
the memory reservation for kexec, for powerpc and arm64, in
of_kexec_alloc_and_setup_fdt() and move it to "drivers/of/kexec.c".

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
  drivers/of/Makefile |   6 ++
  drivers/of/kexec.c  | 258 
  include/linux/of.h  |  13 +++
  3 files changed, 277 insertions(+)
  create mode 100644 drivers/of/kexec.c




diff --git a/include/linux/of.h b/include/linux/of.h
index 4b27c9a27df3..f0eff5e84353 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -560,6 +560,19 @@ int of_map_id(struct device_node *np, u32 id,
  
  phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
  
+/*

+ * Additional space needed for the buffer to build the new FDT
+ * so that we can add initrd, bootargs, kaslr-seed, rng-seed,
+ * userable-memory-range and elfcorehdr.
+ */
+#define FDT_EXTRA_SPACE 0x1000


No need for this to be public now. Move it to of/kexec.c.



Will do.

 -lakshmi




Re: [PATCH v17 05/10] powerpc: Move ima buffer fields to struct kimage

2021-02-10 Thread Lakshmi Ramasubramanian

On 2/10/21 9:20 AM, Rob Herring wrote:

On Tue, Feb 09, 2021 at 10:21:55AM -0800, Lakshmi Ramasubramanian wrote:

The fields ima_buffer_addr and ima_buffer_size in "struct kimage_arch"
for powerpc are used to carry forward the IMA measurement list across
kexec system call.  These fields are not architecture specific, but are
currently limited to powerpc.

arch_ima_add_kexec_buffer() defined in "arch/powerpc/kexec/ima.c"
sets ima_buffer_addr and ima_buffer_size for the kexec system call.
This function does not have architecture specific code, but is
currently limited to powerpc.

Move ima_buffer_addr and ima_buffer_size to "struct kimage".
Rename arch_ima_add_kexec_buffer() to of_ima_add_kexec_buffer()
and move it in drivers/of/kexec.c.

Co-developed-by: Prakhar Srivastava 
Signed-off-by: Prakhar Srivastava 
Signed-off-by: Lakshmi Ramasubramanian 
Suggested-by: Will Deacon 
---
  arch/powerpc/include/asm/ima.h |  3 ---
  arch/powerpc/include/asm/kexec.h   |  5 -
  arch/powerpc/kexec/ima.c   | 29 ++---
  drivers/of/kexec.c | 23 +++
  include/linux/kexec.h  |  3 +++
  include/linux/of.h |  5 +
  security/integrity/ima/ima_kexec.c |  3 ++-
  7 files changed, 39 insertions(+), 32 deletions(-)



diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 469e09613cdd..9f33d215b9f2 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -63,6 +63,29 @@ static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long 
start, unsigned lon
return -ENOENT;
  }
  
+#ifdef CONFIG_IMA_KEXEC

+/**
+ * of_ima_add_kexec_buffer - Add IMA buffer for next kernel
+ *
+ * @image: kimage struct to set IMA buffer data
+ * @load_addr: Starting address where IMA buffer is loaded at
+ * @size: Number of bytes in the IMA buffer
+ *
+ * Use this function to pass on the IMA buffer information to
+ * the next kernel across kexec system call.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int of_ima_add_kexec_buffer(struct kimage *image,
+   unsigned long load_addr, size_t size)
+{
+   image->ima_buffer_addr = load_addr;
+   image->ima_buffer_size = size;
+


There's nothing DT specific about this function, so this is the wrong
place for it. I would just remove it and directly set the members.


Will do.

 -lakshmi




Re: [PATCH v17 00/10] Carry forward IMA measurement log on kexec on ARM64

2021-02-10 Thread Lakshmi Ramasubramanian

On 2/10/21 1:39 PM, Mimi Zohar wrote:

On Wed, 2021-02-10 at 15:55 -0500, Mimi Zohar wrote:

On Wed, 2021-02-10 at 14:42 -0600, Rob Herring wrote:

On Wed, Feb 10, 2021 at 11:33 AM Lakshmi Ramasubramanian



Ideally, we don't apply the same patch in 2 branches. It looks like
there's a conflict but no real dependence on the above patch (the
ima_buffer part). The conflict seems trivial enough that Linus can
resolve it in the merge window.

Or Mimi can take the whole thing if preferred?


How about I create a topic branch with just the two patches, allowing
both of us to merge it?   There shouldn't be a problem with re-writing
next-integrity history.


The 2 patches are now in the ima-kexec-fixes branch.



Thanks a lot Mimi.

Rob - I will address the 2 comments you'd provided today, and build the 
patches in ima-kexec-fixes branch.


If you have more comments in the v17 patches, please let me know.

thanks,
 -lakshmi



Re: [PATCH v17 04/10] powerpc: Use common of_kexec_alloc_and_setup_fdt()

2021-02-10 Thread Lakshmi Ramasubramanian

On 2/10/21 5:42 PM, Thiago Jung Bauermann wrote:


Lakshmi Ramasubramanian  writes:


From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_alloc_and_setup_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_alloc_and_setup_fdt() to setup the device tree
and update the memory reservation for kexec for powerpc.

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
  arch/powerpc/include/asm/kexec.h  |   1 +
  arch/powerpc/kexec/elf_64.c   |  29 ---
  arch/powerpc/kexec/file_load.c| 132 +-
  arch/powerpc/kexec/file_load_64.c |   3 +
  4 files changed, 25 insertions(+), 140 deletions(-)

diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h
index dbf09d2f36d0..bdd0ddb9ac4d 100644
--- a/arch/powerpc/include/asm/kexec.h
+++ b/arch/powerpc/include/asm/kexec.h
@@ -111,6 +111,7 @@ struct kimage_arch {
unsigned long elf_headers_mem;
unsigned long elf_headers_sz;
void *elf_headers;
+   void *fdt;
  
  #ifdef CONFIG_IMA_KEXEC

phys_addr_t ima_buffer_addr;
diff --git a/arch/powerpc/kexec/elf_64.c b/arch/powerpc/kexec/elf_64.c
index d0e459bb2f05..bfabd06f99b1 100644
--- a/arch/powerpc/kexec/elf_64.c
+++ b/arch/powerpc/kexec/elf_64.c
@@ -19,6 +19,7 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #include 
  #include 
@@ -29,7 +30,6 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
unsigned long cmdline_len)
  {
int ret;
-   unsigned int fdt_size;
unsigned long kernel_load_addr;
unsigned long initrd_load_addr = 0, fdt_load_addr;
void *fdt;
@@ -102,19 +102,13 @@ static void *elf64_load(struct kimage *image, char 
*kernel_buf,
pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
}
  
-	fdt_size = fdt_totalsize(initial_boot_params) * 2;

-   fdt = kmalloc(fdt_size, GFP_KERNEL);
+   fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
+  initrd_len, cmdline);
if (!fdt) {
pr_err("Not enough memory for the device tree.\n");


This error string can be a bit misleading now, since
of_kexec_alloc_and_setup_fdt() can fail for reasons other than lack of
memory. I suggest changing it to the error string from fdt_open_into()
below:

pr_err("Error setting up the new device tree.\n");

With this change:

Agreed - I will make this change.



Reviewed-by: Thiago Jung Bauermann 

And also:

Tested-by: Thiago Jung Bauermann 



Thanks a lot for your help Thiago.

 -lakshmi



Fwd: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-11 Thread Lakshmi Ramasubramanian

Hi Rob,

[PATCH] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem

This change causes build problem for x86_64 architecture (please see the 
mail from kernel test bot below) since arch/x86/include/asm/kexec.h uses 
"elf_load_addr" for the ELF header buffer address and not "elf_headers_mem".


struct kimage_arch {
...

/* Core ELF header buffer */
void *elf_headers;
unsigned long elf_headers_sz;
unsigned long elf_load_addr;
};

I am thinking of limiting of_kexec_alloc_and_setup_fdt() to ARM64 and 
PPC64 since they are the only ones using this function now.


#if defined(CONFIG_ARM64) && defined(CONFIG_PPC64)
void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
   unsigned long initrd_load_addr,
   unsigned long initrd_len,
   const char *cmdline)
{
...
}
#endif /* defined(CONFIG_ARM64) && defined(CONFIG_PPC64) */

Please let me know if you have any concerns.

thanks,
 -lakshmi

 Forwarded Message 
Subject: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function
Date: Fri, 12 Feb 2021 00:50:20 +0800
From: kernel test robot 
To: Lakshmi Ramasubramanian 
CC: kbuild-...@lists.01.org

Hi Lakshmi,

I love your patch! Yet something to improve:

[auto build test ERROR on integrity/next-integrity]
[also build test ERROR on v5.11-rc7 next-20210211]
[cannot apply to powerpc/next robh/for-next arm64/for-next/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: 
https://github.com/0day-ci/linux/commits/Lakshmi-Ramasubramanian/Carry-forward-IMA-measurement-log-on-kexec-on-ARM64/20210211-071924
base: 
https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git 
next-integrity

config: x86_64-randconfig-m001-20210211 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
# 
https://github.com/0day-ci/linux/commit/12ae86067d115b84092353109e8798693d102f0d

git remote add linux-review https://github.com/0day-ci/linux
    git fetch --no-tags linux-review 
Lakshmi-Ramasubramanian/Carry-forward-IMA-measurement-log-on-kexec-on-ARM64/20210211-071924

git checkout 12ae86067d115b84092353109e8798693d102f0d
# save the attached .config to linux build tree
make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot 

All errors (new ones prefixed by >>):

   drivers/of/kexec.c: In function 'of_kexec_alloc_and_setup_fdt':

drivers/of/kexec.c:183:17: error: 'const struct kimage_arch' has no member 
named 'elf_headers_mem'; did you mean 'elf_headers_sz'?

 183 | image->arch.elf_headers_mem,
 | ^~~
 | elf_headers_sz
   drivers/of/kexec.c:192:42: error: 'const struct kimage_arch' has no 
member named 'elf_headers_mem'; did you mean 'elf_headers_sz'?

 192 |   ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
 |  ^~~
 |  elf_headers_sz


vim +183 drivers/of/kexec.c

65  
66  /*
67	 * of_kexec_alloc_and_setup_fdt - Alloc and setup a new 
Flattened Device Tree

68   *
69   * @image:  kexec image being loaded.
70   * @initrd_load_addr:   Address where the next initrd will be loaded.
71	 * @initrd_len:		Size of the next initrd, or 0 if there will be 
none.
72	 * @cmdline:		Command line for the next kernel, or NULL if there 
will

73   *  be none.
74   *
75   * Return: fdt on success, or NULL errno on error.
76   */
77  void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
78 unsigned long initrd_load_addr,
79 unsigned long initrd_len,
80 const char *cmdline)
81  {
82  void *fdt;
83  int ret, chosen_node;
84  const void *prop;
85  unsigned long fdt_size;
86  
87  fdt_size = fdt_totalsize(initial_boot_params) +
88 (cmdline ? strlen(cmdline) : 0) +
89 FDT_EXTRA_SPACE;
90  
91  fdt = kvmalloc(fdt_size, GFP_KERNEL);
92  if (!fdt)
93  return NULL;
94  
95  ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
96  if (ret < 0) {
97  pr_err("Error %d setting up the new device tree.\n", 
ret);
98  goto out;
99   

Re: Fwd: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-11 Thread Lakshmi Ramasubramanian

On 2/11/21 9:42 AM, Lakshmi Ramasubramanian wrote:

Hi Rob,

[PATCH] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem

This change causes build problem for x86_64 architecture (please see the 
mail from kernel test bot below) since arch/x86/include/asm/kexec.h uses 
"elf_load_addr" for the ELF header buffer address and not 
"elf_headers_mem".


struct kimage_arch {
 ...

 /* Core ELF header buffer */
 void *elf_headers;
 unsigned long elf_headers_sz;
 unsigned long elf_load_addr;
};

I am thinking of limiting of_kexec_alloc_and_setup_fdt() to ARM64 and 
PPC64 since they are the only ones using this function now.


#if defined(CONFIG_ARM64) && defined(CONFIG_PPC64)

Sorry - I meant to say
#if defined(CONFIG_ARM64) || defined(CONFIG_PPC64)


void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
    unsigned long initrd_load_addr,
    unsigned long initrd_len,
    const char *cmdline)
{
 ...
}
#endif /* defined(CONFIG_ARM64) && defined(CONFIG_PPC64) */

Please let me know if you have any concerns.

thanks,
  -lakshmi

 Forwarded Message 
Subject: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function
Date: Fri, 12 Feb 2021 00:50:20 +0800
From: kernel test robot 
To: Lakshmi Ramasubramanian 
CC: kbuild-...@lists.01.org

Hi Lakshmi,

I love your patch! Yet something to improve:

[auto build test ERROR on integrity/next-integrity]
[also build test ERROR on v5.11-rc7 next-20210211]
[cannot apply to powerpc/next robh/for-next arm64/for-next/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: 
https://github.com/0day-ci/linux/commits/Lakshmi-Ramasubramanian/Carry-forward-IMA-measurement-log-on-kexec-on-ARM64/20210211-071924 

base: 
https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git next-integrity 


config: x86_64-randconfig-m001-20210211 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
     # 
https://github.com/0day-ci/linux/commit/12ae86067d115b84092353109e8798693d102f0d 


     git remote add linux-review https://github.com/0day-ci/linux
         git fetch --no-tags linux-review 
Lakshmi-Ramasubramanian/Carry-forward-IMA-measurement-log-on-kexec-on-ARM64/20210211-071924 


     git checkout 12ae86067d115b84092353109e8798693d102f0d
     # save the attached .config to linux build tree
     make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot 

All errors (new ones prefixed by >>):

    drivers/of/kexec.c: In function 'of_kexec_alloc_and_setup_fdt':
drivers/of/kexec.c:183:17: error: 'const struct kimage_arch' has no 
member named 'elf_headers_mem'; did you mean 'elf_headers_sz'?

  183 | image->arch.elf_headers_mem,
  | ^~~
  | elf_headers_sz
    drivers/of/kexec.c:192:42: error: 'const struct kimage_arch' has no 
member named 'elf_headers_mem'; did you mean 'elf_headers_sz'?

  192 |   ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
  |  ^~~
  |  elf_headers_sz


vim +183 drivers/of/kexec.c

     65
     66    /*
     67 * of_kexec_alloc_and_setup_fdt - Alloc and setup a new 
Flattened Device Tree

     68 *
     69 * @image:    kexec image being loaded.
     70 * @initrd_load_addr:    Address where the next initrd will 
be loaded.
     71 * @initrd_len:    Size of the next initrd, or 0 if there 
will be none.
     72 * @cmdline:    Command line for the next kernel, or NULL 
if there will

     73 *    be none.
     74 *
     75 * Return: fdt on success, or NULL errno on error.
     76 */
     77    void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
     78   unsigned long initrd_load_addr,
     79   unsigned long initrd_len,
     80   const char *cmdline)
     81    {
     82    void *fdt;
     83    int ret, chosen_node;
     84    const void *prop;
     85    unsigned long fdt_size;
     86
     87    fdt_size = fdt_totalsize(initial_boot_params) +
     88   (cmdline ? strlen(cmdline) : 0) +
     89   FDT_EXTRA_SPACE;
     90
     91    fdt = kvmalloc(fdt_size, GFP_KERNEL);
     92    if (!fdt)
     93    return NULL;
     94
     95    ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
     96    if (ret < 0) {
     97    pr_err("Error %d setting up the new device tree.\n

Re: Fwd: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-11 Thread Lakshmi Ramasubramanian

On 2/11/21 3:59 PM, Thiago Jung Bauermann wrote:


Lakshmi Ramasubramanian  writes:


On 2/11/21 9:42 AM, Lakshmi Ramasubramanian wrote:

Hi Rob,
[PATCH] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem
This change causes build problem for x86_64 architecture (please see the
mail from kernel test bot below) since arch/x86/include/asm/kexec.h uses
"elf_load_addr" for the ELF header buffer address and not
"elf_headers_mem".
struct kimage_arch {
  ...
  /* Core ELF header buffer */
  void *elf_headers;
  unsigned long elf_headers_sz;
  unsigned long elf_load_addr;
};
I am thinking of limiting of_kexec_alloc_and_setup_fdt() to ARM64 and
PPC64 since they are the only ones using this function now.
#if defined(CONFIG_ARM64) && defined(CONFIG_PPC64)

Sorry - I meant to say
#if defined(CONFIG_ARM64) || defined(CONFIG_PPC64)



Does it build correctly if you rename elf_headers_mem to elf_load_addr?
Or the other way around, renaming x86's elf_load_addr to
elf_headers_mem. I don't really have a preference.


Yes - changing arm64 and ppc from "elf_headers_mem" to "elf_load_addr" 
builds fine.


But I am concerned about a few other architectures that also define 
"struct kimage_arch" such as "parisc", "arm" which do not have any ELF 
related fields. They would not build if the config defines 
CONFIG_KEXEC_FILE and CONFIG_OF_FLATTREE.


Do you think that could be an issue?

thanks,
 -lakshmi



That would be better than adding an #if condition.


void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 unsigned long initrd_load_addr,
 unsigned long initrd_len,
 const char *cmdline)
{
  ...
}
#endif /* defined(CONFIG_ARM64) && defined(CONFIG_PPC64) */
Please let me know if you have any concerns.
thanks,
   -lakshmi






Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-11 Thread Lakshmi Ramasubramanian

On 2/11/21 5:09 PM, Thiago Jung Bauermann wrote:


There's actually a complication that I just noticed and needs to be
addressed. More below.



<...>


+
+/*
+ * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
+ *
+ * @image: kexec image being loaded.
+ * @initrd_load_addr:  Address where the next initrd will be loaded.
+ * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:   Command line for the next kernel, or NULL if there will
+ * be none.
+ *
+ * Return: fdt on success, or NULL errno on error.
+ */
+void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
+  unsigned long initrd_load_addr,
+  unsigned long initrd_len,
+  const char *cmdline)
+{
+   void *fdt;
+   int ret, chosen_node;
+   const void *prop;
+   unsigned long fdt_size;
+
+   fdt_size = fdt_totalsize(initial_boot_params) +
+  (cmdline ? strlen(cmdline) : 0) +
+  FDT_EXTRA_SPACE;


Just adding 4 KB to initial_boot_params won't be enough for crash
kernels on ppc64. The current powerpc code doubles the size of
initial_boot_params (which is normally larger than 4 KB) and even that
isn't enough. A patch was added to powerpc/next today which uses a more
precise (but arch-specific) formula:

https://lore.kernel.org/linuxppc-dev/161243826811.119001.14083048209224609814.stgit@hbathini/

So I believe we need a hook here where architectures can provide their
own specific calculation for the size of the fdt. Perhaps a weakly
defined function providing a default implementation which an
arch-specific file can override (a la arch_kexec_kernel_image_load())?

Then the powerpc specific hook would be the kexec_fdt_totalsize_ppc64()
function from the patch I linked above.



Do you think it'd better to add "fdt_size" parameter to 
of_kexec_alloc_and_setup_fdt() so that the caller can provide the 
desired FDT buffer size?


thanks,
 -lakshmi


Re: Fwd: Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-11 Thread Lakshmi Ramasubramanian

On 2/11/21 6:11 PM, Thiago Jung Bauermann wrote:


Lakshmi Ramasubramanian  writes:


On 2/11/21 3:59 PM, Thiago Jung Bauermann wrote:

Lakshmi Ramasubramanian  writes:


On 2/11/21 9:42 AM, Lakshmi Ramasubramanian wrote:

Hi Rob,
[PATCH] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem
This change causes build problem for x86_64 architecture (please see the
mail from kernel test bot below) since arch/x86/include/asm/kexec.h uses
"elf_load_addr" for the ELF header buffer address and not
"elf_headers_mem".
struct kimage_arch {
   ...
   /* Core ELF header buffer */
   void *elf_headers;
   unsigned long elf_headers_sz;
   unsigned long elf_load_addr;
};
I am thinking of limiting of_kexec_alloc_and_setup_fdt() to ARM64 and
PPC64 since they are the only ones using this function now.
#if defined(CONFIG_ARM64) && defined(CONFIG_PPC64)

Sorry - I meant to say
#if defined(CONFIG_ARM64) || defined(CONFIG_PPC64)


Does it build correctly if you rename elf_headers_mem to elf_load_addr?
Or the other way around, renaming x86's elf_load_addr to
elf_headers_mem. I don't really have a preference.


Yes - changing arm64 and ppc from "elf_headers_mem" to "elf_load_addr" builds
fine.

But I am concerned about a few other architectures that also define "struct
kimage_arch" such as "parisc", "arm" which do not have any ELF related fields.
They would not build if the config defines CONFIG_KEXEC_FILE and
CONFIG_OF_FLATTREE.

Do you think that could be an issue?


That's a good point. But in practice, arm doesn't support
CONFIG_KEXEC_FILE. And while parisc does support CONFIG_KEXEC_FILE, as
far as I could determine it doesn't support CONFIG_OF.

So IMHO we don't need to worry about them. We'll cross that bridge if we
get there. If they ever implement KEXEC_FILE or OF_FLATTREE support,
then (again, IMHO) the natural solution would be for them to name the
ELF header member the same way the other arches do.

And since no other architecture defines struct kimage_arch, those are
the only ones we need to consider.



Sounds good Thiago.

I'll rename arm64 and ppc kimage_arch ELF address field to match that 
defined for x86/x64.


Also, will add "fdt_size" param to of_kexec_alloc_and_setup_fdt(). For 
now, I'll use 2*fdt_totalsize(initial_boot_params) for ppc.


Will send the updated patches shortly.

 -lakshmi



Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-12 Thread Lakshmi Ramasubramanian

On 2/12/21 6:38 AM, Rob Herring wrote:

On Thu, Feb 11, 2021 at 7:17 PM Lakshmi Ramasubramanian
 wrote:


On 2/11/21 5:09 PM, Thiago Jung Bauermann wrote:


There's actually a complication that I just noticed and needs to be
addressed. More below.



<...>


+
+/*
+ * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
+ *
+ * @image:  kexec image being loaded.
+ * @initrd_load_addr:   Address where the next initrd will be loaded.
+ * @initrd_len: Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:Command line for the next kernel, or NULL if there 
will
+ *  be none.
+ *
+ * Return: fdt on success, or NULL errno on error.
+ */
+void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
+   unsigned long initrd_load_addr,
+   unsigned long initrd_len,
+   const char *cmdline)
+{
+void *fdt;
+int ret, chosen_node;
+const void *prop;
+unsigned long fdt_size;
+
+fdt_size = fdt_totalsize(initial_boot_params) +
+   (cmdline ? strlen(cmdline) : 0) +
+   FDT_EXTRA_SPACE;


Just adding 4 KB to initial_boot_params won't be enough for crash
kernels on ppc64. The current powerpc code doubles the size of
initial_boot_params (which is normally larger than 4 KB) and even that
isn't enough. A patch was added to powerpc/next today which uses a more
precise (but arch-specific) formula:

https://lore.kernel.org/linuxppc-dev/161243826811.119001.14083048209224609814.stgit@hbathini/

So I believe we need a hook here where architectures can provide their
own specific calculation for the size of the fdt. Perhaps a weakly
defined function providing a default implementation which an
arch-specific file can override (a la arch_kexec_kernel_image_load())?

Then the powerpc specific hook would be the kexec_fdt_totalsize_ppc64()
function from the patch I linked above.



Do you think it'd better to add "fdt_size" parameter to
of_kexec_alloc_and_setup_fdt() so that the caller can provide the
desired FDT buffer size?


Yes, I guess so. But please define the param as extra size, not total
size. The kernel command line size addition can be in the common code.


Will do. Just to clarify -

The common code will do:

fdt_totalsize(initial_boot_params) + strlen(cmdline) + extra_fdt_size

The caller will pass "extra_fdt_size"
ARM64 => 4KB
PPC64 => fdt_totalsize(initial_boot_params) - which will be updated when 
the patch Thiago had referred to is merged.




The above change is also going to conflict, so I think this may have
to wait. Or I'll take the common and arm bits and powerpc can be
converted next cycle (or after the merge window).



thanks.

 -lakshmi




Re: [PATCH v17 02/10] of: Add a common kexec FDT setup function

2021-02-12 Thread Lakshmi Ramasubramanian

On 2/12/21 10:24 AM, Rob Herring wrote:

On Fri, Feb 12, 2021 at 11:19 AM Lakshmi Ramasubramanian
 wrote:


On 2/12/21 6:38 AM, Rob Herring wrote:

On Thu, Feb 11, 2021 at 7:17 PM Lakshmi Ramasubramanian
 wrote:


On 2/11/21 5:09 PM, Thiago Jung Bauermann wrote:


There's actually a complication that I just noticed and needs to be
addressed. More below.



<...>


+
+/*
+ * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
+ *
+ * @image:  kexec image being loaded.
+ * @initrd_load_addr:   Address where the next initrd will be loaded.
+ * @initrd_len: Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:Command line for the next kernel, or NULL if there 
will
+ *  be none.
+ *
+ * Return: fdt on success, or NULL errno on error.
+ */
+void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
+   unsigned long initrd_load_addr,
+   unsigned long initrd_len,
+   const char *cmdline)
+{
+void *fdt;
+int ret, chosen_node;
+const void *prop;
+unsigned long fdt_size;
+
+fdt_size = fdt_totalsize(initial_boot_params) +
+   (cmdline ? strlen(cmdline) : 0) +
+   FDT_EXTRA_SPACE;


Just adding 4 KB to initial_boot_params won't be enough for crash
kernels on ppc64. The current powerpc code doubles the size of
initial_boot_params (which is normally larger than 4 KB) and even that
isn't enough. A patch was added to powerpc/next today which uses a more
precise (but arch-specific) formula:

https://lore.kernel.org/linuxppc-dev/161243826811.119001.14083048209224609814.stgit@hbathini/

So I believe we need a hook here where architectures can provide their
own specific calculation for the size of the fdt. Perhaps a weakly
defined function providing a default implementation which an
arch-specific file can override (a la arch_kexec_kernel_image_load())?

Then the powerpc specific hook would be the kexec_fdt_totalsize_ppc64()
function from the patch I linked above.



Do you think it'd better to add "fdt_size" parameter to
of_kexec_alloc_and_setup_fdt() so that the caller can provide the
desired FDT buffer size?


Yes, I guess so. But please define the param as extra size, not total
size. The kernel command line size addition can be in the common code.


Will do. Just to clarify -

The common code will do:

fdt_totalsize(initial_boot_params) + strlen(cmdline) + extra_fdt_size

The caller will pass "extra_fdt_size"
ARM64 => 4KB
PPC64 => fdt_totalsize(initial_boot_params) - which will be updated when
the patch Thiago had referred to is merged.


Yes, I'd leave the 4KB in there by default and arm64 use 0.



Sounds good.

common:
fdt_totalsize(initial_boot_params) + strlen(cmdline) + 0x1000 + extra

arm64 => 0 for extra
ppc => fdt_totalsize(initial_boot_params) for extra.

 -lakshmi



[PATCH v18 03/11] of: Add a common kexec FDT setup function

2021-02-13 Thread Lakshmi Ramasubramanian
From: Rob Herring 

Both arm64 and powerpc do essentially the same FDT /chosen setup for
kexec.  The differences are either omissions that arm64 should have
or additional properties that will be ignored.  The setup code can be
combined and shared by both powerpc and arm64.

The differences relative to the arm64 version:
 - If /chosen doesn't exist, it will be created (should never happen).
 - Any old dtb and initrd reserved memory will be released.
 - The new initrd and elfcorehdr are marked reserved.
 - "linux,booted-from-kexec" is set.

The differences relative to the powerpc version:
 - "kaslr-seed" and "rng-seed" may be set.
 - "linux,elfcorehdr" is set.
 - Any existing "linux,usable-memory-range" is removed.

Combine the code for setting up the /chosen node in the FDT and updating
the memory reservation for kexec, for powerpc and arm64, in
of_kexec_alloc_and_setup_fdt() and move it to "drivers/of/kexec.c".

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
 drivers/of/Makefile |   6 +
 drivers/of/kexec.c  | 265 
 include/linux/of.h  |   5 +
 3 files changed, 276 insertions(+)
 create mode 100644 drivers/of/kexec.c

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 6e1e5212f058..c13b982084a3 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -14,4 +14,10 @@ obj-$(CONFIG_OF_RESOLVE)  += resolver.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o
 obj-$(CONFIG_OF_NUMA) += of_numa.o
 
+ifdef CONFIG_KEXEC_FILE
+ifdef CONFIG_OF_FLATTREE
+obj-y  += kexec.o
+endif
+endif
+
 obj-$(CONFIG_OF_UNITTEST) += unittest-data/
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
new file mode 100644
index ..264cac0fd4cd
--- /dev/null
+++ b/drivers/of/kexec.c
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Arm Limited
+ *
+ * Based on arch/arm64/kernel/machine_kexec_file.c:
+ *  Copyright (C) 2018 Linaro Limited
+ *
+ * And arch/powerpc/kexec/file_load.c:
+ *  Copyright (C) 2016  IBM Corporation
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* relevant device tree properties */
+#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
+#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
+#define FDT_PROP_INITRD_START  "linux,initrd-start"
+#define FDT_PROP_INITRD_END"linux,initrd-end"
+#define FDT_PROP_BOOTARGS  "bootargs"
+#define FDT_PROP_KASLR_SEED"kaslr-seed"
+#define FDT_PROP_RNG_SEED  "rng-seed"
+#define RNG_SEED_SIZE  128
+
+/*
+ * Additional space needed for the FDT buffer so that we can add initrd,
+ * bootargs, kaslr-seed, rng-seed, useable-memory-range and elfcorehdr.
+ */
+#define FDT_EXTRA_SPACE 0x1000
+
+/**
+ * fdt_find_and_del_mem_rsv - delete memory reservation with given address and 
size
+ *
+ * @fdt:   Flattened device tree for the current kernel.
+ * @start: Starting address of the reserved memory.
+ * @size:  Size of the reserved memory.
+ *
+ * Return: 0 on success, or negative errno on error.
+ */
+static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned 
long size)
+{
+   int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
+
+   for (i = 0; i < num_rsvs; i++) {
+   u64 rsv_start, rsv_size;
+
+   ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
+   if (ret) {
+   pr_err("Malformed device tree.\n");
+   return -EINVAL;
+   }
+
+   if (rsv_start == start && rsv_size == size) {
+   ret = fdt_del_mem_rsv(fdt, i);
+   if (ret) {
+   pr_err("Error deleting device tree 
reservation.\n");
+   return -EINVAL;
+   }
+
+   return 0;
+   }
+   }
+
+   return -ENOENT;
+}
+
+/*
+ * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
+ *
+ * @image: kexec image being loaded.
+ * @initrd_load_addr:  Address where the next initrd will be loaded.
+ * @initrd_len:Size of the next initrd, or 0 if there will be 
none.
+ * @cmdline:   Command line for the next kernel, or NULL if there will
+ * be none.
+ * @extra_fdt_size:Additional size for the new FDT buffer.
+ *
+ * Return: fdt on success, or NULL errno on error.
+ */
+void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
+  unsigned long initrd_load_addr,
+  unsigned long initrd_len,
+  const char *cmdline, size_t extra_fdt_size)
+{
+   void *fdt;
+   int ret, chosen_node;
+   const void *pr

[PATCH v18 00/10] Carry forward IMA measurement log on kexec on ARM64

2021-02-13 Thread Lakshmi Ramasubramanian
 new file namely ima_kexec_fdt.c in IMA

v6:
  - Remove any existing FDT_PROP_IMA_KEXEC_BUFFER property in the device
tree and also its corresponding memory reservation in the currently
running kernel.
  - Moved the function remove_ima_buffer() defined for powerpc to IMA
and renamed the function to ima_remove_kexec_buffer(). Also, moved
delete_fdt_mem_rsv() from powerpc to IMA.

v5:
  - Merged get_addr_size_cells() and do_get_kexec_buffer() into a single
function when moving the arch independent code from powerpc to IMA
  - Reverted the change to use FDT functions in powerpc code and added
back the original code in get_addr_size_cells() and
do_get_kexec_buffer() for powerpc.
  - Added fdt_add_mem_rsv() for ARM64 to reserve the memory for
the IMA log buffer during kexec.
  - Fixed the warning reported by kernel test bot for ARM64
arch_ima_add_kexec_buffer() - moved this function to a new file
namely arch/arm64/kernel/ima_kexec.c

v4:
  - Submitting the patch series on behalf of the original author
Prakhar Srivastava 
  - Moved FDT_PROP_IMA_KEXEC_BUFFER ("linux,ima-kexec-buffer") to
libfdt.h so that it can be shared by multiple platforms.

v3:
Breakup patches further into separate patches.
  - Refactoring non architecture specific code out of powerpc
  - Update powerpc related code to use fdt functions
  - Update IMA buffer read related code to use of functions
  - Add support to store the memory information of the IMA
measurement logs to be carried forward.
  - Update the property strings to align with documented nodes
https://github.com/devicetree-org/dt-schema/pull/46

v2:
  Break patches into separate patches.
  - Powerpc related Refactoring
  - Updating the docuemntation for chosen node
  - Updating arm64 to support IMA buffer pass

v1:
  Refactoring carrying over IMA measuremnet logs over Kexec. This patch
moves the non-architecture specific code out of powerpc and adds to
security/ima.(Suggested by Thiago)
  Add Documentation regarding the ima-kexec-buffer node in the chosen
node documentation

v0:
  Add a layer of abstraction to use the memory reserved by device tree
for ima buffer pass.
  Add support for ima buffer pass using reserved memory for arm64 kexec.
    Update the arch sepcific code path in kexec file load to store the
ima buffer in the reserved memory. The same reserved memory is read
on kexec or cold boot.

Lakshmi Ramasubramanian (7):
  arm64: Rename kexec elf_headers_mem to elf_load_addr
  powerpc: Move ima buffer fields to struct kimage
  powerpc: Enable passing IMA log to next kernel on kexec
  powerpc: Move arch independent ima kexec functions to
drivers/of/kexec.c
  kexec: Use fdt_appendprop_addrrange() to add ima buffer to FDT
  powerpc: Delete unused function delete_fdt_mem_rsv()
  arm64: Enable passing IMA log to next kernel on kexec

Rob Herring (4):
  powerpc: Rename kexec elfcorehdr_addr to elf_load_addr
  of: Add a common kexec FDT setup function
  arm64: Use common of_kexec_alloc_and_setup_fdt()
  powerpc: Use common of_kexec_alloc_and_setup_fdt()

 arch/arm64/Kconfig |   1 +
 arch/arm64/include/asm/kexec.h |   2 +-
 arch/arm64/kernel/machine_kexec_file.c | 184 +-
 arch/powerpc/Kconfig   |   2 +-
 arch/powerpc/include/asm/ima.h |  30 --
 arch/powerpc/include/asm/kexec.h   |  12 +-
 arch/powerpc/kexec/Makefile|   7 -
 arch/powerpc/kexec/elf_64.c|  30 +-
 arch/powerpc/kexec/file_load.c | 183 +-
 arch/powerpc/kexec/file_load_64.c  |  11 +-
 arch/powerpc/kexec/ima.c   | 219 
 drivers/of/Makefile|   6 +
 drivers/of/kexec.c | 458 +
 include/linux/kexec.h  |   3 +
 include/linux/of.h |   7 +
 security/integrity/ima/ima.h   |   4 -
 security/integrity/ima/ima_kexec.c |   9 +-
 17 files changed, 516 insertions(+), 652 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/ima.h
 delete mode 100644 arch/powerpc/kexec/ima.c
 create mode 100644 drivers/of/kexec.c

-- 
2.30.0



[PATCH v18 04/11] arm64: Use common of_kexec_alloc_and_setup_fdt()

2021-02-13 Thread Lakshmi Ramasubramanian
From: Rob Herring 

The code for setting up the /chosen node in the device tree
and updating the memory reservation for the next kernel has been
moved to of_kexec_alloc_and_setup_fdt() defined in "drivers/of/kexec.c".

Use the common of_kexec_alloc_and_setup_fdt() to setup the device tree
and update the memory reservation for kexec for arm64.

Signed-off-by: Rob Herring 
Signed-off-by: Lakshmi Ramasubramanian 
---
 arch/arm64/kernel/machine_kexec_file.c | 180 ++---
 1 file changed, 8 insertions(+), 172 deletions(-)

diff --git a/arch/arm64/kernel/machine_kexec_file.c 
b/arch/arm64/kernel/machine_kexec_file.c
index d98bacec9426..c9f112455a4d 100644
--- a/arch/arm64/kernel/machine_kexec_file.c
+++ b/arch/arm64/kernel/machine_kexec_file.c
@@ -15,23 +15,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
-#include 
 #include 
 #include 
 #include 
 #include 
-#include 
-
-/* relevant device tree properties */
-#define FDT_PROP_KEXEC_ELFHDR  "linux,elfcorehdr"
-#define FDT_PROP_MEM_RANGE "linux,usable-memory-range"
-#define FDT_PROP_INITRD_START  "linux,initrd-start"
-#define FDT_PROP_INITRD_END"linux,initrd-end"
-#define FDT_PROP_BOOTARGS  "bootargs"
-#define FDT_PROP_KASLR_SEED"kaslr-seed"
-#define FDT_PROP_RNG_SEED  "rng-seed"
-#define RNG_SEED_SIZE  128
 
 const struct kexec_file_ops * const kexec_file_loaders[] = {
&kexec_image_ops,
@@ -40,7 +29,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 
 int arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-   vfree(image->arch.dtb);
+   kvfree(image->arch.dtb);
image->arch.dtb = NULL;
 
vfree(image->arch.elf_headers);
@@ -50,162 +39,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image)
return kexec_image_post_load_cleanup_default(image);
 }
 
-static int setup_dtb(struct kimage *image,
-unsigned long initrd_load_addr, unsigned long initrd_len,
-char *cmdline, void *dtb)
-{
-   int off, ret;
-
-   ret = fdt_path_offset(dtb, "/chosen");
-   if (ret < 0)
-   goto out;
-
-   off = ret;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-   ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE);
-   if (ret && ret != -FDT_ERR_NOTFOUND)
-   goto out;
-
-   if (image->type == KEXEC_TYPE_CRASH) {
-   /* add linux,elfcorehdr */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_KEXEC_ELFHDR,
-   image->arch.elf_load_addr,
-   image->arch.elf_headers_sz);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-
-   /* add linux,usable-memory-range */
-   ret = fdt_appendprop_addrrange(dtb, 0, off,
-   FDT_PROP_MEM_RANGE,
-   crashk_res.start,
-   crashk_res.end - crashk_res.start + 1);
-   if (ret)
-   return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
-   }
-
-   /* add bootargs */
-   if (cmdline) {
-   ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add initrd-* */
-   if (initrd_load_addr) {
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
- initrd_load_addr);
-   if (ret)
-   goto out;
-
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
- initrd_load_addr + initrd_len);
-   if (ret)
-   goto out;
-   } else {
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-
-   ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
-   if (ret && (ret != -FDT_ERR_NOTFOUND))
-   goto out;
-   }
-
-   /* add kaslr-seed */
-   ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
-   if (ret == -FDT_ERR_NOTFOUND)
-   ret = 0;
-   else if (ret)
-   goto out;
-
-   if (rng_is_initialized()) {
-   u64 seed = get_random_u64();
-   ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
-   if (ret)
-   

  1   2   >