> it depends on handle allocation algorithm. If it always uses the first free slot
in handle table (as one would expect)
IMHO, this is the case.
Yes, if you use DUPLICATE_CLOSE_SOURCE, the source handle is ZwClose’d while
attached to the source process. Then you only need to close the (new)
handle in your process after the system call returns, using CloseHandle
(locally).
Elegant, thanks, Skywing.
I had a lot remoting stuff at hand ready, so for me it was simpler to add
a couple of lines in the DLL being injected, but if remoted closure is all that’s
needed, I would prefer duplication.
-------------- Original message --------------
From: “Michal Vodicka”
> You’re right about handles recycling. I’m not quite sure how serious problem is;
> it depends on handle allocation algorithm. If it always uses the first free slot
> in handle table (as one would expect), it is serious problem.
>
> Indeed, for file handles it is always very dangerous technique. For mutextes, as
> OP wants, handle recycling could cause a deadlock or internal data corruption.
> As I said, the last resort and for one I’d agree with Don it shouldn’t be used
> in a commercial product. It seems as terminating target process can be safer.
>
> Best regards,
>
> Michal Vodicka
> UPEK, Inc.
> [xxxxx@upek.com, http://www.upek.com]
>
> > ----------
> > From:
> xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on
> behalf of Skywing[SMTP:xxxxx@valhallalegends.com]
> > Reply To: Windows System Software Devs Interest List
> > Sent: Wednesday, August 22, 2007 9:55 PM
> > To: Windows System Software Devs Interest List
> > Subject: Re:[ntdev] How to clear handle known by name
> >
> > Yes, if you use DUPLICATE_CLOSE_SOURCE, the source handle is ZwClose’d while
> > attached to the source process. Then you only need to close the (new)
> > handle in your process after the system call returns, using CloseHandle
> > (locally).
> >
> > It is, however, still bad to do this even if the process handles errors.
> > Say you close a handle to a file object that is periodically written to with
> > WriteFile. Now, before the next WriteFile call with the stale handle value,
> > another thread in the same process calls CreateFile and receives the (same,
> > but now unused) handle value for a different file. No error handling will
> > catch the resulting problem, which is that data gets written to the wrong
> > file (data corruption).
> >
> > The assumption that there is some margin of safety only holds true if handle
> > values are never recycled, which is definitely not the case. Although this
> > may not happen every time (it is after all a race), there is no way to
> > protect against it if you are ripping handle values out from under a process
> > without first telling it to stop using that handle value.
> >
> > –
> > Ken Johnson (Skywing)
> > Windows SDK MVP
> > http://www.nynaeve.net
> > “Michal Vodicka” wrote in message
> > news:xxxxx@ntdev…
> > > ----------
> > > From:
> > > xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com]
> > > on behalf of Skywing[SMTP:xxxxx@valhallalegends.com]
> > > Reply To: Windows System Software Devs Interest List
> > > Sent: Wednesday, August 22, 2007 6:12 PM
> > > To: Windows System Software Devs Interest List
> > > Subject: Re:[ntdev] How to clear handle known by name
> > >
> > > Actually, you can do this without thread injection via clever use of
> > > DuplicateHandle.
> > >
> > Nice. I’m surprised something like this is possible. Does it really work
> > between processes?
> >
> > > However, doing so without the cooperation of the target
> > > process is of course extremely dangerous as the process will continue to
> > > try
> > > and use the stale handle value.
> > >
> > Not so bad if target process uses correct error handling
Can be very bad
> > if it doesn’t or if it is a file handle and there are unsaved data in
> > memory. Which isn’t the case if I understand correctly.
> >
> > To OP: this functionality isn’t common enough. It is last resort solution,
> > instead. Actually, it is more hack than solution. How would you like if
> > somebody else randomly closes your handles in unexpected moments? Do you
> > check every API return code and handle all errors appropriately? I do but
> > many people call my code paranoid because they believe there are cases when
> > an API can’t fail. Closing handle from other app would break their
> > assumptions.
> >
> > Best regards,
> >
> > Michal Vodicka
> > UPEK, Inc.
> > [xxxxx@upek.com, http://www.upek.com]
> >
> >
> >
> >
> >
> > —
> > NTDEV is sponsored by OSR
> >
> > For our schedule of WDF, WDM, debugging and other seminars visit: >
> > http://www.osr.com/seminars
> >
> > To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
> >
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer