<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>i.MX ProcessorsのトピックUpdate the linux secure_key patch to match the keyctl.c api changes</title>
    <link>https://community.nxp.com/t5/i-MX-Processors/Update-the-linux-secure-key-patch-to-match-the-keyctl-c-api/m-p/1343517#M180424</link>
    <description>&lt;P&gt;The keyctl api used to call into the read for the key types and expect them to copy the key data to user space, but that was changed to avoid false ENOMEM&lt;/P&gt;&lt;PRE&gt;commit 419d8fb1630cbb04883fc73e08f37400a1e8ce86&lt;BR /&gt;Author: Waiman Long &amp;lt;longman@redhat.com&amp;gt;&lt;BR /&gt;Date: Sat Mar 21 21:11:25 2020 -0400&lt;BR /&gt;&lt;BR /&gt;KEYS: Avoid false positive ENOMEM error on key read&lt;BR /&gt;&lt;BR /&gt;[ Upstream commit 4f0882491a148059a52480e753b7f07fc550e188 ]&lt;BR /&gt;&lt;BR /&gt;By allocating a kernel buffer with a user-supplied buffer length, it&lt;BR /&gt;is possible that a false positive ENOMEM error may be returned because&lt;BR /&gt;the user-supplied length is just too large even if the system do have&lt;BR /&gt;enough memory to hold the actual key data.&lt;BR /&gt;&lt;BR /&gt;Moreover, if the buffer length is larger than the maximum amount of&lt;BR /&gt;memory that can be returned by kmalloc() (2^(MAX_ORDER-1) number of&lt;BR /&gt;pages), a warning message will also be printed.&lt;BR /&gt;&lt;BR /&gt;To reduce this possibility, we set a threshold (PAGE_SIZE) over which we&lt;BR /&gt;do check the actual key length first before allocating a buffer of the&lt;BR /&gt;right size to hold it. The threshold is arbitrary, it is just used to&lt;BR /&gt;trigger a buffer length check. It does not limit the actual key length&lt;BR /&gt;as long as there is enough memory to satisfy the memory request.&lt;BR /&gt;&lt;BR /&gt;To further avoid large buffer allocation failure due to page&lt;BR /&gt;fragmentation, kvmalloc() is used to allocate the buffer so that vmapped&lt;BR /&gt;pages can be used when there is not a large enough contiguous set of&lt;BR /&gt;pages available for allocation.&lt;BR /&gt;&lt;BR /&gt;In the extremely unlikely scenario that the key keeps on being changed&lt;BR /&gt;and made longer (still &amp;lt;= buflen) in between 2 __keyctl_read_key()&lt;BR /&gt;calls, the __keyctl_read_key() calling loop in keyctl_read_key() may&lt;BR /&gt;have to be iterated a large number of times, but definitely not infinite.&lt;BR /&gt;&lt;BR /&gt;Signed-off-by: Waiman Long &amp;lt;longman@redhat.com&amp;gt;&lt;BR /&gt;Signed-off-by: David Howells &amp;lt;dhowells@redhat.com&amp;gt;&lt;BR /&gt;Signed-off-by: Sasha Levin &amp;lt;sashal@kernel.org&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;So, this means that the secure key code would get a kernel buffer and return EFAULT instead of just a pack into the kernel memory. The diff below fixes this problem on the lf-5.10.y branch - not sure if this is already fixed in another branch.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;diff --git a/security/keys/secure_key.c b/security/keys/secure_key.c
index ec8ad4394549..623513bd3fd1 100644
--- a/security/keys/secure_key.c
+++ b/security/keys/secure_key.c
@@ -228,17 +228,16 @@ static int secure_instantiate(struct key *key,
 }
 
 /*
- * secure_read - copy the  blob data to userspace in hex.
+ * secure_read - copy the blob data eventually to userspace in hex.
  * param[in]: key pointer to key struct
- * param[in]: buffer pointer to user data for creating key
+ * param[in]: buffer pointer to kernel memory for storing key
  * param[in]: buflen is the length of the buffer
- * On success, return to userspace the secure key data size.
+ * On success, return the secure key data size.
  */
-static long secure_read(const struct key *key, char __user *buffer,
+static long secure_read(const struct key *key, char *buffer,
 			 size_t buflen)
 {
-	const struct secure_key_payload *p = NULL;
-	char *ascii_buf;
+	const struct secure_key_payload *p;
 	char *bufp;
 	int i;
 
@@ -247,18 +246,9 @@ static long secure_read(const struct key *key, char __user *buffer,
 		return -EINVAL;
 
 	if (buffer &amp;amp;&amp;amp; buflen &amp;gt;= 2 * p-&amp;gt;blob_len) {
-		ascii_buf = kmalloc(2 * p-&amp;gt;blob_len, GFP_KERNEL);
-		if (!ascii_buf)
-			return -ENOMEM;
-
-		bufp = ascii_buf;
+		bufp = buffer;
 		for (i = 0; i &amp;lt; p-&amp;gt;blob_len; i++)
 			bufp = hex_byte_pack(bufp, p-&amp;gt;blob[i]);
-		if (copy_to_user(buffer, ascii_buf, 2 * p-&amp;gt;blob_len) != 0) {
-			kzfree(ascii_buf);
-			return -EFAULT;
-		}
-		kzfree(ascii_buf);
 	}
 	return 2 * p-&amp;gt;blob_len;
 }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Sep 2021 00:59:01 GMT</pubDate>
    <dc:creator>charles_hardin</dc:creator>
    <dc:date>2021-09-22T00:59:01Z</dc:date>
    <item>
      <title>Update the linux secure_key patch to match the keyctl.c api changes</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/Update-the-linux-secure-key-patch-to-match-the-keyctl-c-api/m-p/1343517#M180424</link>
      <description>&lt;P&gt;The keyctl api used to call into the read for the key types and expect them to copy the key data to user space, but that was changed to avoid false ENOMEM&lt;/P&gt;&lt;PRE&gt;commit 419d8fb1630cbb04883fc73e08f37400a1e8ce86&lt;BR /&gt;Author: Waiman Long &amp;lt;longman@redhat.com&amp;gt;&lt;BR /&gt;Date: Sat Mar 21 21:11:25 2020 -0400&lt;BR /&gt;&lt;BR /&gt;KEYS: Avoid false positive ENOMEM error on key read&lt;BR /&gt;&lt;BR /&gt;[ Upstream commit 4f0882491a148059a52480e753b7f07fc550e188 ]&lt;BR /&gt;&lt;BR /&gt;By allocating a kernel buffer with a user-supplied buffer length, it&lt;BR /&gt;is possible that a false positive ENOMEM error may be returned because&lt;BR /&gt;the user-supplied length is just too large even if the system do have&lt;BR /&gt;enough memory to hold the actual key data.&lt;BR /&gt;&lt;BR /&gt;Moreover, if the buffer length is larger than the maximum amount of&lt;BR /&gt;memory that can be returned by kmalloc() (2^(MAX_ORDER-1) number of&lt;BR /&gt;pages), a warning message will also be printed.&lt;BR /&gt;&lt;BR /&gt;To reduce this possibility, we set a threshold (PAGE_SIZE) over which we&lt;BR /&gt;do check the actual key length first before allocating a buffer of the&lt;BR /&gt;right size to hold it. The threshold is arbitrary, it is just used to&lt;BR /&gt;trigger a buffer length check. It does not limit the actual key length&lt;BR /&gt;as long as there is enough memory to satisfy the memory request.&lt;BR /&gt;&lt;BR /&gt;To further avoid large buffer allocation failure due to page&lt;BR /&gt;fragmentation, kvmalloc() is used to allocate the buffer so that vmapped&lt;BR /&gt;pages can be used when there is not a large enough contiguous set of&lt;BR /&gt;pages available for allocation.&lt;BR /&gt;&lt;BR /&gt;In the extremely unlikely scenario that the key keeps on being changed&lt;BR /&gt;and made longer (still &amp;lt;= buflen) in between 2 __keyctl_read_key()&lt;BR /&gt;calls, the __keyctl_read_key() calling loop in keyctl_read_key() may&lt;BR /&gt;have to be iterated a large number of times, but definitely not infinite.&lt;BR /&gt;&lt;BR /&gt;Signed-off-by: Waiman Long &amp;lt;longman@redhat.com&amp;gt;&lt;BR /&gt;Signed-off-by: David Howells &amp;lt;dhowells@redhat.com&amp;gt;&lt;BR /&gt;Signed-off-by: Sasha Levin &amp;lt;sashal@kernel.org&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;So, this means that the secure key code would get a kernel buffer and return EFAULT instead of just a pack into the kernel memory. The diff below fixes this problem on the lf-5.10.y branch - not sure if this is already fixed in another branch.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;diff --git a/security/keys/secure_key.c b/security/keys/secure_key.c
index ec8ad4394549..623513bd3fd1 100644
--- a/security/keys/secure_key.c
+++ b/security/keys/secure_key.c
@@ -228,17 +228,16 @@ static int secure_instantiate(struct key *key,
 }
 
 /*
- * secure_read - copy the  blob data to userspace in hex.
+ * secure_read - copy the blob data eventually to userspace in hex.
  * param[in]: key pointer to key struct
- * param[in]: buffer pointer to user data for creating key
+ * param[in]: buffer pointer to kernel memory for storing key
  * param[in]: buflen is the length of the buffer
- * On success, return to userspace the secure key data size.
+ * On success, return the secure key data size.
  */
-static long secure_read(const struct key *key, char __user *buffer,
+static long secure_read(const struct key *key, char *buffer,
 			 size_t buflen)
 {
-	const struct secure_key_payload *p = NULL;
-	char *ascii_buf;
+	const struct secure_key_payload *p;
 	char *bufp;
 	int i;
 
@@ -247,18 +246,9 @@ static long secure_read(const struct key *key, char __user *buffer,
 		return -EINVAL;
 
 	if (buffer &amp;amp;&amp;amp; buflen &amp;gt;= 2 * p-&amp;gt;blob_len) {
-		ascii_buf = kmalloc(2 * p-&amp;gt;blob_len, GFP_KERNEL);
-		if (!ascii_buf)
-			return -ENOMEM;
-
-		bufp = ascii_buf;
+		bufp = buffer;
 		for (i = 0; i &amp;lt; p-&amp;gt;blob_len; i++)
 			bufp = hex_byte_pack(bufp, p-&amp;gt;blob[i]);
-		if (copy_to_user(buffer, ascii_buf, 2 * p-&amp;gt;blob_len) != 0) {
-			kzfree(ascii_buf);
-			return -EFAULT;
-		}
-		kzfree(ascii_buf);
 	}
 	return 2 * p-&amp;gt;blob_len;
 }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 00:59:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/Update-the-linux-secure-key-patch-to-match-the-keyctl-c-api/m-p/1343517#M180424</guid>
      <dc:creator>charles_hardin</dc:creator>
      <dc:date>2021-09-22T00:59:01Z</dc:date>
    </item>
  </channel>
</rss>

