Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If you can't understand it, it is intuitively obvious.


devel / comp.lang.forth / Hardening Defined Words

SubjectAuthor
* Hardening Defined WordsKrishna Myneni
+* Re: Hardening Defined Wordsdxforth
|`- Re: Hardening Defined WordsKrishna Myneni
+* Re: Hardening Defined WordsAnton Ertl
|+* Re: Hardening Defined WordsMarcel Hendrix
||`* Re: Hardening Defined WordsAnton Ertl
|| +* Re: Hardening Defined WordsMarcel Hendrix
|| |`- Re: Hardening Defined WordsAnton Ertl
|| `* Re: Hardening Defined WordsKrishna Myneni
||  `- Re: Hardening Defined WordsKrishna Myneni
|`* Re: Hardening Defined WordsKrishna Myneni
| `* Re: Hardening Defined WordsAnton Ertl
|  `* Re: Hardening Defined WordsKrishna Myneni
|   `* Re: Hardening Defined Wordsnone
|    +* Re: Hardening Defined WordsS Jack
|    |`- Re: Hardening Defined Wordsnone
|    `* Re: Hardening Defined WordsKrishna Myneni
|     +* Re: Hardening Defined Wordsdxforth
|     |`- Re: Hardening Defined WordsKrishna Myneni
|     `* Re: Hardening Defined WordsAndy Valencia
|      `- Re: Hardening Defined Wordsnone
+* Re: Hardening Defined Wordsminf...@arcor.de
|+- Re: Hardening Defined Wordsdxforth
|`* Re: Hardening Defined WordsKrishna Myneni
| +- Re: Hardening Defined Wordsminf...@arcor.de
| `* Re: Hardening Defined Wordsantispam
|  `- Re: Hardening Defined WordsKrishna Myneni
`- Re: Hardening Defined WordsHans Bezemer

Pages:12
Hardening Defined Words

<tcklr5$3jiko$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19560&group=comp.lang.forth#19560

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Hardening Defined Words
Date: Fri, 5 Aug 2022 22:06:11 -0500
Organization: A noiseless patient Spider
Lines: 123
Message-ID: <tcklr5$3jiko$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Aug 2022 03:06:13 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="830e69c26277ffb2dc09d3f1a0b27f88";
logging-data="3787416"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+R3QgMZCHP2hk13Ii0KO+V"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:laS5a6zeL8yFDrMTd29Mj8NyRMY=
Content-Language: en-US
 by: Krishna Myneni - Sat, 6 Aug 2022 03:06 UTC

Summary: for some non-native Forth systems, it should be possible to
relocate the compiled code of a colon definition into memory which can
be marked read-only, to protect against corruption. For this to be
feasible the run-time xt for a word should have at least one level of
indirection to the code being executed by the virtual machine.

Consider the following ordinary colon definition in kForth, an indirect
threaded code Forth interpreter/compiler:

: foo 0 ;

' foo execute . \ same as typing FOO
0 ok

see foo
565403F101F0 #0
565403F101F9 RET
ok

Now, let's do some bad things to FOO.

0 ' foo ! \ store a zero at the execution address for FOO
foo
Segmentation fault (core dumped)
$

Start kForth again and define FOO as above.

' foo a@ execute-bc . \ execute the byte code for FOO

One may infer that the xt for FOO is an address at which the compiled
byte code for FOO resides. The byte code is the code executed by
kForth's virtual machine.

Now, let's define a word BAR and demonstrate that we can modify the byte
code for BAR directly from the Forth interpreter.

: bar 10 0 do i . loop ;

see bar
560DD5DABDA0 #10
560DD5DABDA9 #0
560DD5DABDB2 >R
560DD5DABDB3 >R
560DD5DABDB4 IP>R
560DD5DABDB5 I
560DD5DABDB6 .
560DD5DABDB7 LOOP
560DD5DABDB8 RET
ok

To see the actual byte code of BAR,

' bar a@ 32 dump

560DD5DABDA0 : 49 0A 00 00 00 00 00 00 00 49 00 00 00 00
00 00 I........I......
560DD5DABDB0 : 00 00 DC DC DE 69 2E E9 EE 00 00 00 00 00
00 00 .....i..........

( the RET instruction for the virtual machine is byte EE ).

Now, we may corrupt the byte code, for example, by changing the loop
count to 5, instead of 10:

5 ' bar a@ 1+ !

Now, when BAR is executed, it will output "0 1 2 3 4 ok"

It is possible to use mmap and mprotect system calls (or equivalents
under Windows) to relocate the byte code to a new memory region and mark
that memory region as read-only, thereby avoiding this type of
corruption. It is relatively simple to do this from Forth itself,
although the details are obviously system-dependent. In this way, we
can, in principle, protect the executed code for a colon definition.

It's important to note that the dictionary structure for the word itself
is not able to be protected from being overwritten in this scheme.
Protecting the dictionary headers for colon definitions would require a
significant change in architecture, but it's not out of the question.

Although I used kForth as the example system since I'm familiar with its
internals, other systems may be able to do the same. I don't know the
internals of Gforth, but one can see that at least one level of
indirection appears to be involved in going from the xt to the executed
code, e.g., in Gforth,

see execute
Code execute
404AB9: mov $50[r13],r15
404ABD: mov rdx,[r14]
404AC0: add r14,$08
404AC4: mov rcx,-$10[rdx]
404AC8: jmp ecx
end-code

Here, the assembly code gives us the hint that r14 is the TOS (top of
stack) and there seems to be one level of indirection from the xt on top
of the stack to the code which is subsequently executed. The code
pointed to by xt can be overwritten, e.g., in Gforth,

: bar 10 0 do i . loop ; ok
bar 0 1 2 3 4 5 6 7 8 9 ok

0 ' bar @ ! ok
bar
*the terminal*:3:1: error: Stack underflow

I don't know enough about Gforth internals to be able to say that a
relocation of the code for BAR to a region which can be protected as
read only is possible. Perhaps one of the Gforth developers can say
definitively whether or not this is possible.

--
Krishna Myneni

Re: Hardening Defined Words

<tckpha$gju$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19561&group=comp.lang.forth#19561

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!aioe.org!7AktqsUqy5CCvnKa3S0Dkw.user.46.165.242.75.POSTED!not-for-mail
From: dxfo...@gmail.com (dxforth)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 14:09:13 +1000
Organization: Aioe.org NNTP Server
Message-ID: <tckpha$gju$1@gioia.aioe.org>
References: <tcklr5$3jiko$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="17022"; posting-host="7AktqsUqy5CCvnKa3S0Dkw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.12.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-GB
 by: dxforth - Sat, 6 Aug 2022 04:09 UTC

The only protected system I've used is FlashForth. It attempts to protect
the kernel on the basis a user should be able restart forth after a crash
without having to re-flash the system. It's hard for me to evaluate the
benefits of such a system without disabling the protection (not easy). The
costs are known but the gain remains nebulous. Is there a developer who
wouldn't have access to a programmer should re-flashing become necessary?
And what failure rate are we talking about - once a day, once a month?

Re: Hardening Defined Words

<2022Aug6.074823@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19562&group=comp.lang.forth#19562

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 06 Aug 2022 05:48:23 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 52
Message-ID: <2022Aug6.074823@mips.complang.tuwien.ac.at>
References: <tcklr5$3jiko$1@dont-email.me>
Injection-Info: reader01.eternal-september.org; posting-host="34f4628157c945352550d9587ac3b5e4";
logging-data="3852074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+23SbHxzjgG7NhsLjoM4v3"
Cancel-Lock: sha1:EH/lcSgdiYFPP1LFL9yOQzPYx2w=
X-newsreader: xrn 10.00-beta-3
 by: Anton Ertl - Sat, 6 Aug 2022 05:48 UTC

Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>It is possible to use mmap and mprotect system calls (or equivalents
>under Windows) to relocate the byte code to a new memory region and mark
>that memory region as read-only, thereby avoiding this type of
>corruption. It is relatively simple to do this from Forth itself,
>although the details are obviously system-dependent. In this way, we
>can, in principle, protect the executed code for a colon definition.
>
>It's important to note that the dictionary structure for the word itself
>is not able to be protected from being overwritten in this scheme.

I don't see a reason why not. Compile-to-flash systems do it. If you
don't want to change protection on every IMMEDIATE, DOES> etc., keep
the most recent header in writeable memory, and only move it to
read-only memory when the next header is created.

>I don't know the
>internals of Gforth, but one can see that at least one level of
>indirection appears to be involved in going from the xt to the executed
>code, e.g., in Gforth,
>
>see execute
>Code execute
> 404AB9: mov $50[r13],r15
> 404ABD: mov rdx,[r14]
> 404AC0: add r14,$08
> 404AC4: mov rcx,-$10[rdx]
> 404AC8: jmp ecx
>end-code
>
>Here, the assembly code gives us the hint that r14 is the TOS (top of
>stack) and there seems to be one level of indirection from the xt on top
>of the stack to the code which is subsequently executed.

As far as EXECUTE is concerned, Gforth uses indirect-threaded code.
That's the indirection you are seeing.

It would require substantial changes to make the threaded code and/or
the headers read-only; for the native code it would be relatively
straight-forward to make all but the most recent native-code page
unwriteable.

Bugs where code or headers were overwritten have not been problematic
enough in our experience to take any such action. I have not had such
a request by users, either.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: Hardening Defined Words

<b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19563&group=comp.lang.forth#19563

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:2806:b0:6b8:eced:ba3a with SMTP id f6-20020a05620a280600b006b8ecedba3amr7483287qkp.462.1659766773008;
Fri, 05 Aug 2022 23:19:33 -0700 (PDT)
X-Received: by 2002:a05:6214:76b:b0:476:5c98:2a55 with SMTP id
f11-20020a056214076b00b004765c982a55mr8515691qvz.70.1659766772883; Fri, 05
Aug 2022 23:19:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Fri, 5 Aug 2022 23:19:32 -0700 (PDT)
In-Reply-To: <tcklr5$3jiko$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2003:f7:1f27:a8ed:3d52:cab2:4744:b86d;
posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 2003:f7:1f27:a8ed:3d52:cab2:4744:b86d
References: <tcklr5$3jiko$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
Subject: Re: Hardening Defined Words
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Sat, 06 Aug 2022 06:19:32 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1762
 by: minf...@arcor.de - Sat, 6 Aug 2022 06:19 UTC

Krishna Myneni schrieb am Samstag, 6. August 2022 um 05:06:15 UTC+2:
> Summary: for some non-native Forth systems, it should be possible to
> relocate the compiled code of a colon definition into memory which can
> be marked read-only, to protect against corruption. For this to be
> feasible the run-time xt for a word should have at least one level of
> indirection to the code being executed by the virtual machine.
>

The easiest way in a VM-based Forth would be to just add
address-checking to all words that write to memory.
Eg
! (store sanitized, safe but slow)
_! (store naked, fast and unaccessible to the user)

Re: Hardening Defined Words

<2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19564&group=comp.lang.forth#19564

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:1a92:b0:341:53bc:5a3 with SMTP id s18-20020a05622a1a9200b0034153bc05a3mr8944263qtc.659.1659772171415;
Sat, 06 Aug 2022 00:49:31 -0700 (PDT)
X-Received: by 2002:a37:8084:0:b0:6b9:2963:5eab with SMTP id
b126-20020a378084000000b006b929635eabmr1982609qkd.404.1659772171278; Sat, 06
Aug 2022 00:49:31 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Sat, 6 Aug 2022 00:49:31 -0700 (PDT)
In-Reply-To: <2022Aug6.074823@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:1c05:2f14:600:9d48:e813:c679:f1b8;
posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 2001:1c05:2f14:600:9d48:e813:c679:f1b8
References: <tcklr5$3jiko$1@dont-email.me> <2022Aug6.074823@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com>
Subject: Re: Hardening Defined Words
From: mhx...@iae.nl (Marcel Hendrix)
Injection-Date: Sat, 06 Aug 2022 07:49:31 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2380
 by: Marcel Hendrix - Sat, 6 Aug 2022 07:49 UTC

On Saturday, August 6, 2022 at 8:02:26 AM UTC+2, Anton Ertl wrote:
[..]
> Bugs where code or headers were overwritten have not been problematic
> enough in our experience to take any such action. I have not had such
> a request by users, either.

I have had a problem with this a few times. Actually, I'm extremely glad that
code is *not* read protected: how would I have noticed that something was
wrong? A slightly off final result in a big program is not straightforward.

An overwite of native code causes an almost immediate crash
that leads to a useful exception report. It can be problematic
to instrument the calling code to catch the reason (i.e. if the overwrite
happens very infrequently under special conditions). I have had one or two
cases (in 40 years) where I had to use an external debugger which supports
break on memory access. The steps are: start Forth first, then attach
the debugger to the image. Run Forth until you get the exception address,
switch to the debugger and setup the breakpoint there, then go back to
Forth and restart or halt the program in the vicinity of the problem.
Inspecting memory and data is much more convenient at the Forth end.

-marcel

Re: Hardening Defined Words

<2022Aug6.112107@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19565&group=comp.lang.forth#19565

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 06 Aug 2022 09:21:07 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 43
Message-ID: <2022Aug6.112107@mips.complang.tuwien.ac.at>
References: <tcklr5$3jiko$1@dont-email.me> <2022Aug6.074823@mips.complang.tuwien.ac.at> <2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="34f4628157c945352550d9587ac3b5e4";
logging-data="3964836"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CxrXu44VnAmHPTJRS5BnK"
Cancel-Lock: sha1:IR7D5gaxAvxlStkVqXiMK2g2mcE=
X-newsreader: xrn 10.00-beta-3
 by: Anton Ertl - Sat, 6 Aug 2022 09:21 UTC

Marcel Hendrix <mhx@iae.nl> writes:
> Actually, I'm extremely glad that
>code is *not* read protected: how would I have noticed that something was
>wrong?

If the code was write-protected and you tried to write to it, you
would get a SIGSEGV on Unix. E.g., when you do SEE FSIN in Gforth,
you see the code for FSIN coming from the gcc, which is
write-protected. Now let's see what happens when I try to write
there:

see fsin
Code fsin
5586FF58AFFB: movapd xmm0,xmm15
5586FF58B000: mov $20[rsp],r8
5586FF58B005: add r15,$08
5586FF58B009: call $5586FF5876E0
5586FF58B00E: mov r8,$20[rsp]
5586FF58B013: movapd xmm15,xmm0
5586FF58B018: mov rcx,-$08[r15]
5586FF58B01C: jmp ecx
end-code
ok
1 $5586FF58AFFB c!
*the terminal*:3:17: error: Invalid memory address
1 $5586FF58AFFB >>>c!<<<

>An overwite of native code causes an almost immediate crash
>that leads to a useful exception report.

If you are unlucky, the code is executed long after the write, and you
have to puzzle out what went wrong. With write-protected code you see
the write that would otherwise cause the problem, as shown above.

So write-protecting the code can have an advantage. The question is
if the advantage is big enough to justify the effort.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: Hardening Defined Words

<tcleu1$1af2$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19566&group=comp.lang.forth#19566

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!aioe.org!7AktqsUqy5CCvnKa3S0Dkw.user.46.165.242.75.POSTED!not-for-mail
From: dxfo...@gmail.com (dxforth)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 20:14:24 +1000
Organization: Aioe.org NNTP Server
Message-ID: <tcleu1$1af2$1@gioia.aioe.org>
References: <tcklr5$3jiko$1@dont-email.me>
<b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="43490"; posting-host="7AktqsUqy5CCvnKa3S0Dkw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.12.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-GB
 by: dxforth - Sat, 6 Aug 2022 10:14 UTC

On 6/08/2022 16:19, minf...@arcor.de wrote:
> Krishna Myneni schrieb am Samstag, 6. August 2022 um 05:06:15 UTC+2:
>> Summary: for some non-native Forth systems, it should be possible to
>> relocate the compiled code of a colon definition into memory which can
>> be marked read-only, to protect against corruption. For this to be
>> feasible the run-time xt for a word should have at least one level of
>> indirection to the code being executed by the virtual machine.
>>
>
> The easiest way in a VM-based Forth would be to just add
> address-checking to all words that write to memory.
> Eg
> ! (store sanitized, safe but slow)
> _! (store naked, fast and unaccessible to the user)

! need not be slow - at least not for RAM - where it matters.
If application RAM in a system is segregated then it's a simple
test for ! to determine. Storing to CODE/FLASH/EEPROM can afford to
be slower as such operations are either atypical or inherently slow.

Re: Hardening Defined Words

<108ddf8a-b4d6-42bf-a0c2-bc1d60c7295bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19567&group=comp.lang.forth#19567

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:1a11:b0:341:4c47:8cc2 with SMTP id f17-20020a05622a1a1100b003414c478cc2mr9297046qtb.175.1659784335208;
Sat, 06 Aug 2022 04:12:15 -0700 (PDT)
X-Received: by 2002:a05:622a:1e0e:b0:31f:1931:b2ab with SMTP id
br14-20020a05622a1e0e00b0031f1931b2abmr8898222qtb.686.1659784335027; Sat, 06
Aug 2022 04:12:15 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Sat, 6 Aug 2022 04:12:14 -0700 (PDT)
In-Reply-To: <2022Aug6.112107@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2001:1c05:2f14:600:9d48:e813:c679:f1b8;
posting-account=-JQ2RQoAAAB6B5tcBTSdvOqrD1HpT_Rk
NNTP-Posting-Host: 2001:1c05:2f14:600:9d48:e813:c679:f1b8
References: <tcklr5$3jiko$1@dont-email.me> <2022Aug6.074823@mips.complang.tuwien.ac.at>
<2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com> <2022Aug6.112107@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <108ddf8a-b4d6-42bf-a0c2-bc1d60c7295bn@googlegroups.com>
Subject: Re: Hardening Defined Words
From: mhx...@iae.nl (Marcel Hendrix)
Injection-Date: Sat, 06 Aug 2022 11:12:15 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3035
 by: Marcel Hendrix - Sat, 6 Aug 2022 11:12 UTC

On Saturday, August 6, 2022 at 11:34:00 AM UTC+2, Anton Ertl wrote:
[..]
> If the code was write-protected and you tried to write to it, you
> would get a SIGSEGV on Unix. E.g., when you do SEE FSIN in Gforth,
> you see the code for FSIN coming from the gcc, which is
> write-protected. Now let's see what happens when I try to write
> there:
[..]
> 1 $5586FF58AFFB c!
> *the terminal*:3:17: error: Invalid memory address
> 1 $5586FF58AFFB >>>c!<<<

Indeed useful: the exception is generated immediately
when the overwrite happens. The stack trace shows
who tried to do that.

FORTH> ' fsin idis
$01250FE0 : FSIN
$01250FEA call REDUCE.2PI ( $0124AD00 ) offset NEAR
$01250FEF fsin
$01250FF1 ;
FORTH> 1 $01250FEA c! ok
FORTH> ' fsin idis
$01250FE0 : FSIN
$01250FEA add [ecx] dword, rdx
$01250FEC popfq
$01250FED ??? rdi
$01250FEF fsin
$01250FF1 ;
FORTH> 0e fsin
Caught exception 0xc0000005
ACCESS VIOLATION
instruction pointer = $0000000001250FEA
RAX = $01253425 RBX = $01250FE0
RCX = $00000000 RDX = $0000028E
RSI = $01155C00 RDI = $2C9EF798
RBP = $01125F88 RSP = $2C9EF7D8
R8 = $01099A20 R9 = $00000020
R10 = $01046F50 R11 = $011128A5
R12 = $01099AC0 R13 = $01156FF0
R14 = $01136000 R15 = $01110000
Hardware exception in ``FSIN''+$0000000A
**** RETURN STACK DUMP **** for MAIN-THREAD

Only shows the problem after the fact, needing an external
debugger to set up a break.
Knowing how much effort/nuisance it is to make words
r/o during compilation and debugging would make it possible
to weigh advantages and disadvantages. It seems that
code and data can't be interleaved at all.

-marcel

Re: Hardening Defined Words

<tcm2vs$3um9t$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19570&group=comp.lang.forth#19570

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 10:56:42 -0500
Organization: A noiseless patient Spider
Lines: 98
Message-ID: <tcm2vs$3um9t$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<2022Aug6.074823@mips.complang.tuwien.ac.at>
<2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com>
<2022Aug6.112107@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Aug 2022 15:56:44 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="830e69c26277ffb2dc09d3f1a0b27f88";
logging-data="4151613"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187bvJqspf0xZraNC9cRbZa"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:Bm9vb0hDx1T4gxEPBbjg8aCWqDE=
In-Reply-To: <2022Aug6.112107@mips.complang.tuwien.ac.at>
Content-Language: en-US
 by: Krishna Myneni - Sat, 6 Aug 2022 15:56 UTC

On 8/6/22 04:21, Anton Ertl wrote:
> Marcel Hendrix <mhx@iae.nl> writes:
>> Actually, I'm extremely glad that
>> code is *not* read protected: how would I have noticed that something was
>> wrong?
>
> If the code was write-protected and you tried to write to it, you
> would get a SIGSEGV on Unix. ...
> So write-protecting the code can have an advantage. The question is
> if the advantage is big enough to justify the effort.
>

Yes, even when the code to be executed is in the form of tokenized byte
code and executed by a virtual machine, protecting the memory containing
the byte code by making it read only will cause an overwrite to fail
immediately and unambiguously.

I've run into this type of memory corruption problem a few times and I
remember they were extremely difficult to debug. It seems more common to
see memory corruption of the dictionary headers via a stray address, so
that problem may be more pressing than overwriting the byte code. In
kForth, corruption of the dictionary headers is often indicated by a
core dump upon performing bye, when the dynamically allocated dictionary
space is freed.

The machine code part of all code words in kForth are stored in memory
which is read-execute except when new code is added (see mc.4th). It is
not terribly difficult to protect ordinary colon definitions in kForth,
although it is a bit of a hack write now. The dictionary header needs
another field or two, one of which indicates whether or not the word's
executable code is read-only, and another to store the executable code size.

I wrote a variant of mc.4th, called protect.4th, which allows a colon
definition to be protected. Going back to our BAR example,

: bar 10 0 do i . loop ;
ok
see bar
55C34CBC89C0 #10
55C34CBC89C9 #0
55C34CBC89D2 >R
55C34CBC89D3 >R
55C34CBC89D4 IP>R
55C34CBC89D5 I
55C34CBC89D6 .
55C34CBC89D7 LOOP
55C34CBC89D8 RET
ok
bar
0 1 2 3 4 5 6 7 8 9 ok

' bar 32 Protect-Def \ relocate BAR's byte code to protected memory
ok

\ Protect-Def also updates the dictionary header for BAR

see bar
7F09C30E3000 #10
7F09C30E3009 #0
7F09C30E3012 >R
7F09C30E3013 >R
7F09C30E3014 IP>R
7F09C30E3015 I
7F09C30E3016 .
7F09C30E3017 LOOP
7F09C30E3018 RET
ok
\ Note the new address space of the relocated byte code.
bar
0 1 2 3 4 5 6 7 8 9 ok

Now, unlike before, when we try to overwrite the byte code memory there
is an immediate and hard failure.

5 ' bar a@ 1+ !
Segmentation fault (core dumped)
$

The details of PROTECT-DEF are, of course, Forth system and OS
system-dependent. The source code for protect.4th is posted at

https://github.com/mynenik/kForth-64/blob/master/forth-src/protect.4th

One problem with the current approach is a segmentation fault on
executing BYE , because the cleanup code executed upon BYE tries to free
the new byte code memory. This is why a protection flag is needed in the
dictionary header, which involves changes to the source code for the
Forth system. However, these are relatively simple changes to kForth.

--
Krishna

Re: Hardening Defined Words

<2022Aug6.182116@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19571&group=comp.lang.forth#19571

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 06 Aug 2022 16:21:16 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 19
Message-ID: <2022Aug6.182116@mips.complang.tuwien.ac.at>
References: <tcklr5$3jiko$1@dont-email.me> <2022Aug6.074823@mips.complang.tuwien.ac.at> <2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com> <2022Aug6.112107@mips.complang.tuwien.ac.at> <108ddf8a-b4d6-42bf-a0c2-bc1d60c7295bn@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="34f4628157c945352550d9587ac3b5e4";
logging-data="4132461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CkG2CnW598A+nnZThF5h2"
Cancel-Lock: sha1:ybOOOFieZ4haYI0sbt/FLIKVwjQ=
X-newsreader: xrn 10.00-beta-3
 by: Anton Ertl - Sat, 6 Aug 2022 16:21 UTC

Marcel Hendrix <mhx@iae.nl> writes:
>Knowing how much effort/nuisance it is to make words
>r/o during compilation and debugging would make it possible
>to weigh advantages and disadvantages. It seems that
>code and data can't be interleaved at all.

If you want to write-protect code, you cannot have writable data in
the same page. You can have read-only data (e.g., settled headers,
constant values) in the same page (and that's good enough to avoid the
cache consistency performance problem on the Pentium Pro, Athlon, and
later CPUs), but cache utilization is better if you separate code and
data.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: Hardening Defined Words

<tcmf51$1prg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19572&group=comp.lang.forth#19572

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 14:24:15 -0500
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <tcmf51$1prg$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Aug 2022 19:24:17 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="830e69c26277ffb2dc09d3f1a0b27f88";
logging-data="59248"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aZw6MVnrXL+RojyxITrPW"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:slVLPG1bwm2CWzLD3O8Vdl3vSLk=
In-Reply-To: <b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
Content-Language: en-US
 by: Krishna Myneni - Sat, 6 Aug 2022 19:24 UTC

On 8/6/22 01:19, minf...@arcor.de wrote:
> Krishna Myneni schrieb am Samstag, 6. August 2022 um 05:06:15 UTC+2:
>> Summary: for some non-native Forth systems, it should be possible to
>> relocate the compiled code of a colon definition into memory which can
>> be marked read-only, to protect against corruption. For this to be
>> feasible the run-time xt for a word should have at least one level of
>> indirection to the code being executed by the virtual machine.
>>
>
> The easiest way in a VM-based Forth would be to just add
> address-checking to all words that write to memory.
> Eg
> ! (store sanitized, safe but slow)
> _! (store naked, fast and unaccessible to the user)

Not sure what you mean. How does a VM-based Forth distinguish between
addresses which are data space vs addresses which contain the virtual
machine code?

kForth uses a separate type stack to distinguish between ordinary
numbers and addresses. This has proven useful in flagging common
mistakes caused by incorrect stack manipulation. It is quite useful, I
think, in aiding beginning Forth programmers to reveal the source of the
problem. The added complexity of the type stack gives a performance hit
of about 15% in kForth.

--
Krishna

Re: Hardening Defined Words

<tcmg1o$218d$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19573&group=comp.lang.forth#19573

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 14:39:34 -0500
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <tcmg1o$218d$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<2022Aug6.074823@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Aug 2022 19:39:36 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="830e69c26277ffb2dc09d3f1a0b27f88";
logging-data="66829"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184JeZYaHi7HO+K/T9OMU3X"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:dlZHx7H89c4DPuoDZOTJd9mAN8E=
In-Reply-To: <2022Aug6.074823@mips.complang.tuwien.ac.at>
Content-Language: en-US
 by: Krishna Myneni - Sat, 6 Aug 2022 19:39 UTC

On 8/6/22 00:48, Anton Ertl wrote:
> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
....
>> It's important to note that the dictionary structure for the word itself
>> is not able to be protected from being overwritten in this scheme.
>
> I don't see a reason why not. Compile-to-flash systems do it. If you
> don't want to change protection on every IMMEDIATE, DOES> etc., keep
> the most recent header in writeable memory, and only move it to
> read-only memory when the next header is created.
>

All dictionary headers don't correspond to ordinary colon definitions.
If one were to protect all headers, there may be issues with relocation
affecting previously compiled code, such as with DEFERred words. I
haven't thought through this problem enough yet to say with certainty
that all headers can be protected as read-only. It may be highly
system-dependent.

....
>
> It would require substantial changes to make the threaded code and/or
> the headers read-only; for the native code it would be relatively
> straight-forward to make all but the most recent native-code page
> unwriteable.
>

I expect that your use of memory segments in Gforth should simplify the
problem of placing the threaded code in write-protected segments.

> Bugs where code or headers were overwritten have not been problematic
> enough in our experience to take any such action. I have not had such
> a request by users, either.
>

Well, such bugs may be occurring more often than you realize. Such bugs
often don't have immediate consequences. I can run Forth code which
works perfectly fine because it hasn't made use of corrupt parts of the
system, but if such corruption has occurred I often find out when I type
BYE and then see a Seg Fault as memory is freed while the application is
terminating. This tells me to go back and find the bugs in my
application Forth code.

--
Krishna

Re: Hardening Defined Words

<tcmgch$2491$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19574&group=comp.lang.forth#19574

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sat, 6 Aug 2022 14:45:20 -0500
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <tcmgch$2491$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me> <tckpha$gju$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Aug 2022 19:45:21 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="830e69c26277ffb2dc09d3f1a0b27f88";
logging-data="69921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3Hj6nrvp8mg6hnP1liSqW"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:wxLFRDegCZyYf0zMo6YBtfX0X1A=
Content-Language: en-US
In-Reply-To: <tckpha$gju$1@gioia.aioe.org>
 by: Krishna Myneni - Sat, 6 Aug 2022 19:45 UTC

On 8/5/22 23:09, dxforth wrote:
> The only protected system I've used is FlashForth. It attempts to protect
> the kernel on the basis a user should be able restart forth after a crash
> without having to re-flash the system. It's hard for me to evaluate the
> benefits of such a system without disabling the protection (not easy). The
> costs are known but the gain remains nebulous. Is there a developer who
> wouldn't have access to a programmer should re-flashing become necessary?
> And what failure rate are we talking about - once a day, once a month?

I expect the failure rate is highly application dependent. An
alternative to write-protecting the executable code and constant data,
is to store hash/checksum of the data within a read-only region (even an
EEPROM for a release application). Then, the user can check for
corruption on demand by recomputing the hashes/checksums and comparing
with the read-only data.

I expect the probability of a failure to be highly dependent on the
complexity of the application, and possibly on the hardware operating
environment, e.g. if the power cycles on an off frequently while writing
to the storage medium.

--
Krishna

Re: Hardening Defined Words

<4a450814-6d6c-4781-bd61-ac9eaf0194a6n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19576&group=comp.lang.forth#19576

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:ac8:5e54:0:b0:341:b0a6:ef3b with SMTP id i20-20020ac85e54000000b00341b0a6ef3bmr11348601qtx.132.1659850415708;
Sat, 06 Aug 2022 22:33:35 -0700 (PDT)
X-Received: by 2002:a05:620a:1981:b0:6b5:cccf:62e1 with SMTP id
bm1-20020a05620a198100b006b5cccf62e1mr9954716qkb.376.1659850415572; Sat, 06
Aug 2022 22:33:35 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Sat, 6 Aug 2022 22:33:35 -0700 (PDT)
In-Reply-To: <tcmf51$1prg$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=87.157.100.114; posting-account=AqNUYgoAAADmkK2pN-RKms8sww57W0Iw
NNTP-Posting-Host: 87.157.100.114
References: <tcklr5$3jiko$1@dont-email.me> <b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
<tcmf51$1prg$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4a450814-6d6c-4781-bd61-ac9eaf0194a6n@googlegroups.com>
Subject: Re: Hardening Defined Words
From: minfo...@arcor.de (minf...@arcor.de)
Injection-Date: Sun, 07 Aug 2022 05:33:35 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2963
 by: minf...@arcor.de - Sun, 7 Aug 2022 05:33 UTC

Krishna Myneni schrieb am Samstag, 6. August 2022 um 21:24:20 UTC+2:
> On 8/6/22 01:19, minf...@arcor.de wrote:
> > Krishna Myneni schrieb am Samstag, 6. August 2022 um 05:06:15 UTC+2:
> >> Summary: for some non-native Forth systems, it should be possible to
> >> relocate the compiled code of a colon definition into memory which can
> >> be marked read-only, to protect against corruption. For this to be
> >> feasible the run-time xt for a word should have at least one level of
> >> indirection to the code being executed by the virtual machine.
> >>
> >
> > The easiest way in a VM-based Forth would be to just add
> > address-checking to all words that write to memory.
> > Eg
> > ! (store sanitized, safe but slow)
> > _! (store naked, fast and unaccessible to the user)
> Not sure what you mean. How does a VM-based Forth distinguish between
> addresses which are data space vs addresses which contain the virtual
> machine code?

When the dataspace is allocated at program start, it will contain no VM code.

> kForth uses a separate type stack to distinguish between ordinary
> numbers and addresses. This has proven useful in flagging common
> mistakes caused by incorrect stack manipulation. It is quite useful, I
> think, in aiding beginning Forth programmers to reveal the source of the
> problem. The added complexity of the type stack gives a performance hit
> of about 15% in kForth.

IIRC StrongForth used a type stack to type-tag _all_ numeric types. But I
don't know how much performance it eat.

For pure stack and address checking (hard-coded in the words) I measured
a runtime penalty of about 5%-7% in my system. But it is C-based and therefore
slower anyhow so that those different performance hits cannot be compared.

Re: Hardening Defined Words

<2022Aug7.161158@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19579&group=comp.lang.forth#19579

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sun, 07 Aug 2022 14:11:58 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 70
Message-ID: <2022Aug7.161158@mips.complang.tuwien.ac.at>
References: <tcklr5$3jiko$1@dont-email.me> <2022Aug6.074823@mips.complang.tuwien.ac.at> <tcmg1o$218d$1@dont-email.me>
Injection-Info: reader01.eternal-september.org; posting-host="0416b7432c9c248491e25f7bb29860a1";
logging-data="658896"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18JUCqcgwPDHYLv6cCDN4ow"
Cancel-Lock: sha1:N5Cdp91Ic0G4qYy94ifGm4kf5Hc=
X-newsreader: xrn 10.00-beta-3
 by: Anton Ertl - Sun, 7 Aug 2022 14:11 UTC

Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>On 8/6/22 00:48, Anton Ertl wrote:
>> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>...
>>> It's important to note that the dictionary structure for the word itself
>>> is not able to be protected from being overwritten in this scheme.
>>
>> I don't see a reason why not. Compile-to-flash systems do it. If you
>> don't want to change protection on every IMMEDIATE, DOES> etc., keep
>> the most recent header in writeable memory, and only move it to
>> read-only memory when the next header is created.
>>
>
>All dictionary headers don't correspond to ordinary colon definitions.

?

>If one were to protect all headers, there may be issues with relocation
>affecting previously compiled code, such as with DEFERred words.

It's unclear to me how relocation comes in here, but for DEFERed words
the thing that IS changes is not part of the header, just like for a
VALUE the thing the TO changes is not part of the header. And of
course you would need one indirection to get from the header to the
data in these cases.

>I
>haven't thought through this problem enough yet to say with certainty
>that all headers can be protected as read-only. It may be highly
>system-dependent.

If you put in enough effort, they can.

>I expect that your use of memory segments in Gforth should simplify the
>problem of placing the threaded code in write-protected segments.

Yes, one can use sections for that purpose, but there is still a
substantial amount of changes to make.

>Well, such bugs may be occurring more often than you realize. Such bugs
>often don't have immediate consequences. I can run Forth code which
>works perfectly fine because it hasn't made use of corrupt parts of the
>system, but if such corruption has occurred I often find out when I type
>BYE and then see a Seg Fault as memory is freed while the application is
>terminating. This tells me to go back and find the bugs in my
>application Forth code.

Apart from your use as a debugging tool, freeing before exit()ing the
process is a waste of time.

One extreme case was Mozilla, which
apparently leaked memory, and that memory was paged out over time
(that was at a time when we still used swap space). When exiting
Mozilla, it took several minutes to page the leaked memory back in in
order to free() it. Only then it performed the exit(). It would have
exited much faster if it had not freed first.

My guess is that a manager saw the memory leaks, and demanded that the
programmers fix them; so they dutifully recorded all memory that they
allocated, and freed it when the user wanted to quit Mozilla. As a
result, the memory leak tool they used for finding leaks reported that
no memory was leaked, and the manager was satisfied. In reality the
leaks were still there, and Mozilla was now sluggish on termination.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2022: https://euro.theforth.net

Re: Hardening Defined Words

<tconob$klc8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19584&group=comp.lang.forth#19584

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sun, 7 Aug 2022 11:03:22 -0500
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <tconob$klc8$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<2022Aug6.074823@mips.complang.tuwien.ac.at> <tcmg1o$218d$1@dont-email.me>
<2022Aug7.161158@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 7 Aug 2022 16:03:24 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="63aaad52e75441c2b7de1c0ad387a0cd";
logging-data="677256"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pEHbwXl083uD0CUUH+MZi"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:zS5O63Gv5siXPyS2F7J0N1PbWDc=
Content-Language: en-US
In-Reply-To: <2022Aug7.161158@mips.complang.tuwien.ac.at>
 by: Krishna Myneni - Sun, 7 Aug 2022 16:03 UTC

On 8/7/22 09:11, Anton Ertl wrote:
> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>> On 8/6/22 00:48, Anton Ertl wrote:
>>> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>> ...
>>>> It's important to note that the dictionary structure for the word itself
>>>> is not able to be protected from being overwritten in this scheme.
>>>
>>> I don't see a reason why not. Compile-to-flash systems do it. If you
>>> don't want to change protection on every IMMEDIATE, DOES> etc., keep
>>> the most recent header in writeable memory, and only move it to
>>> read-only memory when the next header is created.
>>>
>>
>> All dictionary headers don't correspond to ordinary colon definitions.
>
> ?
>

I was thinking of CREATEd words and whether or not protecting the
dictionary headers for such words could cause problems for subsequently
defined words which call the earlier words. It seems that as long as
each dictionary header is protected after the corresponding word is
protected (relocated) there shouldn't arise a problem with incorrect
references to header entry fields, referenced by subsequent code.

>> If one were to protect all headers, there may be issues with relocation
>> affecting previously compiled code, such as with DEFERred words.
>
> It's unclear to me how relocation comes in here, but for DEFERed words
> the thing that IS changes is not part of the header, just like for a
> VALUE the thing the TO changes is not part of the header. And of
> course you would need one indirection to get from the header to the
> data in these cases.
>

As long as the indirection is not bypassed by a compiler, there
shouldn't be a problem.

>> I
>> haven't thought through this problem enough yet to say with certainty
>> that all headers can be protected as read-only. It may be highly
>> system-dependent.
>
> If you put in enough effort, they can.
>

I agree that a Forth system architecture which provides memory
protection for dictionary headers, non-native executable code of colon
definitions, and for native code of CODE words/ordinary definitions is
possible.

>> I expect that your use of memory segments in Gforth should simplify the
>> problem of placing the threaded code in write-protected segments.
>
> Yes, one can use sections for that purpose, but there is still a
> substantial amount of changes to make.
>

I'm taking a cautious approach, focusing on protecting the non-native
(tokenized) executable code for colon definitions for now, and seeing if
there are any issues which come up. If there aren't any significant
issues, then I can tackle dictionary header protection.

>> Well, such bugs may be occurring more often than you realize. Such bugs
>> often don't have immediate consequences. I can run Forth code which
>> works perfectly fine because it hasn't made use of corrupt parts of the
>> system, but if such corruption has occurred I often find out when I type
>> BYE and then see a Seg Fault as memory is freed while the application is
>> terminating. This tells me to go back and find the bugs in my
>> application Forth code.
>
> Apart from your use as a debugging tool, freeing before exit()ing the
> process is a waste of time.
>

My recollection is that, in Linux, it wasn't always the case that the OS
cleaned up dynamically allocated memory for an application after
termination -- that was the original reason for freeing memory prior to
exit(). In almost all of my use cases, the exit() time is ignorable/not
noticeable and, thus, there was no need to remove the memory freeing
step. But it has the highly useful benefit of warning me via a seg fault
that my session was corrupt and that any results from the session should
be checked after fixing the bug(s) causing the corruption.

--
Krishna

Re: Hardening Defined Words

<tcpf2m$nf65$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19589&group=comp.lang.forth#19589

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Sun, 7 Aug 2022 17:41:24 -0500
Organization: A noiseless patient Spider
Lines: 108
Message-ID: <tcpf2m$nf65$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<2022Aug6.074823@mips.complang.tuwien.ac.at>
<2f878f83-2ab9-4b4b-9791-92c794b12596n@googlegroups.com>
<2022Aug6.112107@mips.complang.tuwien.ac.at> <tcm2vs$3um9t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 7 Aug 2022 22:41:26 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="e2b0e965a716ca24ba943fc003c7e151";
logging-data="769221"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xTT0E1bK1gs154C55BMkY"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Cancel-Lock: sha1:ABMUy7VvDO2Fo+D91CoTn5Kt3iQ=
In-Reply-To: <tcm2vs$3um9t$1@dont-email.me>
Content-Language: en-US
 by: Krishna Myneni - Sun, 7 Aug 2022 22:41 UTC

On 8/6/22 10:56, Krishna Myneni wrote:
....
> The details of PROTECT-DEF are, of course, Forth system and OS
> system-dependent. The source code for protect.4th is posted at
>
> https://github.com/mynenik/kForth-64/blob/master/forth-src/protect.4th
>

I revised protect.4th to provide general purpose data buffer protection,
e.g. create write-protected data tables. In addition to PROTECT-DEF for
write protecting the executable data of colon definitions, PROTECT-DATA
may be used to protect a data buffer of constant values,

PROTECT-DATA ( aold u -- anew u )

aold is the address of the existing read/write-able data buffer and u is
its size in bytes. PROTECT-DATA copies the constant data into a
read-only buffer of the same size. Currently there is a size limit of 1K
bytes for the constant data buffer -- the memory management code in
protect.4th may be revised to overcome this limit. PROTECT-DATA does not
tamper with the original data buffer

An example (in kForth) where this is useful is in the checksum program,
sha512.4th, which uses a table of constants for computing the 512 byte
checksum of a data buffer.

-----
include ans-words
include strings
include modules
include utils
include ssd
include protect

\ Hash constant words K for SHA-512
HEX
428A2F98D728AE22 7137449123EF65CD B5C0FBCFEC4D3B2F E9B5DBA58189DBBC
3956C25BF348B538 59F111F1B605D019 923F82A4AF194F9B AB1C5ED5DA6D8118
D807AA98A3030242 12835B0145706FBE 243185BE4EE4B28C 550C7DC3D5FFB4E2
72BE5D74F27B896F 80DEB1FE3B1696B1 9BDC06A725C71235 C19BF174CF692694
E49B69C19EF14AD2 EFBE4786384F25E3 0FC19DC68B8CD5B5 240CA1CC77AC9C65
2DE92C6F592B0275 4A7484AA6EA6E483 5CB0A9DCBD41FBD4 76F988DA831153B5
983E5152EE66DFAB A831C66D2DB43210 B00327C898FB213F BF597FC7BEEF0EE4
C6E00BF33DA88FC2 D5A79147930AA725 06CA6351E003826F 142929670A0E6E70
27B70A8546D22FFC 2E1B21385C26C926 4D2C6DFC5AC42AED 53380D139D95B3DF
650A73548BAF63DE 766A0ABB3C77B2A8 81C2C92E47EDAEE6 92722C851482353B
A2BFE8A14CF10364 A81A664BBC423001 C24B8B70D0F89791 C76C51A30654BE30
D192E819D6EF5218 D69906245565A910 F40E35855771202A 106AA07032BBD1B8
19A4C116B8D2D0C8 1E376C085141AB53 2748774CDF8EEB99 34B0BCB5E19B48A8
391C0CB3C5C95A63 4ED8AA4AE3418ACB 5B9CCA4F7763E373 682E6FF3D6B2B8A3
748F82EE5DEFB2FC 78A5636F43172F60 84C87814A1F0AB72 8CC702081A6439EC
90BEFFFA23631E28 A4506CEBDE82BDE9 BEF9A3F7B2C67915 C67178F2E372532B
CA273ECEEA26619C D186B8C721C0C207 EADA7DD6CDE0EB1E F57D4F7FEE6ED178
06F067AA72176FBA 0A637DC5A2C898A6 113F9804BEF90DAE 1B710B35131C471B
28DB77F523047D84 32CAAB7B40C72493 3C9EBE0A15C9BEBC 431D67C49C100D4C
4CC5D4BECB3E42B6 597F299CFC657E2A 5FCB6FAB3AD6FAEC 6C44198C4A475817
50 table K512[]

\ Read-only versiion of K512[]
K512[] 50 cells Protect-Data drop constant K512P[]

cr .( Base is HEX ) cr
-----

The word TABLE is defined in utils.4th -- it creates the ordinary data
buffer, the address of which is returned by the word K512[]. To make a
read-only version of this data buffer,

Example of using protect.4th (in kForth-64)
-----
include protect-ex1

Base is HEX
ok
K512P[] @ .
428A2F98D728AE22 ok

K512P[] 8 cells + @ u.
D807AA98A3030242 ok
ok
0 K512[] ! \ the original buffer still exists and we can modify it.
ok
0 K512P[] ! \ attempt to modify the read-only version
Segmentation fault (core dumped)
$ ------

> One problem with the current approach is a segmentation fault on
> executing BYE , because the cleanup code executed upon BYE tries to free
> the new byte code memory. This is why a protection flag is needed in the
> dictionary header, which involves changes to the source code for the
> Forth system. However, these are relatively simple changes to kForth.
> ...

Unlike protecting the executable code of colon definitions, PROTECT-DATA
does not require any changes to the internals of the Forth system, so it
is immediately useful for creating read-only data buffers. There is no
segmentation fault upon exiting the Forth system when using
PROTECT-DATA, because K512P[] is simply a constant, and there is no
attempt to free the read-only buffer.

--
Krishna

Re: Hardening Defined Words

<tcpnkh$j15$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19591&group=comp.lang.forth#19591

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!aioe.org!NZ87pNe1TKxNDknVl4tZhw.user.46.165.242.91.POSTED!not-for-mail
From: antis...@math.uni.wroc.pl
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Mon, 8 Aug 2022 01:07:29 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tcpnkh$j15$1@gioia.aioe.org>
References: <tcklr5$3jiko$1@dont-email.me> <b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com> <tcmf51$1prg$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="19493"; posting-host="NZ87pNe1TKxNDknVl4tZhw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.10.0-9-amd64 (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:Lc+h3VPMlzfMZEjKiLRXsCO8XrU=
 by: antis...@math.uni.wroc.pl - Mon, 8 Aug 2022 01:07 UTC

Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
>
> kForth uses a separate type stack to distinguish between ordinary
> numbers and addresses. This has proven useful in flagging common
> mistakes caused by incorrect stack manipulation. It is quite useful, I
> think, in aiding beginning Forth programmers to reveal the source of the
> problem. The added complexity of the type stack gives a performance hit
> of about 15% in kForth.

If you limit this to stack, then it may help beginners, but
will miss more "interesting" errors, like having a record
with numbers in some fields and xt's in other. To detect
access to wrong field you would need tags on _all_ data.

--
Waldek Hebisch

Re: Hardening Defined Words

<nnd$5118b55c$36a4488f@3c1c20e49841f6d8>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19716&group=comp.lang.forth#19716

  copy link   Newsgroups: comp.lang.forth
Newsgroups: comp.lang.forth
References: <tcklr5$3jiko$1@dont-email.me> <tcmg1o$218d$1@dont-email.me> <2022Aug7.161158@mips.complang.tuwien.ac.at> <tconob$klc8$1@dont-email.me>
Subject: Re: Hardening Defined Words
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: alb...@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$5118b55c$36a4488f@3c1c20e49841f6d8>
Organization: KPN B.V.
Date: Tue, 16 Aug 2022 22:05:54 +0200
Path: i2pn2.org!rocksolid2!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe004.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 24
Injection-Date: Tue, 16 Aug 2022 22:05:54 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1568
 by: none - Tue, 16 Aug 2022 20:05 UTC

In article <tconob$klc8$1@dont-email.me>,
Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
<SNIP>
>I agree that a Forth system architecture which provides memory
>protection for dictionary headers, non-native executable code of colon
>definitions, and for native code of CODE words/ordinary definitions is
>possible.

Note that all this effort expended is for the case of defects in the
program. It is much more useful to prevent defects.
Making the the architecture more complicated doesn't help for preventing
defects.

>--
>Krishna
>

Groetjes Albert
--
"in our communism country Viet Nam, people are forced to be
alive and in the western country like US, people are free to
die from Covid 19 lol" duc ha
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Re: Hardening Defined Words

<cf38725e-5d33-4792-a9ed-2280e9b573ean@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19737&group=comp.lang.forth#19737

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a0c:a88a:0:b0:474:7f16:f272 with SMTP id x10-20020a0ca88a000000b004747f16f272mr21796926qva.4.1660740147834;
Wed, 17 Aug 2022 05:42:27 -0700 (PDT)
X-Received: by 2002:a37:9344:0:b0:6b9:b91a:1634 with SMTP id
v65-20020a379344000000b006b9b91a1634mr18241241qkd.75.1660740147561; Wed, 17
Aug 2022 05:42:27 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 17 Aug 2022 05:42:27 -0700 (PDT)
In-Reply-To: <nnd$5118b55c$36a4488f@3c1c20e49841f6d8>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:3f7a:20d0:e081:2973:50ee:6ef6;
posting-account=V5nGoQoAAAC_P2U0qnxm2kC0s1jNJXJa
NNTP-Posting-Host: 2600:1700:3f7a:20d0:e081:2973:50ee:6ef6
References: <tcklr5$3jiko$1@dont-email.me> <tcmg1o$218d$1@dont-email.me>
<2022Aug7.161158@mips.complang.tuwien.ac.at> <tconob$klc8$1@dont-email.me> <nnd$5118b55c$36a4488f@3c1c20e49841f6d8>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cf38725e-5d33-4792-a9ed-2280e9b573ean@googlegroups.com>
Subject: Re: Hardening Defined Words
From: sdwjac...@gmail.com (S Jack)
Injection-Date: Wed, 17 Aug 2022 12:42:27 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1751
 by: S Jack - Wed, 17 Aug 2022 12:42 UTC

On Tuesday, August 16, 2022 at 3:05:59 PM UTC-5, none albert wrote:
> In article <tconob$klc8$1...@dont-email.me>,
> Krishna Myneni <krishna...@ccreweb.org> wrote:

> Note that all this effort expended is for the case of defects in the
> program. It is much more useful to prevent defects.

I don't discount hardening of "perfect" code not because the code may
not be perfect but because hardware in the field under stress doesn't
always follow the code.
--
me

Re: Hardening Defined Words

<b1969cd0-07c1-4579-841f-a0f14bbeba38n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19738&group=comp.lang.forth#19738

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:205:b0:343:282:3d0e with SMTP id b5-20020a05622a020500b0034302823d0emr23201971qtx.436.1660750034516;
Wed, 17 Aug 2022 08:27:14 -0700 (PDT)
X-Received: by 2002:a05:620a:454b:b0:6bb:63e1:7456 with SMTP id
u11-20020a05620a454b00b006bb63e17456mr5632714qkp.328.1660750034360; Wed, 17
Aug 2022 08:27:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 17 Aug 2022 08:27:14 -0700 (PDT)
In-Reply-To: <tcklr5$3jiko$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=77.174.47.232; posting-account=Ebqe4AoAAABfjCRL4ZqOHWv4jv5ZU4Cs
NNTP-Posting-Host: 77.174.47.232
References: <tcklr5$3jiko$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b1969cd0-07c1-4579-841f-a0f14bbeba38n@googlegroups.com>
Subject: Re: Hardening Defined Words
From: the.beez...@gmail.com (Hans Bezemer)
Injection-Date: Wed, 17 Aug 2022 15:27:14 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3032
 by: Hans Bezemer - Wed, 17 Aug 2022 15:27 UTC

On Saturday, August 6, 2022 at 5:06:15 AM UTC+2, Krishna Myneni wrote:
Well, 4tH doesn't have that issue.

: foo 0 ;
' foo execute . \ same as typing FOO
0 ' foo ! \ store a zero at the execution address for FOO
foo

Compiles to:

4tH message: No errors at word 10
Object size: 10 words
String size: 0 chars
Variables : 0 cells
Strings : 0 chars
Symbols : 1 names
Reliable : Yes

Addr| Opcode Operand Argument

0| branch 2 foo
1| literal 0
2| exit 0
3| literal 0
4| execute 0
5| . 0
6| literal 0
7| literal 0
8| ! 0
9| call 0 foo

And executes: "0 Executing; Word 8: Bad variable"
Where of course, the leading zero is generated by the program.

What ' returns is the address of "foo" in the Code Segment and "!" treats this as an address in the Integer Segment.
Unfortunately, address 0 of the Code Segment points to an address in the Integer Segment that is protected and
cannot be overwritten. Hence, it is a "bad variable".

4tH segmentation has been there since its very inception - and some segments are r/o (like the code- or the string
segment), some are partially r/o (system vars in the integer segment) and some are completely r/w (like the
character segment). Every access to these segments is either closely guarded or just impossible, since there
are no words to write anything there.

I don't have to go into "bar", because that one is equally impossible.

Maybe one could design an equally segmented Forth compiler, idunno. Never tried. But it *has* worked for me the last
30 odd years.

Hans Bezemer

Re: Hardening Defined Words

<tdleu4$120kg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19749&group=comp.lang.forth#19749

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Thu, 18 Aug 2022 08:30:42 -0500
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <tdleu4$120kg$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me> <tcmg1o$218d$1@dont-email.me>
<2022Aug7.161158@mips.complang.tuwien.ac.at> <tconob$klc8$1@dont-email.me>
<nnd$5118b55c$36a4488f@3c1c20e49841f6d8>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 Aug 2022 13:30:44 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f4ebdeda1f0bad114bcf6059fda2aa85";
logging-data="1114768"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wTg75Hj4tBlRmrqNUaBqE"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.12.0
Cancel-Lock: sha1:jipX7etWRGs0dLyIuLeM/D1CMow=
Content-Language: en-US
In-Reply-To: <nnd$5118b55c$36a4488f@3c1c20e49841f6d8>
 by: Krishna Myneni - Thu, 18 Aug 2022 13:30 UTC

On 8/16/22 15:05, albert wrote:
> In article <tconob$klc8$1@dont-email.me>,
> Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
> <SNIP>
>> I agree that a Forth system architecture which provides memory
>> protection for dictionary headers, non-native executable code of colon
>> definitions, and for native code of CODE words/ordinary definitions is
>> possible.
>
> Note that all this effort expended is for the case of defects in the
> program. It is much more useful to prevent defects.

Write protecting the virtual threaded code using low-level OS methods is
a means of *detecting* program defects which corrupt the Forth system's
code. Otherwise, a defective word may corrupt a part of the Forth system
for which the consequences may not be readily apparent when executing
words. With low level memory protection of the virtual threaded code,
such corruption becomes immediately obvious.

> Making the the architecture more complicated doesn't help for preventing
> defects.
>

The people who write link loaders may disagree with you -- such
protection usually exists for native code on desktop systems -- the
suggestion here is to extend memory protection to virtual threaded code
on the same type of systems.

--
Krishna

Re: Hardening Defined Words

<tdlfms$12676$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19750&group=comp.lang.forth#19750

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: krishna....@ccreweb.org (Krishna Myneni)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Thu, 18 Aug 2022 08:43:55 -0500
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <tdlfms$12676$1@dont-email.me>
References: <tcklr5$3jiko$1@dont-email.me>
<b175222f-bc4c-417e-b366-2f6ef38466den@googlegroups.com>
<tcmf51$1prg$1@dont-email.me> <tcpnkh$j15$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 Aug 2022 13:43:56 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f4ebdeda1f0bad114bcf6059fda2aa85";
logging-data="1120486"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198JAhVzha6+EPW9gfdqEkl"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.12.0
Cancel-Lock: sha1:zqk2GxteyXrcTYSP+W8uIZwqcxk=
In-Reply-To: <tcpnkh$j15$1@gioia.aioe.org>
Content-Language: en-US
 by: Krishna Myneni - Thu, 18 Aug 2022 13:43 UTC

On 8/7/22 20:07, antispam@math.uni.wroc.pl wrote:
> Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
>>
>> kForth uses a separate type stack to distinguish between ordinary
>> numbers and addresses. This has proven useful in flagging common
>> mistakes caused by incorrect stack manipulation. It is quite useful, I
>> think, in aiding beginning Forth programmers to reveal the source of the
>> problem. The added complexity of the type stack gives a performance hit
>> of about 15% in kForth.
>
> If you limit this to stack, then it may help beginners, but
> will miss more "interesting" errors, like having a record
> with numbers in some fields and xt's in other. To detect
> access to wrong field you would need tags on _all_ data.
>

The programmer must keep track of which member fields of a structure are
addresses (pointers) and which are not when accessing them through fetch
operators. kForth provides distinct fetch operators for single cell
non-address values and for addresses: @ for non-address values, and A@
for address values. This allows the value to be type-tagged when it is
fetched onto the data stack and for subsequent errors to be caught. For
example, the sequences "@ @" and "@ A@" will result in a virtual machine
error, while "A@ @" or "A@ A@" are legal. It is up to the programmer to
use @ and A@ appropriately when accessing member fields of a structure.
kForth does not catch usage errors when a field is accessed, but such
errors are likely to result in the VM reporting a type mismatch error
further down the execution chain.

--
Krishna

Re: Hardening Defined Words

<tdmkuo$1rta$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19754&group=comp.lang.forth#19754

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!aioe.org!7AktqsUqy5CCvnKa3S0Dkw.user.46.165.242.75.POSTED!not-for-mail
From: dxfo...@gmail.com (dxforth)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Fri, 19 Aug 2022 10:19:35 +1000
Organization: Aioe.org NNTP Server
Message-ID: <tdmkuo$1rta$1@gioia.aioe.org>
References: <tcklr5$3jiko$1@dont-email.me> <tcmg1o$218d$1@dont-email.me>
<2022Aug7.161158@mips.complang.tuwien.ac.at> <tconob$klc8$1@dont-email.me>
<nnd$5118b55c$36a4488f@3c1c20e49841f6d8> <tdleu4$120kg$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="61354"; posting-host="7AktqsUqy5CCvnKa3S0Dkw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.1.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: dxforth - Fri, 19 Aug 2022 00:19 UTC

On 18/08/2022 23:30, Krishna Myneni wrote:
> On 8/16/22 15:05, albert wrote:
>> In article <tconob$klc8$1@dont-email.me>,
>> Krishna Myneni  <krishna.myneni@ccreweb.org> wrote:
>> <SNIP>
>>> I agree that a Forth system architecture which provides memory
>>> protection for dictionary headers, non-native executable code of colon
>>> definitions, and for native code of CODE words/ordinary definitions is
>>> possible.
>>
>> Note that all this effort expended is for the case of defects in the
>> program. It is much more useful to prevent defects.
>
> Write protecting the virtual threaded code using low-level OS methods is a means of *detecting* program defects which corrupt the Forth system's code. Otherwise, a defective word may corrupt a part of the Forth system for which the consequences may not be readily apparent when executing words. With low level memory protection of the virtual threaded code, such corruption becomes immediately obvious.

Lack of checking in general should mean Forth applications are the most
unreliable there are. Yet reports I've seen suggest opposite is true.
Working 'closer to the metal' I believe forth programmers are in a better
position to know what can go wrong. In contrast, programmers in other
languages rely on the compiler to tell them what they're doing is wrong.

Re: Hardening Defined Words

<166087087894.31034.14766942655302290779@media.vsta.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19755&group=comp.lang.forth#19755

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!lilly.ping.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: van...@vsta.org (Andy Valencia)
Newsgroups: comp.lang.forth
Subject: Re: Hardening Defined Words
Date: Thu, 18 Aug 2022 18:01:18 -0700
Lines: 19
Message-ID: <166087087894.31034.14766942655302290779@media.vsta.org>
References: <2022Aug7.161158@mips.complang.tuwien.ac.at> <tconob$klc8$1@dont-email.me> <nnd$5118b55c$36a4488f@3c1c20e49841f6d8> <tdleu4$120kg$1@dont-email.me>
X-Trace: individual.net iRfmJS/3+t9g/wsvv0DIUwFRhqS0UZa6rXg9q921NtZnC6aiDE
X-Orig-Path: media
Cancel-Lock: sha1:lN9fbBDpy7DYo8AJdV7vovfnUwA=
User-Agent: rn.py v0.0.1
 by: Andy Valencia - Fri, 19 Aug 2022 01:01 UTC

dxforth <dxforth@gmail.com> writes:
> Lack of checking in general should mean Forth applications are the most
> unreliable there are. Yet reports I've seen suggest opposite is true.
> Working 'closer to the metal' I believe forth programmers are in a better
> position to know what can go wrong. In contrast, programmers in other
> languages rely on the compiler to tell them what they're doing is wrong.

I'll go ahead and admit it: the hardest bugs to find I've ever written
were in ForthOS. Next was VSTa (in C), and then downward from there.
I think Golang let me write the hairiest performance intensive code
while still hitting reliability with little effort.

But, admittedly, it wasn't OS kernel code. Nor was the Python code
which was not far away from Golang in ease, though its performance
and scalability are a pathetic shadow of Golang.

Andy Valencia
Home page: https://www.vsta.org/andy/
To contact me: https://www.vsta.org/contact/andy.html

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor