| ︙ | | | ︙ | |
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# See the safe.n man page for details.
#
# Copyright (c) 1996-1997 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: safe.tcl,v 1.10.2.6 2007/04/08 14:59:25 dgp Exp $
#
# The implementation is based on namespaces. These naming conventions
# are followed:
# Private procs starts with uppercase.
# Public procs are exported and starts with lowercase
#
|
|
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# See the safe.n man page for details.
#
# Copyright (c) 1996-1997 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: safe.tcl,v 1.10.2.7 2008/06/26 04:07:40 dgp Exp $
#
# The implementation is based on namespaces. These naming conventions
# are followed:
# Private procs starts with uppercase.
# Public procs are exported and starts with lowercase
#
|
| ︙ | | | ︙ | |
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
# build new one
set slave_auto_path {}
set i 0
foreach dir $access_path {
Set [PathToken $i $slave] $dir
lappend slave_auto_path "\$[PathToken $i]"
incr i
}
Set $nname $i
Set [PathListName $slave] $access_path
Set [VirtualPathListName $slave] $slave_auto_path
Set [StaticsOkName $slave] $staticsok
Set [NestedOkName $slave] $nestedok
Set [DeleteHookName $slave] $deletehook
SyncAccessPath $slave
}
|
>
>
>
>
>
>
>
>
>
>
>
>
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# build new one
set slave_auto_path {}
set i 0
foreach dir $access_path {
Set [PathToken $i $slave] $dir
lappend slave_auto_path "\$[PathToken $i]"
incr i
}
# Extend the access list with the paths used to look for Tcl
# Modules. We safe the virtual form separately as well, as
# syncing it with the slave has to be defered until the
# necessary commands are present for setup.
foreach dir [::tcl::tm::list] {
lappend access_path $dir
Set [PathToken $i $slave] $dir
lappend slave_auto_path "\$[PathToken $i]"
lappend slave_tm_path "\$[PathToken $i]"
incr i
}
Set $nname $i
Set [PathListName $slave] $access_path
Set [VirtualPathListName $slave] $slave_auto_path
Set [TmPathListName $slave] $slave_tm_path
Set [StaticsOkName $slave] $staticsok
Set [NestedOkName $slave] $nestedok
Set [DeleteHookName $slave] $deletehook
SyncAccessPath $slave
}
|
| ︙ | | | ︙ | |
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
# This alias lets the slave use the encoding names, convertfrom,
# convertto, and system, but not "encoding system <name>" to set
# the system encoding.
::interp alias $slave encoding {} [namespace current]::AliasEncoding \
$slave
# This alias lets the slave have access to a subset of the 'file'
# command functionality.
AliasSubset $slave file file dir.* join root.* ext.* tail \
path.* split
# This alias interposes on the 'exit' command and cleanly terminates
# the slave.
::interp alias $slave exit {} [namespace current]::interpDelete $slave
# The allowed slave variables already have been set
# by Tcl_MakeSafe(3)
# Source init.tcl into the slave, to get auto_load and other
# procedures defined:
if {[catch {::interp eval $slave\
{source [file join $tcl_library init.tcl]}} msg]} {
Log $slave "can't source init.tcl ($msg)"
error "can't source init.tcl into slave $slave ($msg)"
}
return $slave
}
# Add (only if needed, avoid duplicates) 1 level of
# sub directories to an existing path list.
# Also removes non directories from the returned list.
|
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
|
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# This alias lets the slave use the encoding names, convertfrom,
# convertto, and system, but not "encoding system <name>" to set
# the system encoding.
::interp alias $slave encoding {} [namespace current]::AliasEncoding \
$slave
# Handling Tcl Modules, we need a restricted form of Glob.
::interp alias $slave glob {} [namespace current]::AliasGlob \
$slave
# This alias lets the slave have access to a subset of the 'file'
# command functionality.
AliasSubset $slave file file dir.* join root.* ext.* tail \
path.* split
# This alias interposes on the 'exit' command and cleanly terminates
# the slave.
::interp alias $slave exit {} [namespace current]::interpDelete $slave
# The allowed slave variables already have been set
# by Tcl_MakeSafe(3)
# Source init.tcl and tm.tcl into the slave, to get auto_load
# and other procedures defined:
if {[catch {::interp eval $slave\
{source [file join $tcl_library init.tcl]}} msg]} {
Log $slave "can't source init.tcl ($msg)"
error "can't source init.tcl into slave $slave ($msg)"
}
if {[catch {::interp eval $slave \
{source [file join $tcl_library tm.tcl]}} msg]} {
Log $slave "can't source tm.tcl ($msg)"
error "can't source tm.tcl into slave $slave ($msg)"
}
# Sync the paths used to search for Tcl modules. This can be
# done only now, after tm.tcl was loaded.
::interp eval $slave [list ::tcl::tm::add {*}[Set [TmPathListName $slave]]]
return $slave
}
# Add (only if needed, avoid duplicates) 1 level of
# sub directories to an existing path list.
# Also removes non directories from the returned list.
|
| ︙ | | | ︙ | |
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
# returns the variable name of the complete path list
proc PathListName {slave} {
return "[InterpStateName $slave](access_path)"
}
# returns the variable name of the complete path list
proc VirtualPathListName {slave} {
return "[InterpStateName $slave](access_path_slave)"
}
# returns the variable name of the number of items
proc PathNumberName {slave} {
return "[InterpStateName $slave](access_path,n)"
}
# returns the staticsok flag var name
proc StaticsOkName {slave} {
|
>
>
>
>
|
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
# returns the variable name of the complete path list
proc PathListName {slave} {
return "[InterpStateName $slave](access_path)"
}
# returns the variable name of the complete path list
proc VirtualPathListName {slave} {
return "[InterpStateName $slave](access_path_slave)"
}
# returns the variable name of the complete tm path list
proc TmPathListName {slave} {
return "[InterpStateName $slave](tm_path_slave)"
}
# returns the variable name of the number of items
proc PathNumberName {slave} {
return "[InterpStateName $slave](access_path,n)"
}
# returns the staticsok flag var name
proc StaticsOkName {slave} {
|
| ︙ | | | ︙ | |
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
if {![file readable $file]} {
# don't tell the file path
error "not readable"
}
}
# AliasSource is the target of the "source" alias in safe interpreters.
proc AliasSource {slave args} {
set argc [llength $args]
# Allow only "source filename"
if {$argc != 1} {
set msg "wrong # args: should be \"source fileName\""
Log $slave "$msg ($args)"
return -code error $msg
}
set file [lindex $args 0]
# get the real path from the virtual one.
if {[catch {set file [TranslatePath $slave $file]} msg]} {
Log $slave $msg
return -code error "permission denied"
}
# check that the path is in the access path of that slave
if {[catch {FileInAccessPath $slave $file} msg]} {
Log $slave $msg
return -code error "permission denied"
}
# do the checks on the filename :
if {[catch {CheckFileName $slave $file} msg]} {
Log $slave "$file:$msg"
return -code error $msg
}
# passed all the tests , lets source it:
if {[catch {::interp invokehidden $slave source $file} msg]} {
Log $slave $msg
return -code error "script error"
}
return $msg
}
# AliasLoad is the target of the "load" alias in safe interpreters.
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
|
|
|
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
if {![file readable $file]} {
# don't tell the file path
error "not readable"
}
}
# AliasGlob is the target of the "glob" alias in safe interpreters.
proc AliasGlob {slave args} {
Log $slave "GLOB ! $args" NOTICE
set cmd {}
set at 0
set dir {}
set virtualdir {}
while {$at < [llength $args]} {
switch -glob -- [set opt [lindex $args $at]] {
-nocomplain -
-join { lappend cmd $opt ; incr at }
-directory {
lappend cmd $opt ; incr at
set virtualdir [lindex $args $at]
# get the real path from the virtual one.
if {[catch {set dir [TranslatePath $slave $virtualdir]} msg]} {
Log $slave $msg
return -code error "permission denied"
}
# check that the path is in the access path of that slave
if {[catch {DirInAccessPath $slave $dir} msg]} {
Log $slave $msg
return -code error "permission denied"
}
lappend cmd $dir ; incr at
}
pkgIndex.tcl {
# Oops, this is globbing a subdirectory in regular
# package search. That is not wanted. Abort,
# handler does catch already (because glob was not
# defined before). See package.tcl, lines 484ff in
# tclPkgUnknown.
error "unknown command glob"
}
-* {
Log $slave "Safe base rejecting glob option '$opt'"
error "Safe base rejecting glob option '$opt'"
}
default {
lappend cmd $opt ; incr at
}
}
}
Log $slave "GLOB = $cmd" NOTICE
if {[catch {::interp invokehidden $slave glob {*}$cmd} msg]} {
Log $slave $msg
return -code error "script error"
}
Log $slave "GLOB @ $msg" NOTICE
# Translate path back to what the slave should see.
set res {}
foreach p $msg {
regsub -- ^$dir $p $virtualdir p
lappend res $p
}
Log $slave "GLOB @ $res" NOTICE
return $res
}
# AliasSource is the target of the "source" alias in safe interpreters.
proc AliasSource {slave args} {
set argc [llength $args]
# Extended for handling of Tcl Modules to allow not only
# "source filename", but "source -encoding E filename" as
# well.
if {[lindex $args 0] eq "-encoding"} {
incr argc -2
set encoding [lrange $args 0 1]
set at 2
} else {
set at 0
set encoding {}
}
if {$argc != 1} {
set msg "wrong # args: should be \"source ?-encoding E? fileName\""
Log $slave "$msg ($args)"
return -code error $msg
}
set file [lindex $args $at]
# get the real path from the virtual one.
if {[catch {set file [TranslatePath $slave $file]} msg]} {
Log $slave $msg
return -code error "permission denied"
}
# check that the path is in the access path of that slave
if {[catch {FileInAccessPath $slave $file} msg]} {
Log $slave $msg
return -code error "permission denied"
}
# do the checks on the filename :
if {[catch {CheckFileName $slave $file} msg]} {
Log $slave "$file:$msg"
return -code error $msg
}
# passed all the tests , lets source it:
if {[catch {::interp invokehidden $slave source {*}$encoding $file} msg]} {
Log $slave $msg
return -code error "script error"
}
return $msg
}
# AliasLoad is the target of the "load" alias in safe interpreters.
|
| ︙ | | | ︙ | |
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
lappend norm_access_path [file normalize $path]
}
if {[lsearch -exact $norm_access_path $norm_parent] == -1} {
error "\"$file\": not in access_path"
}
}
# This procedure enables access from a safe interpreter to only a subset of
# the subcommands of a command:
proc Subset {slave command okpat args} {
set subcommand [lindex $args 0]
if {[regexp $okpat $subcommand]} {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
lappend norm_access_path [file normalize $path]
}
if {[lsearch -exact $norm_access_path $norm_parent] == -1} {
error "\"$file\": not in access_path"
}
}
proc DirInAccessPath {slave dir} {
set access_path [GetAccessPath $slave]
if {[file isfile $dir]} {
error "\"$dir\": is a file"
}
# Normalize paths for comparison since lsearch knows nothing of
# potential pathname anomalies.
set norm_dir [file normalize $dir]
foreach path $access_path {
lappend norm_access_path [file normalize $path]
}
if {[lsearch -exact $norm_access_path $norm_dir] == -1} {
error "\"$dir\": not in access_path"
}
}
# This procedure enables access from a safe interpreter to only a subset of
# the subcommands of a command:
proc Subset {slave command okpat args} {
set subcommand [lindex $args 0]
if {[regexp $okpat $subcommand]} {
|
| ︙ | | | ︙ | |