Project

General

Profile

Actions

Bug #5418

closed

one cannot run 'export' after an 'export'

Added by laforge about 2 years ago. Updated 3 months ago.

Status:
Resolved
Priority:
Low
Assignee:
Category:
pySim-shell
Target version:
-
Start date:
01/25/2022
Due date:
% Done:

100%

Spec Reference:

Description

pySIM-shell (MF)> export
...
# skipped dedicated files(s): 2
#  MF/DF.EIRENE, 6a82: Wrong parameters - File not found
#  MF/ADF.ARA-M/ADF.ARA-M, '62'
EXCEPTION of type 'RuntimeError' occurred with message: 'unable to export 47 elementary file(s) and 2 dedicated file(s)'
To enable full traceback, run the following command: 'set debug true'

and If I then try

pySIM-shell (MF)> export
################################################################################
# Export summary                                                               #
################################################################################
# total files visited: 0
# bad files:           0
# skipped dedicated files(s): 0
pySIM-shell (MF)> 


Related issues

Related to SIMtrace 2 - Bug #5421: cardem: failure to correctly detect end of reset of CardMan 3121Resolvedlaforge01/26/2022

Actions
Related to pySim - Bug #6120: select MF raise an exception after selecting ADF.ARA-MNewdexter07/29/2023

Actions
Actions #1

Updated by dexter about 2 years ago

There is something wrong with MF/ADF.ARA-M. When it is selected, the following happens:

pySIM-shell (MF)> select ADF.ARA-M
Traceback (most recent call last):
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2129, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/owner/.local/lib/python3.7/site-packages/cmd2/cmd2.py", line 2559, in onecmd
    stop = func(statement)
  File "./pySim-shell.py", line 641, in do_select
    fcp_dec = self._cmd.rs.select(path, self._cmd)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 1238, in select
    select_resp = f.decode_select_response(data)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 192, in decode_select_response
    return self.parent.decode_select_response(data_hex)
  File "/home/owner/work/git_master/pysim/pySim/filesystem.py", line 371, in decode_select_response
    return profile.decode_select_response(data_hex)
  File "/home/owner/work/git_master/pysim/pySim/ts_102_221.py", line 696, in decode_select_response
    fcp = fcp_tlv.parse(fcp_base['62'])
KeyError: '62'
EXCEPTION of type 'KeyError' occurred with message: ''62''
pySIM-shell (MF)> 

after that all files in the directory are gone and it is not possible to select MF anymore. That is why export only works once. As soon as it hits ADF.ARA-M the problem is triggered. (The tree command also triggers the problem.)

Actions #2

Updated by laforge about 2 years ago

  • Related to Bug #5421: cardem: failure to correctly detect end of reset of CardMan 3121 added
Actions #3

Updated by laforge about 2 years ago

dexter wrote in #note-1:

after that all files in the directory are gone and it is not possible to select MF anymore.

Are you sure it's not just simply us sending the wrong CLA byte? Maybe ARA-M requires different CLA than normal UICC/SIM.

Actions #4

Updated by dexter about 2 years ago

What I can see so far is that the select works. It returns with 9000, but once ADF.ARA-M the new CLA seems to get valid and that gets us trapped because we cannot even select the MF anymore using the UICC APDUs.

pySIM-shell (MF/ADF.ARA-M)> select MF
EXCEPTION of type 'RuntimeError' occurred with message: '6e00: ARA-M - Invalid class'
Actions #5

Updated by laforge about 2 years ago

I've played around a bit with various CLA values, and if 0x80 is used, the resposne suddenly becomes "6D00 == invalid instruction". So the applet supports CLA=0x80 but no INS=0xA4 (SELECT).

Looking at the source code seems to confirm this: https://github.com/bertrandmartel/aram-applet/blob/master/aram/src/main/java/fr/bmartel/aram/AccessRuleMaster.java#L77 - any CLA != 0x80 is rejected, and the only INS supported are STORE_DATA and GET_DATA.

I've filed an issue with the ARA-M applet project at https://github.com/bertrandmartel/aram-applet/issues/7

In future cards we can of course install an applet with explicit de-select capability. However, until that point, I think it is best if we black-list the ARA-M AID from export. After all, there are no files specified in he ARA-M specification.

Actions #6

Updated by laforge about 2 years ago

reading https://www.win.tue.nl/pinpasjc/docs/apis/jc222/javacard/framework/Applet.html and https://stackoverflow.com/questions/26816690/what-is-the-functionality-of-selectingapplet-method-in-javacard2-2 it looks lmore like the SELECT APDU should be captured/interpreted by the JCRE (java card runtime environment)...

And indeed, selecting other AIDs still works. IF I send "00a4040410a0000000871002ffffffff8907090000" after the "export" (e.g. via the new "apdu" command added in https://gerrit.osmocom.org/c/pysim/+/27165), one can get back to the USIM application, and from there again perform "select MF". So this sounds like the best approach to implement.

Maybe thre's an Application ID for "no application", but in general we should select some other application and resolve this.

Actions #7

Updated by dexter almost 2 years ago

I have thought through various solutions. They are all hackish. I think where the problem hurts the most is during export, because it prevents that export can print its summary. Here is a patch to resolve this part:

https://gerrit.osmocom.org/c/pysim/+/28162

Apart from that I do not think that the problem hurts much. EF.ARAM is at the end of the file system tree, so we get everything we need.

What I didn't try yet is to implicitly select the parent when going one level up in the file system tree, maybe JCRE is able to interpret that correctly.

Actions #8

Updated by dexter almost 2 years ago

I have checked it apdu 00a404... seems to be the only SELECT variant that works, so we cannot use the P2 option "Select parent DF of the current DF"

Actions #9

Updated by laforge over 1 year ago

dexter wrote in #note-7:

I have thought through various solutions. They are all hackish. I think where the problem hurts the most is during export, because it prevents that export can print its summary. Here is a patch to resolve this part:

https://gerrit.osmocom.org/c/pysim/+/28162

Apart from that I do not think that the problem hurts much. EF.ARAM is at the end of the file system tree, so we get everything we need.

That is true only right now for one type of card in one use case. Bad assumption. Maybe reset the card after every application? Or have a blacklist of known AIDs that require some kind of reset or explicit select of ADF.USIM afte the export?

In general, the pysim shell should return the card to the state it had before the export, so that any subsequent command (including another export) will work after an export, from the same cwd as before calling export.

Actions #10

Updated by laforge over 1 year ago

  • Category set to pySim-shell
Actions #11

Updated by dexter 9 months ago

(GlobalPlatform Card Specification section 6.4.2.1 "Application Selection on Basic Logical Channel" may explain this behavior)

Actions #12

Updated by dexter 4 months ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 80

Unfortunately there is no clean way to solve this problem. The problem is that when we select ADF.ARA-M, we wend up with the GlobalPlatform OPEN SELECT (see GlobalPlatform Card Specification section 6.4.2.1). This variation of the SELECT command only supports the selection of an application. So the only way out of the ADF.ARA-M trap is to select either ADF.USIM or ADF.ISIM in order to get file system access back. So when we want to move one directory up, we may check if the currently active ADF is ADF.USIM or ADF.ISIM. If so, everything is fine. If not we cannot trust the SELECT command and must thread it like the OPEN SELECT, which is the smallest common denominator here.

Here is a patch that implements a possible solution:
https://gerrit.osmocom.org/c/pysim/+/34884 pySim-shell: don't get trapped in applications without file system

(I am not super happy with this solution since it is kind of a work around, but there seems to be no other way.)

Actions #13

Updated by dexter 4 months ago

(The issue came up again in the following ticket: #6116, which is basically a duplicate of this one)

Actions #14

Updated by laforge 4 months ago

  • Related to Bug #6120: select MF raise an exception after selecting ADF.ARA-M added
Actions #15

Updated by dexter 3 months ago

  • Status changed from In Progress to Resolved
  • % Done changed from 80 to 100

The patch (see note 12) is merged into master. I have also re-tested this. The commands "tree" and "export" work fine now. We can close this ticket now.

Actions

Also available in: Atom PDF

Add picture from clipboard (Maximum size: 48.8 MB)