All of lore.kernel.org
 help / color / mirror / Atom feed
* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-02 15:56 Tadeusz Struk
  0 siblings, 0 replies; 6+ messages in thread
From: Tadeusz Struk @ 2019-10-02 15:56 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 934 bytes --]

On 10/2/19 7:16 AM, Roberts, William C wrote:
>> wait.. i should be doing "negmagic" and not -negmagic in the function call! Maybe
>> that the cause of the bug!
> Yeah -- would make it positive and you would seek too far.
> 
> I'm not sure though why you have some unsigned issue on windows, fseek takes a long which is signed.

To avoid dealing with a negative offset you could do something like this:

diff --git a/lib/files.c b/lib/files.c
index d45ead87..9da1543a 100644
--- a/lib/files.c
+++ b/lib/files.c
@@ -335,7 +335,7 @@ static bool check_magic(FILE *fstream, bool seek_reset) {
     bool match = magic == MAGIC;
 
     if (seek_reset) {
-        int rc = fseek(fstream, -sizeof(magic), SEEK_CUR);
+        int rc = fseek(fstream, ftell(fstream) - sizeof(magic), SEEK_SET);
         if (rc != 0) {
             LOG_ERR("fseek failed: %s", strerror(errno));
             return false;
  

-- 
Tadeusz

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-02 14:16 Roberts, William C
  0 siblings, 0 replies; 6+ messages in thread
From: Roberts, William C @ 2019-10-02 14:16 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 3168 bytes --]



> -----Original Message-----
> From: Arun Sudhir [mailto:arunsudhir19(a)gmail.com]
> Sent: Tuesday, October 1, 2019 4:16 PM
> To: Struk, Tadeusz <tadeusz.struk(a)intel.com>
> Cc: tpm2(a)lists.01.org
> Subject: [tpm2] Re: Error running tpm2_create: magic does not match!
> 
> wait.. i should be doing "negmagic" and not -negmagic in the function call! Maybe
> that the cause of the bug!

Yeah -- would make it positive and you would seek too far.

I'm not sure though why you have some unsigned issue on windows, fseek takes a long which is signed.
 
> 
> On Tue, Oct 1, 2019 at 4:02 PM Arun Sudhir <arunsudhir19(a)gmail.com
> <mailto:arunsudhir19(a)gmail.com> > wrote:
> 
> 
> 	I found the issue:
> 
> 	Its with the part i have highlighted in bold below in the check_magic()
> function in files.c:
> 
> 	 long negmagic = 0 - sizeof(magic);
> 	    if (seek_reset) {
> 	        int rc = fseek(fstream, -negmagic, SEEK_CUR);
> 
> 
> 
> 	Initially, the code does a check_magic with seek_reset = true. So it tries
> to do an fseek backwards by 4 bytes . I remember i had to modify this code to
> port to windows as clang on windows would not compile with a  unary minus on
> an unsigned int in windows. That seems to be the issue.  It instead goes forward
> by 4  bytes ! and the next check_magic fails which is called again from
> files_read_header function (Also in files.c).
> 
> 	I skipped over the fseek and the second check_magic call and everything
> worked fine.
> 
> 	I someone knows how to fix this (by properly fixing fseek) , please let me
> know. For now, I have introduced a boolean flag that skips the second
> check_magic and gets rid of the fseek altogether.
> 
> 	Thanks Tadeusz (again)  for your help!
> 
> 	Arun
> 
> 	On Tue, Oct 1, 2019 at 2:30 PM Arun Sudhir <arunsudhir19(a)gmail.com
> <mailto:arunsudhir19(a)gmail.com> > wrote:
> 
> 
> 		I will try that.
> 
> 		On Tue, Oct 1, 2019, 2:25 PM Tadeusz Struk
> <tadeusz.struk(a)intel.com <mailto:tadeusz.struk(a)intel.com> > wrote:
> 
> 
> 			On 10/1/19 11:50 AM, Arun Sudhir wrote:
> 			> HEre are the commands i used:
> 			>
> 			> |tpm2_createprimary -c primary.ctx (works)
> |tpm2_create -C primary.ctx -u obj.pub -r obj.priv (fails)
> 			>
> 			> ||
> 			>
> 			>
> 			> This is the error i get:
> 			>
> 			> ERROR: Found magic 0x40000001 did not match
> expected magic of 0xbadcc0de!
> 			> WARN: The loaded tpm context does not appear to be
> in the proper format, assuming old format, this will be converted on the next
> save.
> 			> ERROR: Could not load tpm context file
> 			> ERROR: Failed to load_tpm_context_file()
> 			> ERROR: Unable to run
> C:\Users\arunsu\source\repos\tpm2-tools-4.0\vstudio\x64\Debug\tpm2-
> create.exe
> 
> 			You can try to hex dump the primary.ctx after
> tpm2_createprimary() to see what's in it.
> 			The format should be:
> 
> 			U32 magic (0xBADCC0DE)
> 			U32 ctx version (0x00000001)
> 			U32 savedHandle
> 			U64 sequence
> 			U16 contextBlobLength
> 			BYTE[] contextBlob
> 
> 			The 0x40000001 looks like the owner handle.
> 
> 			--
> 			Tadeusz
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-01 23:16 Arun Sudhir
  0 siblings, 0 replies; 6+ messages in thread
From: Arun Sudhir @ 2019-10-01 23:16 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 2541 bytes --]

wait.. i should be doing "negmagic" and not -negmagic in the function call!
Maybe that the cause of the bug!

On Tue, Oct 1, 2019 at 4:02 PM Arun Sudhir <arunsudhir19(a)gmail.com> wrote:

> I found the issue:
>
> Its with the part i have highlighted in bold below in the check_magic()
> function in files.c:
>
>
>
> * long negmagic = 0 - sizeof(magic);    if (seek_reset) {        int rc =
> fseek(fstream, -negmagic, SEEK_CUR);*
>
> Initially, the code does a check_magic with seek_reset = true. So it tries
> to do an fseek backwards by 4 bytes . I remember i had to modify this code
> to port to windows as clang on windows would not compile with a  unary
> minus on an unsigned int in windows. That seems to be the issue.  It
> instead goes forward by 4  bytes ! and the next check_magic fails which is
> called again from *files_read_header *function (Also in files.c).
>
> I skipped over the fseek and the second check_magic call and everything
> worked fine.
>
> I someone knows how to fix this (by properly fixing fseek) , please let me
> know. For now, I have introduced a boolean flag that skips the second
> check_magic and gets rid of the fseek altogether.
>
> Thanks Tadeusz (again)  for your help!
>
> Arun
>
> On Tue, Oct 1, 2019 at 2:30 PM Arun Sudhir <arunsudhir19(a)gmail.com> wrote:
>
>> I will try that.
>>
>> On Tue, Oct 1, 2019, 2:25 PM Tadeusz Struk <tadeusz.struk(a)intel.com>
>> wrote:
>>
>>> On 10/1/19 11:50 AM, Arun Sudhir wrote:
>>> > HEre are the commands i used:
>>> >
>>> > |tpm2_createprimary -c primary.ctx (works)
>>> |tpm2_create -C primary.ctx -u obj.pub -r obj.priv (fails)
>>> >
>>> > ||
>>> >
>>> >
>>> > This is the error i get:
>>> >
>>> > ERROR: Found magic 0x40000001 did not match expected magic of
>>> 0xbadcc0de!
>>> > WARN: The loaded tpm context does not appear to be in the proper
>>> format, assuming old format, this will be converted on the next save.
>>> > ERROR: Could not load tpm context file
>>> > ERROR: Failed to load_tpm_context_file()
>>> > ERROR: Unable to run
>>> C:\Users\arunsu\source\repos\tpm2-tools-4.0\vstudio\x64\Debug\tpm2-create.exe
>>>
>>> You can try to hex dump the primary.ctx after tpm2_createprimary() to
>>> see what's in it.
>>> The format should be:
>>>
>>> U32 magic (0xBADCC0DE)
>>> U32 ctx version (0x00000001)
>>> U32 savedHandle
>>> U64 sequence
>>> U16 contextBlobLength
>>> BYTE[] contextBlob
>>>
>>> The 0x40000001 looks like the owner handle.
>>>
>>> --
>>> Tadeusz
>>>
>>

[-- Attachment #2: attachment.html --]
[-- Type: text/html, Size: 3538 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-01 23:02 Arun Sudhir
  0 siblings, 0 replies; 6+ messages in thread
From: Arun Sudhir @ 2019-10-01 23:02 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 2257 bytes --]

I found the issue:

Its with the part i have highlighted in bold below in the check_magic()
function in files.c:



* long negmagic = 0 - sizeof(magic);    if (seek_reset) {        int rc =
fseek(fstream, -negmagic, SEEK_CUR);*

Initially, the code does a check_magic with seek_reset = true. So it tries
to do an fseek backwards by 4 bytes . I remember i had to modify this code
to port to windows as clang on windows would not compile with a  unary
minus on an unsigned int in windows. That seems to be the issue.  It
instead goes forward by 4  bytes ! and the next check_magic fails which is
called again from *files_read_header *function (Also in files.c).

I skipped over the fseek and the second check_magic call and everything
worked fine.

I someone knows how to fix this (by properly fixing fseek) , please let me
know. For now, I have introduced a boolean flag that skips the second
check_magic and gets rid of the fseek altogether.

Thanks Tadeusz (again)  for your help!

Arun

On Tue, Oct 1, 2019 at 2:30 PM Arun Sudhir <arunsudhir19(a)gmail.com> wrote:

> I will try that.
>
> On Tue, Oct 1, 2019, 2:25 PM Tadeusz Struk <tadeusz.struk(a)intel.com>
> wrote:
>
>> On 10/1/19 11:50 AM, Arun Sudhir wrote:
>> > HEre are the commands i used:
>> >
>> > |tpm2_createprimary -c primary.ctx (works)
>> |tpm2_create -C primary.ctx -u obj.pub -r obj.priv (fails)
>> >
>> > ||
>> >
>> >
>> > This is the error i get:
>> >
>> > ERROR: Found magic 0x40000001 did not match expected magic of
>> 0xbadcc0de!
>> > WARN: The loaded tpm context does not appear to be in the proper
>> format, assuming old format, this will be converted on the next save.
>> > ERROR: Could not load tpm context file
>> > ERROR: Failed to load_tpm_context_file()
>> > ERROR: Unable to run
>> C:\Users\arunsu\source\repos\tpm2-tools-4.0\vstudio\x64\Debug\tpm2-create.exe
>>
>> You can try to hex dump the primary.ctx after tpm2_createprimary() to see
>> what's in it.
>> The format should be:
>>
>> U32 magic (0xBADCC0DE)
>> U32 ctx version (0x00000001)
>> U32 savedHandle
>> U64 sequence
>> U16 contextBlobLength
>> BYTE[] contextBlob
>>
>> The 0x40000001 looks like the owner handle.
>>
>> --
>> Tadeusz
>>
>

[-- Attachment #2: attachment.html --]
[-- Type: text/html, Size: 3044 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-01 21:30 Arun Sudhir
  0 siblings, 0 replies; 6+ messages in thread
From: Arun Sudhir @ 2019-10-01 21:30 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 1109 bytes --]

I will try that.

On Tue, Oct 1, 2019, 2:25 PM Tadeusz Struk <tadeusz.struk(a)intel.com> wrote:

> On 10/1/19 11:50 AM, Arun Sudhir wrote:
> > HEre are the commands i used:
> >
> > |tpm2_createprimary -c primary.ctx (works)
> |tpm2_create -C primary.ctx -u obj.pub -r obj.priv (fails)
> >
> > ||
> >
> >
> > This is the error i get:
> >
> > ERROR: Found magic 0x40000001 did not match expected magic of 0xbadcc0de!
> > WARN: The loaded tpm context does not appear to be in the proper format,
> assuming old format, this will be converted on the next save.
> > ERROR: Could not load tpm context file
> > ERROR: Failed to load_tpm_context_file()
> > ERROR: Unable to run
> C:\Users\arunsu\source\repos\tpm2-tools-4.0\vstudio\x64\Debug\tpm2-create.exe
>
> You can try to hex dump the primary.ctx after tpm2_createprimary() to see
> what's in it.
> The format should be:
>
> U32 magic (0xBADCC0DE)
> U32 ctx version (0x00000001)
> U32 savedHandle
> U64 sequence
> U16 contextBlobLength
> BYTE[] contextBlob
>
> The 0x40000001 looks like the owner handle.
>
> --
> Tadeusz
>

[-- Attachment #2: attachment.html --]
[-- Type: text/html, Size: 1450 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [tpm2] Re: Error running tpm2_create: magic does not match!
@ 2019-10-01 21:25 Tadeusz Struk
  0 siblings, 0 replies; 6+ messages in thread
From: Tadeusz Struk @ 2019-10-01 21:25 UTC (permalink / raw)
  To: tpm2

[-- Attachment #1: Type: text/plain, Size: 952 bytes --]

On 10/1/19 11:50 AM, Arun Sudhir wrote:
> HEre are the commands i used:
> 
> |tpm2_createprimary -c primary.ctx (works) |tpm2_create -C primary.ctx -u obj.pub -r obj.priv (fails)
> 
> ||
> 
> 
> This is the error i get: 
> 
> ERROR: Found magic 0x40000001 did not match expected magic of 0xbadcc0de!
> WARN: The loaded tpm context does not appear to be in the proper format, assuming old format, this will be converted on the next save.
> ERROR: Could not load tpm context file
> ERROR: Failed to load_tpm_context_file()
> ERROR: Unable to run C:\Users\arunsu\source\repos\tpm2-tools-4.0\vstudio\x64\Debug\tpm2-create.exe

You can try to hex dump the primary.ctx after tpm2_createprimary() to see what's in it.
The format should be:

U32 magic (0xBADCC0DE)
U32 ctx version (0x00000001)
U32 savedHandle
U64 sequence
U16 contextBlobLength
BYTE[] contextBlob
 
The 0x40000001 looks like the owner handle.

-- 
Tadeusz

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-10-02 15:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-02 15:56 [tpm2] Re: Error running tpm2_create: magic does not match! Tadeusz Struk
  -- strict thread matches above, loose matches on Subject: below --
2019-10-02 14:16 Roberts, William C
2019-10-01 23:16 Arun Sudhir
2019-10-01 23:02 Arun Sudhir
2019-10-01 21:30 Arun Sudhir
2019-10-01 21:25 Tadeusz Struk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.