Suppose I have implemented file_operations such as read, write, open, release, flush etc. and I wrote userspace application which calls these routines. In character driver, Userspace application communicate through /dev interface node.
for example - (/dev/diagnostics_1000_1-4:2.1) and I am bit surprise another application call our driver routines and we don't have control on those application.
Do they really call flush system call which directly/indirectly mapped to our function pointer ".flush"?
Snippet below -
[Wed Aug 16 23:07:02 2022] UserspaceOpen:448 PID = 291098, Pname = MyApp
[Wed Aug 16 23:07:02 2022] Diagnostics_1000: UserspaceOpen:461 - Interface is 3
....
[Tue Aug 16 23:07:38 2022] UserspaceRead:1182 PID = 25460, Pname = MyApp
[Tue Aug 16 23:07:38 2022] UserspaceFlush:622 PID = 25470, Pname = lsb_release
[Tue Aug 16 23:07:38 2022] UserspaceFlush:631 Wrong process:: PID = 25470, Pname = lsb_release, and pDev->pName = MyApp
[Tue Aug 16 23:07:38 2022] UserspaceFlush:622 PID = 25469, Pname = sh
[Tue Aug 16 23:07:38 2022] UserspaceFlush:631 Wrong process:: PID = 25469, Pname = sh, and pDev->pName = MyApp
[Tue Aug 16 23:07:38 2022] UserspaceWrite:1394 PID = 25463, Pname = MyApp
[Tue Aug 16 23:07:38 2022] UserspaceWrite_bulk_callback:1241 PID = 25428, Pname = VizCompositorTh
[Tue Aug 16 23:07:39 2022] UserspaceRead:1182 PID = 25460, Pname = MyApp
You can see that MyApp opens the interface "diagnostics_1000_1-4:2.1" but UserspaceFlush driver routines also called by lsb_release and sh process in the middle of operation and breaking the code flow. Though lsb_release and sh process haven't open the interface but somehow they triggered Flush operation.
We fixed the code by comparing the process name and continues if it matches otherwise return error code.
UserspaceFlush:631 Wrong process:: PID = 25469, Pname = sh, and pDev->pName = MyApp
Is there any design flaw? I want to understand what I am missing conceptually and how we can make it secure.
How to make sure that flush routine get called by the same process always i.e MyApp and file descriptor get closed by same application i.e MyApp because files actually opened by MyApp only.